State Machines
A state machine allows you to design and automate business processes and data pipelines by composing compute resources (like functions and batch jobs) and other AWS services into a workflow. State machines manage failures, retries, parallelization, and observability, so you can focus on your business logic.
When to use them
- ETL processes: State machines can ensure that long-running, multi-step ETL jobs execute in order and complete successfully.
- Microservice orchestration: You can use state machines to combine multiple functions into a responsive, serverless application.
Defining states
You define state machines using the Amazon States Language. This example shows a simple order payment flow composed of Lambda functions:
resources:checkAndHoldProduct:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'check-and-hold-product.ts'billCustomer:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'bill-customer.ts'shipmentNotification:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'shipment-notification.ts'eshopBuyingProcessStateMachine:type: 'state-machine'properties:definition:StartAt: 'checkAndHold'States:checkAndHold:Type: TaskResource: $ResourceParam('checkAndHoldProduct', 'arn')Next: billbill:Type: TaskResource: $ResourceParam('billCustomer', 'arn')Next: notifynotify:Type: TaskResource: $ResourceParam('shipmentNotification', 'arn')Next: succeedsucceed:Type: Succeed
Retry example
This example shows a state machine that ties together a batch job and a function. The state machine is configured to retry only the upload step if it fails, since regenerating the report would be costly and unnecessary.
generateReport
: A batch job that generates a report.uploadReport
: A function that uploads the report.reportStateMachine
: The state machine that orchestrates the workflow.
resources:uploadReport:type: functionproperties:packaging:type: stacktape-lambda-buildpackproperties:entryfilePath: 'upload-report.ts'generateReport:type: 'batch-job'properties:container:packaging:type: stacktape-image-buildpackproperties:entryfilePath: generate-report.tsresources:cpu: 2memory: 7800reportStateMachine:type: 'state-machine'properties:definition:StartAt: 'generate'States:generate:Type: TaskResource: 'arn:aws:states:::batch:submitJob.sync'Parameters:JobDefinition: $ResourceParam('generateReport', 'jobDefinitionArn')JobName: report-jobJobQueue: $Param('SHARED_GLOBAL', 'BatchOnDemandJobQueue::Arn')Next: uploadupload:Type: TaskResource: $Param('uploadReport', 'LambdaFunction::Arn')Next: succeedRetry:- ErrorEquals:- 'State.ALL'IntervalSeconds: 10succeed:Type: Succeed
Referenceable parameters
The following parameters can be easily referenced using $ResourceParam directive directive.
To learn more about referencing parameters, refer to referencing parameters.
Arn of the state machine.
- Usage:
$ResourceParam('<<resource-name>>', 'arn')
AWS (physical) name of the state machine.
- Usage:
$ResourceParam('<<resource-name>>', 'name')