AWS.StateStore reference
stateEncryption
Section titled “stateEncryption”Source:
packages/aws/src/StateStore/Encryption.tsKind: Layer · Provides:StateEncryption,AWS.StateKmsKey
Encrypt the secrets in any state store with AWS KMS.
Each secret (a Redacted value: a minted token, a Random value) is
encrypted with AES-256-GCM under a data key from KMS before the store
writes it, so the store only ever holds ciphertext. A deploy asks KMS for
one data key per stage. Every other value in a record stays readable, so
plans and alchemy state keep working.
Reading the secrets back takes kms:Decrypt on the key, and writing them
kms:GenerateDataKey. Each data key is bound to its stack and stage
(alchemy:stack, alchemy:stage in the encryption context), so an IAM
policy can grant decrypt for one stage only.
The AWS state stores use this with their own key unless you provide it. Provided to one of them, its key also encrypts the store’s table and buckets at rest.
stateEncryption: Choosing the AWS State Store’s Key
Section titled “stateEncryption: Choosing the AWS State Store’s Key”const stateStore = AWS.state().pipe( Layer.provide(AWS.stateEncryption({ kmsKeyId: "alias/my-state-key" })),);stateEncryption: Encrypting another Cloud’s State Store
Section titled “stateEncryption: Encrypting another Cloud’s State Store”Cloudflare state, secrets encrypted with AWS KMS
import * as AWS from "@alchemy.run/aws";import * as Cloudflare from "@alchemy.run/cloudflare";import * as Layer from "effect/Layer";
const Stack = Alchemy.Stack( "my-stack", { providers: Cloudflare.providers(), state: Cloudflare.state().pipe(Layer.provide(AWS.stateEncryption())), }, Effect.gen(function* () { // ... }),);Local state with encrypted secrets
import { localState } from "alchemy/State";
const stateStore = localState().pipe( Layer.provide(AWS.stateEncryption({ kmsKeyId: "alias/dev-state" })),);stateEncryption: Turning Encryption Off
Section titled “stateEncryption: Turning Encryption Off”Decrypting needs the key, so turn encryption off with the layer still in
place: set disable, deploy every stage (each decrypts its secrets as it
loads), then remove the layer. A stage still encrypted fails to load
without the layer, and says so. The AWS state stores encrypt by default,
so keep disable on them to store secrets in plaintext.
state: Cloudflare.state().pipe(Layer.provide(AWS.stateEncryption({ disable: true }))),Source:
packages/aws/src/StateStore/Store.ts
The AWS state store: one DynamoDB table per account and region,
alchemy-state.
A deploy loads a stage’s state with one query and writes each change as it happens. Records too large for a DynamoDB item go to an S3 overflow bucket. Concurrent deploys of a stage fail fast on its lease instead of mixing writes.
The table is created on first use, except in CI, where alchemy state bootstrap --backend aws provisions it ahead.
state: Using the State Store
Section titled “state: Using the State Store”Default table
import * as Alchemy from "alchemy";import * as AWS from "@alchemy.run/aws";
const Stack = Alchemy.Stack( "my-stack", { providers: AWS.providers(), state: AWS.state() }, Effect.gen(function* () { // ... }),);A separate, named store
const stateStore = AWS.state({ name: "team-a" }); // table alchemy-state-team-astate: Encryption
Section titled “state: Encryption”The store has its own KMS key, alias/alchemy-state, created with the
table. The table and overflow bucket are encrypted at rest with it, and
every secret in state (a Redacted value) is encrypted with a data key
from it before it is written. A deploy asks KMS for one data key per
stage. A store created before encryption moves to the key on its first
use outside CI.
Provide AWS.stateEncryption() to choose the key, which then encrypts the
table and buckets too, or any other StateEncryption layer to encrypt the
secrets with it instead.
const stateStore = AWS.state().pipe( Layer.provide(AWS.stateEncryption({ kmsKeyId: "alias/my-state-key" })),);state: Migrating from the S3 State Store
Section titled “state: Migrating from the S3 State Store”Earlier versions kept state in an S3 bucket,
alchemy-state-<accountId>-<region>-an. A stage still there is refused
until it is moved. In the project, so the store the entrypoint
configures is the one migrated:
alchemy state migrate --profile <your-profile>The command moves every stage, or those --stack and --stage select.
The bucket policy closes each moved stage’s S3 objects, so every older
alchemy fails at its first read instead of deploying from the old copy;
the objects are kept. A stage another deploy holds is reported and
skipped: run the command again once it finishes. --rollback, run with
the same version, moves stages back to S3 before going back to an older
one.
state: Keeping State in S3
Section titled “state: Keeping State in S3”The S3 store
const stateStore = AWS.state({ backend: "s3" });A custom bucket, prefix and KMS key
const stateStore = AWS.state({ bucketName: "my-company-state-123456789012-us-east-1-an", prefix: "alchemy", encryption: { sseAlgorithm: "aws:kms", kmsMasterKeyId: "alias/alchemy-state" },});state: Supplying the AWS Environment
Section titled “state: Supplying the AWS Environment”The store resolves its account, region and credentials from the configured
profile, CI credentials or the ambient AWS environment. Provide an
AWSEnvironment to use another credential source, and the same layer to
AWS.providers().
const Stack = Alchemy.Stack( "my-stack", { providers: AWS.providers().pipe(Layer.provide(environment)), state: AWS.state().pipe(Layer.provide(environment)), }, Effect.gen(function* () { // ... }),);