Skip to content

Instantly share code, notes, and snippets.

@heitorlessa
Created October 13, 2021 10:50
Show Gist options
  • Save heitorlessa/60da41620334a692a9f8a83549303741 to your computer and use it in GitHub Desktop.
Save heitorlessa/60da41620334a692a9f8a83549303741 to your computer and use it in GitHub Desktop.
Step Functions SDK Integration - Nested Sync Express Workflow
{
"StartAt": "Sit and wait",
"States": {
"Sit and wait": {
"Type": "Wait",
"Seconds": 1,
"Next": "Dummy Output"
},
"Dummy Output": {
"Type": "Pass",
"Result": {
"hello": "world"
},
"End": true
}
}
}
{
"StartAt": "Sit and wait",
"States": {
"Sit and wait": {
"Type": "Wait",
"Seconds": 1,
"Next": "Dummy Output"
},
"Dummy Output": {
"Type": "Pass",
"Result": {
"hello": "universe"
},
"End": true
}
}
}
{
"Comment": "Master express workflow",
"StartAt": "Wait 3s",
"States": {
"Wait 3s": {
"Comment": "A Wait state delays the state machine from continuing for a specified time.",
"Type": "Wait",
"Seconds": 3,
"Next": "Nested workflows"
},
"Nested workflows": {
"Comment": "A Parallel state can be used to create parallel branches of execution in your state machine.",
"Type": "Parallel",
"Next": "Bye",
"ResultPath": "$.nested_result_combined",
"Branches": [
{
"StartAt": "HelloWorldWorkflow",
"States": {
"HelloWorldWorkflow": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:sfn:startSyncExecution",
"Parameters": {
"StateMachineArn": "${HelloWorldWorkflow}",
"Input.$": "$"
},
"ResultSelector": {
"osrm.$": "States.StringToJson($.Output)"
},
"End": true
}
}
},
{
"StartAt": "HelloUniverseWorkflow",
"States": {
"HelloUniverseWorkflow": {
"Type": "Task",
"Resource": "arn:aws:states:::aws-sdk:sfn:startSyncExecution",
"Parameters": {
"StateMachineArn": "${HelloUniverseWorkflow}",
"Input.$": "$"
},
"ResultSelector": {
"orientation.$": "States.StringToJson($.Output)"
},
"End": true
}
}
}
]
},
"Bye": {
"Type": "Pass",
"End": true
}
}
}
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
Step Functions Express with nested workflow using native SDK integration
Parameters:
LogRetentionInDays:
Description: Expire CloudWatch Logs after X days
Type: Number
Default: 14
Resources:
##########################################################################
# Workflows #
##########################################################################
MainWorkflow:
Type: AWS::Serverless::StateMachine # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html
Properties:
DefinitionUri: main.asl.json
Type: EXPRESS
Role: !GetAtt MainWorkflowRole.Arn
DefinitionSubstitutions:
HelloWorldWorkflow: !Ref HelloWorldWorkflow
HelloUniverseWorkflow: !Ref HelloUniverseWorkflow
Tracing:
Enabled: True
Logging:
Destinations:
- CloudWatchLogsLogGroup:
LogGroupArn: !GetAtt MainWorkflowLogs.Arn
IncludeExecutionData: true # debugging
Level: 'ALL'
HelloWorldWorkflow:
Type: AWS::Serverless::StateMachine # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html
Properties:
DefinitionUri: hello_world.asl.json
Type: EXPRESS
Role: !GetAtt HelloWorldWorkflowRole.Arn
Tracing:
Enabled: True
Logging:
Destinations:
- CloudWatchLogsLogGroup:
LogGroupArn: !GetAtt HelloWorldWorkflowLogs.Arn
IncludeExecutionData: true # debugging
Level: 'ALL'
HelloUniverseWorkflow:
Type: AWS::Serverless::StateMachine # More info about State Machine Resource: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-statemachine.html
Properties:
DefinitionUri: hello_universe.asl.json
Type: EXPRESS
Role: !GetAtt HelloUniverseWorkflowRole.Arn
Tracing:
Enabled: True
Logging:
Destinations:
- CloudWatchLogsLogGroup:
LogGroupArn: !GetAtt HelloUniverseWorkflowLogs.Arn
IncludeExecutionData: true # debugging
Level: 'ALL'
##########################################################################
# Workflow Logs #
##########################################################################
MainWorkflowLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /stepfunctions/MainWorkflow
RetentionInDays: !Ref LogRetentionInDays
HelloWorldWorkflowLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /stepfunctions/HelloWorldWorkflow
RetentionInDays: !Ref LogRetentionInDays
HelloUniverseWorkflowLogs:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: /stepfunctions/HelloUniverseWorkflow
RetentionInDays: !Ref LogRetentionInDays
##########################################################################
# Roles #
##########################################################################
MainWorkflowRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: states.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: StartNestedWorkflows
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action: "states:StartSyncExecution"
Resource:
- !Ref HelloWorldWorkflow
- !Ref HelloUniverseWorkflow
- PolicyName: LogsPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogDelivery"
- "logs:GetLogDelivery"
- "logs:UpdateLogDelivery"
- "logs:DeleteLogDelivery"
- "logs:ListLogDeliveries"
- "logs:PutResourcePolicy"
- "logs:DescribeResourcePolicies"
- "logs:DescribeLogGroups"
Resource:
- !GetAtt MainWorkflowLogs.Arn
# Quick IAM reference: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_aws-services-that-work-with-iam.html
- PolicyName: MetricsTracingPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "cloudwatch:PutMetricData"
- "xray:PutTraceSegments"
- "xray:PutTelemetryRecords"
Resource: "*"
HelloWorldWorkflowRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: states.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: LogsPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogDelivery"
- "logs:GetLogDelivery"
- "logs:UpdateLogDelivery"
- "logs:DeleteLogDelivery"
- "logs:ListLogDeliveries"
- "logs:PutResourcePolicy"
- "logs:DescribeResourcePolicies"
- "logs:DescribeLogGroups"
Resource:
- !GetAtt HelloWorldWorkflowLogs.Arn
# Quick IAM reference: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_aws-services-that-work-with-iam.html
- PolicyName: MetricsTracingPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "cloudwatch:PutMetricData"
- "xray:PutTraceSegments"
- "xray:PutTelemetryRecords"
Resource: "*"
HelloUniverseWorkflowRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: states.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: LogsPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "logs:CreateLogDelivery"
- "logs:GetLogDelivery"
- "logs:UpdateLogDelivery"
- "logs:DeleteLogDelivery"
- "logs:ListLogDeliveries"
- "logs:PutResourcePolicy"
- "logs:DescribeResourcePolicies"
- "logs:DescribeLogGroups"
Resource:
- !GetAtt HelloUniverseWorkflowLogs.Arn
# Quick IAM reference: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_aws-services-that-work-with-iam.html
- PolicyName: MetricsTracingPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "cloudwatch:PutMetricData"
- "xray:PutTraceSegments"
- "xray:PutTelemetryRecords"
Resource: "*"
##########################################################################
# Outputs #
##########################################################################
Outputs:
MainWorkflowArn:
Description: "Main workflow ARN"
Value: !Ref MainWorkflow
HelloWorldWorkflowArn:
Description: "Hello world workflow ARN"
Value: !Ref HelloWorldWorkflow
HelloUniverseWorkflowArn:
Description: "Hello Universe workflow ARN"
Value: !Ref HelloUniverseWorkflow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment