AWS Step Functions
AWS Step Fucntions with Lambda
ASL (Amazon State Language)
{
"Comment": "Transaction Processor State Machine",
"StartAt": "ProcessTransaction",
"States": {
"ProcessTransaction": {
"Type": "Choice",
"Choices": [
{
"Variable": "&.TransactionType",
"StringEquals": "PURCHASE",
"Next": "ProcessPurchase"
},
{
"Variable": "&.TransactionType",
"StringEquals": "REFUND",
"Next": "ProcessRefund"
}
]
},
"ProcessRefund": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
},
"ProcessPurchase": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
}
}
}
State Machine
AWS Step Functions with SQS
ASL (Amazon State Language)
{
"Comment": "Transaction Processor State Machine using SQS",
"StartAt": "ProcessTransaction",
"States": {
"ProcessTransaction": {
"Type": "Pass",
"Next": "BroadcastToSqs"
},
"BroadcastToSqs": {
"Type": "Task",
"Resource": "arn:aws:states:::sqs:sendMessage",
"Parameters": {
"QueueUrl": "[link of QueueUrl]",
"MessageBody": {
"TransactionId.$": "$.TransactionId",
"Type.$": "$.Type"
}
},
"End": true
}
}
}
Event
{
"TransactionId": "abc",
"Type": "PURCHASE"
}
State Machine
AWS Step Functions with SNS
ASL (Amazon State Language)
{
"Comment": "Transaction Processor State Machine Using SNS",
"StartAt": "ProcessTransaction",
"States": {
"ProcessTransaction": {
"Type": "Pass",
"Next": "BroadcastToSns"
},
"BroadcastToSns": {
"Type": "Task",
"Resource": "arn:aws:state:::sns:publish",
"Parameters": {
"TopicArn": "[arn of Topic]",
"Parameters": {
"TransactionId.$": "$.TransactionId",
"Type.$": "$.Type",
"Source": "Step Functions!"
}
}
}
}
}
Event
{
"TransactionId": "abc",
"Type": "PURCHASE"
}
State Machine
AWS Step Functions with DynamoDB
ASL (Amazon State Language)
{
"Comment": "Transaction Processor State Machine",
"StartAt": "ProcessTransaction",
"States": {
"ProcessTransaction": {
"Type": "Pass",
"Next": "StoreHistory"
},
"StoreHistory": {
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:putItem",
"Parameters": {
"TableName": "TransactionHistoryTable",
"Item": {
"TransactionId": {
"s.$": "$.TransactionId"
}
},
"Retry": [
{
"ErrEquals": ["States.All"],
"IntervalSeconds": 1,
"MaxAttempts": 3
}
],
"End": true,
"ResultPash": "$.DynamoDB"
}
}
}
}
State Machine
Start an AWS Step Function Workflow From Lambda
AWS Lambda function:
python
import json
import bonto3
import uuid
client = boto3.client("stepfunction")
def lambda_handler(evetn, context):
#INPUT -> { "TransactionId": "foo", "Type": "PURCHASE" }
transactionId = str(uuid.uuid1())
input = {"TransactionId": transactionId, "Type": "PURCHASE"}
response = client.start_execution(
startMachineArn="[ARN of state machine]"
name=transactionId,
input=json.dumps(input)
)
AWS Step Function Map Task
input.json
{
"transactions": [
{
"transactionId": "R31",
"type": "PURCHASE",
"amount": 1344
},
{
"transactionId": "R39",
"type": "REFUND",
"amount": 40
},
{
"transactionId": "R31",
"type": "REFUND",
"amount": 12
}
]
}
ASL (Amazon State Language)
{
"StartAt": "Validate-All",
"States": {
"Validate-All": {
"Type": "Map",
"InputPath": "$",
"ItemsPath": "$.transactions",
"MaxConsurrency": 0,
"Iterator": {
"StartAt": "Valiate",
"States": {
"Validate": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "Complete"
},
"Complete": {
"Type": "Pass",
"End": true
}
}
},
"ResultPath": "$",
"End": true
}
}
}
lambda
def lambda_handler(event, context):
return event["transactionId"] + "-" + event["type"]
State Machine
Set timers with AWS Step Functions using Wait State
ASL (Amazon State Language)
{
"Comment": "A Heello World example of the Amazon States Language using Pass states",
"StartAt": "Wait 3 seconds",
"States": {
"Wait 3 secondes": {
"Type": "Wait",
"Seconds": 3,
"Next": "Wait Until Hard Coded"
},
"Wait Until Hard Coded": {
"Type": "Wait",
"Timestamp": "2019-05-18-T00:00:00Z",
"Next": "Dynamic Wait"
},
"Dynamic Wait": {
"Type": "Wait",
"TimestampPath": "$.waitUntilTimestamp",
"End": true
}
}
}
State Machine
Resources
- AWS Step Function Guides - Everything you need to know about Step Functions: https://www.youtube.com/watch?v=zCIpWFYDJ8s&list=PL9nWRykSBSFgQrO66TmO1vHFP6yuPF5G-