Compose State Storage
Serverless Compose stores the outputs of each deployed service in an Amazon S3 state bucket, so another service can reference them (${service-a.queueUrl}) in a later command without redeploying the first one. Compose creates and finds the bucket by itself; configure state only to keep it in a bucket you choose.
The following serverless-compose.yml needs no state configuration: service-b reads queueUrl from the stored state of service-a.
# serverless-compose.yml
services:
service-a:
path: service-a
service-b:
path: service-b
params:
queueUrl: ${service-a.queueUrl}
State applies to Compose projects only. A service deployed on its own with serverless deploy in its directory reads and writes no Compose state.
What Compose state contains
Compose state holds, for each deployed service, the CloudFormation stack outputs of its last deploy or info, as JSON:
{
"outputs": {
"queueUrl": "https://sqs.us-east-1.amazonaws.com/123456789012/service-a-dev-queue"
}
}
Each service has one object in the state bucket, at services/<runner type>/<stack ID>/state/state.json. The runner type is traditional for a serverless.yml service and cfn for a SAM or CloudFormation service. The stack ID is the full CloudFormation stack ID with each / replaced by _, so each stage and Region of a service has its own state, and a service that is removed and deployed again gets a new state object.
Commands that read and write Compose state
Compose writes a service's state after these commands, when it runs them on that service:
serverless deploy: the stack outputs after the deploy.serverless info: the current stack outputs, which also refreshes stale state.serverless remove: an empty object ({}); see State afterserverless remove.
Compose reads state when a command needs another service's outputs and doesn't run on that service. For example, serverless service-b deploy reads the stored outputs of service-a instead of running a command on it. A service that has no stack yet has no state; a reference to it fails, or uses its fallback, as described in Serverless Compose.
Default state bucket
When serverless-compose.yml has no state key, Compose uses a default state bucket in the AWS account of the Compose project's credentials (--aws-profile, the AWS resolver in serverless-compose.yml, or the default AWS credential chain). Compose looks for the bucket in us-east-1, whichever Region the services deploy to:
- Compose reads the SSM parameter
/serverless-framework/state/s3-bucketinus-east-1. Its value is JSON withbucketNameandbucketRegion. - When the parameter doesn't exist, Compose writes it with a new name,
serverless-framework-state-<uuid>, and creates that bucket inus-east-1with versioning enabled. The write is create-only: when several commands start at once, one creates the bucket and the others wait up to 60 seconds for it. A command that waits longer stops with an error that starts withThe S3 bucket "<bucket>" named in the SSM parameter "/serverless-framework/state/s3-bucket" was not ready within 60 seconds.; run it again. - When the parameter names a bucket that no longer exists, Compose creates it again with versioning enabled.
All Compose projects in an AWS account share this bucket. The deployment bucket that holds artifacts is a separate bucket (Deployment Bucket).
Permissions for the default state bucket
The Compose project's credentials need:
ssm:GetParameteron/serverless-framework/state/s3-bucketinus-east-1, ands3:CreateBucket, on every run.s3:PutBucketVersioningon every run: the bucket is inus-east-1, where S3 answersCreateBucketfor a bucket you already own with success (CreateBucket), and Compose then enables versioning again.ssm:PutParameterwhen Compose creates the parameter.s3:GetBucketVersioningwhile waiting for a bucket another command is creating.s3:GetObjectands3:PutObjecton the state bucket.
When SSM or S3 denies one of the calls that find or create the bucket (other than s3:GetBucketVersioning while waiting, which has its own Access denied when checking the S3 bucket message), the command stops with an error that starts with:
Access denied when accessing /serverless-framework/state/s3-bucket SSM parameter and serverless-framework-state-... S3 bucket. Please check your permissions and try again.
The bucket name in the message is a newly generated one, even when the parameter already names a bucket. To run Compose without these permissions, use your own state bucket.
Use your own state bucket
To keep Compose state in a bucket you manage, define an s3 resolver for the bucket and name it in the top-level state key of serverless-compose.yml:
# serverless-compose.yml
state: my-state-bucket
stages:
default:
resolvers:
my-aws-account:
type: aws
region: us-east-1
my-state-bucket:
type: s3
bucketName: my-company-compose-state
region: us-east-1
services:
service-a:
path: service-a
service-b:
path: service-b
params:
queueUrl: ${service-a.queueUrl}
state accepts the resolver's name as a string (state: my-state-bucket) or as an object (state: { resolver: my-state-bucket }). The resolver sets:
| Key | Required | Description |
|---|---|---|
type | Yes | s3; any other type stops the command with Unsupported state resolver type |
bucketName | Yes | An existing bucket with versioning enabled |
region | No | The bucket's Region. Default: us-east-1 |
serverSideEncryption | No | Sent as the ServerSideEncryption header when Compose writes state, for example AES256 or aws:kms |
Compose reads and writes state with the credentials of the aws resolver that contains the s3 resolver (my-aws-account in the example). When that aws resolver sets its own region, state reads and writes use that Region, so keep both region values the same as the bucket's Region.
Before each Compose command, Compose checks that the bucket has versioning enabled. The check uses the Compose project's credentials (not the aws resolver's) and the s3 resolver's region (us-east-1 when unset), and needs s3:GetBucketVersioning. When the check can't read the bucket (access denied, or no such bucket), the command stops with an error that starts with An error occurred while checking versioning for bucket "<bucket>". A bucket without versioning stops the command:
State bucket "my-company-compose-state" is not versioned. Please enable versioning for the bucket if you want to use it as a state store.
Compose never creates a bucket you name in state, and it creates no SSM parameter for it. For more on resolvers, see Variables and S3 Objects.
State after serverless remove
When serverless remove removes a service through Compose, Compose replaces that service's state object with an empty object ({}). It doesn't delete the state object or the state bucket, and the versioned bucket keeps earlier versions of the state.
The default state bucket holds the state of every Compose project in the AWS account, and a bucket you name in state can be shared too. To delete the removed service's state, delete every version of its object, services/<runner type>/<stack ID>/state/state.json, and leave the rest of the bucket. Delete the bucket only after you confirm that no other Compose project stores state in it; for the default bucket, then also delete the /serverless-framework/state/s3-bucket parameter in us-east-1, or the next Compose run creates the bucket again under the same name.
Related pages
- Serverless Compose: deploy several services together and pass outputs between them.
- Deployment Bucket: the S3 bucket that holds deployment artifacts.
- S3 Objects: the
s3resolver that a custom state bucket uses.