docs/sf/guides/state.md
Serverless Framework Compose allows for the orchestration and deployment of multiple services. A crucial aspect of this is managing the state of each service, which includes storing outputs and other runtime information necessary for the correct operation of services. This guide covers how to set up and manage state using AWS S3 and AWS SSM.
State management in Serverless Framework Compose is crucial for:
Serverless Framework Compose uses AWS S3 to store the state of services and AWS SSM (Systems Manager) to track the location of the state storage.
The easiest way to manage state in Serverless Framework Compose is through its default, zero-configuration setup. When you don’t specify any state configuration, Serverless Framework Compose automatically handles everything for you.
serverless-compose.yml, Serverless Framework Compose automatically creates an S3 bucket to store the state.us-east-1 AWS region, under the parameter /serverless-framework/state/s3-bucket. This parameter contains a JSON object with the following keys:bucketName: The name of the S3 bucket.bucketRegion: The AWS region where the bucket is located.Note: To create the default state bucket, you must have the necessary permissions to put SSM parameters and create versioned S3 buckets. If you don’t have these permissions, you can set up a custom state configuration to use an existing S3 bucket.
Assume you have the following serverless-compose.yml with two services:
services:
service-a:
path: service-a
service-b:
path: service-b
params:
queueUrl: ${service-a.queueUrl}
In this case, without any additional state configuration, Serverless Framework Compose will:
To deploy all services and manage their state automatically:
serverless deploy
This command will:
queueUrl) as parameters.State allows you to execute commands on individual services even if they have dependencies on other services. When you use commands like
serverless <service> <command>
the Framework automatically fetches the necessary input data from the stored state, making it easy to manage and deploy services with complex interdependencies.
While the zero-configuration setup is convenient, there may be situations where you want to customize how and where the state is stored.
If you require this level of customization, you can specify custom state management settings in your serverless-compose.yml.
Here’s how you can do it:
You can specify an existing S3 bucket for storing the state using Resolvers:
state: my-s3-state-resolver
stages:
default:
resolvers:
my-aws-account:
type: aws
my-s3-state-resolver:
type: s3
bucketName: my-custom-state-bucket
services:
service-a:
path: service-a
service-b:
path: service-b
params:
queueUrl: ${service-a.queueUrl}
In this setup:
state field defines the state resolver, which is linked to an existing S3 bucket.Note: For more information on Resolvers, refer to the Resolvers documentation.
When you run serverless remove, the state stored in the S3 bucket is also deleted, ensuring that no unnecessary data is left in your AWS account.