Resources as code

Keep buckets, functions, policies, environment variables and secrets in a git repository, and fetch, plan, apply and live-sync them with the CLI.

Resources as code lets you treat a Spica instance like infrastructure: the source of truth is a folder of YAML and code files in git, changes are reviewed in pull requests, and spica apply brings an instance in line with the files.

Project layout

Text
my-project/
├── bucket/
│   ├── Posts/schema.yaml
│   └── Authors/schema.yaml
├── function/
│   └── hello/
│       ├── schema.yaml
│       ├── index.mjs          # or index.ts for TypeScript functions
│       └── package.json
├── policy/
│   └── Blog Reader/schema.yaml
├── env-var/
│   └── SITE_URL/schema.yaml
├── secret/
│   └── STRIPE_KEY/schema.yaml
└── .spica/                    # per-context values, keep out of git
    └── local.env
ModuleFolder nameFiles
bucketBucket titleschema.yaml
functionFunction nameschema.yaml, index.mjs or index.ts, package.json
policyPolicy nameschema.yaml
env-varKeyschema.yaml
secretKeyschema.yaml (never contains the value)

Resources are matched by folder name, and by _id when the file has one. Files written by spica fetch include _id.

bucket/Tags/schema.yaml
title: Tags
description: Tags used to group posts
primary: name
properties:
  name:
    type: string
    title: Name
  color:
    type: color
    title: Color

Add .spica/ to your .gitignore.

Fetch

Export an instance's resources into the current folder (or [dir]):

Terminal
spica fetch
Text
── ENVIRONMENT VARIABLE ──  +1
  + GREETING  key: GREETING

── FUNCTION ──  +1
  + hello  2 triggers

Plan:  +7 to write   ~0 to update   -0 to delete

✓ Fetch complete.  +7 written
OptionDescription
-y, --auto-approveWrite without asking.
--cleanDelete local folders whose resources no longer exist remotely.
--module <name>Only policy, bucket, env-var, secret or function.
--detailedShow full diffs before writing.
--concurrency <n>Parallel requests and writes (default 10).
--abort-on-errorStop at the first failure.

Plan

Compare the folder with the current context's instance and show what apply would do:

Terminal
spica plan --detailed
Text
── BUCKET ──  +1  ~1
  + Tags  2 properties
  ~ Posts  11 properties  [description]
      --- remote/schema.yaml
      +++ local/schema.yaml
      -description: Blog posts
      +description: Articles published on the blog

── ENVIRONMENT VARIABLE ──  +1
  + SITE_URL  key: SITE_URL

Plan:  +2 to create   ~1 to update   -0 to delete
OptionDescription
--detailedShow unified diffs instead of field names.
--jsonPrint the plan as JSON for scripts.
--detailed-exitcodeExit with 0 when there are no changes, 1 on errors and 2 when changes are planned.
--module <name>Limit to one module.
--concurrency <n>Parallel requests (default 10).

The JSON plan lists creates, updates and deletes per module:

JSON
[{"module": "bucket", "creates": [], "updates": [{"slug": "Tags", "summary": "2 properties", "changedFields": ["history", "acl", "indexes", "icon"]}], "deletes": []}]

Apply

Terminal
spica apply

apply shows the plan, asks for confirmation and applies it:

Text
Applying changes…
✔ Updating Bucket: Posts (1/1)
✔ Creating Bucket: Tags (1/1)
✔ Creating Environment Variable: SITE_URL (1/1)

✓ Apply completed.  +2 created  ~1 updated  -0 deleted
OptionDescription
-y, --auto-approveApply without asking. Use in CI.
--module <name>Limit to one module.
--detailedShow full diffs before applying.
--concurrency <n>Parallel requests (default 5).
--abort-on-errorStop at the first failure.

After you create new resources, run spica fetch once. The server fills in defaults (such as acl, icon and indexes) and ids, and fetching them keeps the next plan clean.

Develop with live sync

spica dev checks that the folder and the instance are in sync, then watches your files and applies every change as you save:

Terminal
spica dev
Text
✓ Already in sync. Starting watcher…
👁  Watching for changes in /home/me/my-project…  (Ctrl-C to stop)
✔ ~  Function  hello  [index]  updated

It watches schema.yaml, index.ts, index.mjs, index.js and package.json, and only uploads what changed: a code change uploads the code, a package.json change installs dependencies. Use it against a local or personal development instance, never against production.

Values per environment

Environment variable values can differ between instances, and secret values must never be committed. The CLI resolves them per context. See Values per environment.

Guards

A guard pins plan and apply to one directory on your machine, and optionally requires some folders to exist, so that you can't apply the wrong folder by accident:

Terminal
spica guard set --path ~/work/my-project --folders bucket,function,policy,env-var,secret
spica guard show
spica guard remove

The guard is stored in ~/.spicaguard. While it is set, plan and apply fail outside that directory:

Text
Invalid working directory: "/tmp/other". This command must be run from "/home/me/work/my-project".
  1. Develop against a local instance with spica project start and spica dev.
  2. Commit the files. Never commit .spica/.
  3. In CI, run spica plan --detailed-exitcode on pull requests and post the plan for review.
  4. After merge, run spica apply -y against staging, then production.

See the CI/CD with GitHub Actions recipe, and @spica-devkit/sync to plan and apply from your own services.