Close
logoStacktape docs



State Machines

Overview

State machines allow you to design and automate business processes and data pipelines by composing workloads (functions, batch-jobs) or other AWS services into workflows. Workflows manage failures, retries, parallelization, service integrations, and observability so developers can focus on higher-value business logic.

When to use

  • Extract, Transform, and Load (ETL) process - state machines ensure that long-running, multiple ETL jobs execute in order and complete successfully, instead of manually orchestrating those jobs or maintaining a separate application.

  • Orchestrate microservices - use state machines to combine multiple functions into responsive serverless applications and microservices.

Usage

➡️ Definition

When defining state machines you use Amazon states language.

Amazon states language syntax gives you ability to specify any workflow from easy ones to most complex ones.

Following example describes an order-payment flow made up of lambda functions:

resources:
checkAndHoldProduct:
Type: function
Properties:
packageConfig:
filePath: 'check-and-hold-product.ts'
billCustomer:
Type: function
Properties:
packageConfig:
filePath: 'bill-customer.ts'
shipmentNotification:
Type: function
Properties:
packageConfig:
filePath: 'shipment-notification.ts'
buyProcessStateMachine:
Type: 'state-machine'
Properties:
definition:
StartAt: 'checkAndHold'
States:
checkAndHold:
Type: Task
Resource: $GetParam('checkAndHoldProduct', 'LambdaFunction::Arn')
Next: bill
bill:
Type: Task
Resource: $GetParam('billCustomer', 'LambdaFunction::Arn')
Next: notify
notify:
Type: Task
Resource: $GetParam('shipmentNotification', 'LambdaFunction::Arn')
Next: succeed
succeed:
Type: Succeed

Retry example

Following example shows example with components:

  • generateReport - batch-job which generates report.
  • uploadReport - function that uploads generated report.

State machine definitions provide great flexibility. In this case we only retry upload part of our workflow, since re-generating report (in case of upload failure) would be costly and redundant.

resources:
uploadReport:
Type: function
Properties:
packageConfig:
filePath: 'upload-report.ts'
generateReport:
Type: 'batch-job'
Properties:
container:
imageConfig:
filePath: generate-report.ts
resources:
cpu: 2
memory: 7800
reportStateMachine:
Type: 'state-machine'
Properties:
definition:
StartAt: 'generate'
States:
generate:
Type: Task
Resource: 'arn:aws:states:::batch:submitJob.sync'
Parameters:
JobDefinition: $GetParam('generateReport', 'JobDefinition::Arn')
JobName: report-job
JobQueue: $GetParam('SHARED_GLOBAL', 'BatchOnDemandJobQueue::Arn')
Next: upload
upload:
Type: Task
Resource: $GetParam('uploadReport', 'LambdaFunction::Arn')
Next: succeed
Retry:
- ErrorEquals:
- 'State.ALL'
IntervalSeconds: 10
succeed:
Type: Succeed