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
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| Module | Folder name | Files |
|---|---|---|
bucket | Bucket title | schema.yaml |
function | Function name | schema.yaml, index.mjs or index.ts, package.json |
policy | Policy name | schema.yaml |
env-var | Key | schema.yaml |
secret | Key | schema.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.
title: Tags
description: Tags used to group posts
primary: name
properties:
name:
type: string
title: Name
color:
type: color
title: ColorAdd .spica/ to your .gitignore.
Fetch
Export an instance's resources into the current folder (or [dir]):
spica fetch── 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| Option | Description |
|---|---|
-y, --auto-approve | Write without asking. |
--clean | Delete local folders whose resources no longer exist remotely. |
--module <name> | Only policy, bucket, env-var, secret or function. |
--detailed | Show full diffs before writing. |
--concurrency <n> | Parallel requests and writes (default 10). |
--abort-on-error | Stop at the first failure. |
Plan
Compare the folder with the current context's instance and show what apply would do:
spica plan --detailed── 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| Option | Description |
|---|---|
--detailed | Show unified diffs instead of field names. |
--json | Print the plan as JSON for scripts. |
--detailed-exitcode | Exit 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:
[{"module": "bucket", "creates": [], "updates": [{"slug": "Tags", "summary": "2 properties", "changedFields": ["history", "acl", "indexes", "icon"]}], "deletes": []}]Apply
spica applyapply shows the plan, asks for confirmation and applies it:
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| Option | Description |
|---|---|
-y, --auto-approve | Apply without asking. Use in CI. |
--module <name> | Limit to one module. |
--detailed | Show full diffs before applying. |
--concurrency <n> | Parallel requests (default 5). |
--abort-on-error | Stop 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:
spica dev✓ Already in sync. Starting watcher…
👁 Watching for changes in /home/me/my-project… (Ctrl-C to stop)
✔ ~ Function hello [index] updatedIt 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:
spica guard set --path ~/work/my-project --folders bucket,function,policy,env-var,secret
spica guard show
spica guard removeThe guard is stored in ~/.spicaguard. While it is set, plan and apply fail outside that directory:
Invalid working directory: "/tmp/other". This command must be run from "/home/me/work/my-project".Recommended workflow
- Develop against a local instance with
spica project startandspica dev. - Commit the files. Never commit
.spica/. - In CI, run
spica plan --detailed-exitcodeon pull requests and post the plan for review. - After merge, run
spica apply -yagainst staging, then production.
See the CI/CD with GitHub Actions recipe, and @spica-devkit/sync to plan and apply from your own services.