Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Step Functions example state machine - retrying by stoppedReason of ECS task
{
"Version": "1.0",
"Comment": "Run AWS Fargate task",
"TimeoutSeconds": 900,
"StartAt": "Run Fargate Task",
"States": {
"Run Fargate Task": {
"Type": "Task",
"Resource": "arn:aws:states:::ecs:runTask.sync",
"Parameters": {
"LaunchType": "FARGATE",
"Cluster": "<Cluster-ARN>",
"TaskDefinition": "<TaskDef-ARN>",
"Group.$": "$$.Execution.Name",
"NetworkConfiguration": {
"AwsvpcConfiguration": {
"Subnets": [
"<Subnet-1>",
"<Subnet-2>",
"<Subnet-3>"
],
"AssignPublicIp": "ENABLED",
"SecurityGroups": [
"<SecurityGroup-ID>"
]
}
},
"Overrides": {
"ContainerOverrides": [
{
"Name": "<Container-Name>",
"Environment": [
{
"Name": "<Environment-Variable-Name-To-Override>",
"Value": "<Environment-Variable-Value-To-Override>"
}
]
}
]
}
},
"End": true,
"Catch": [
{
"ErrorEquals": [
"States.TaskFailed"
],
"Next": "Cause to Json"
}
]
},
"Cause to Json": {
"Type": "Pass",
"Parameters": {
"Cause.$": "States.StringToJson($.Cause)"
},
"Next": "Retry or Finish"
},
"Retry or Finish": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.Cause.StoppedReason",
"StringMatches": "ResourceInitializationError: *",
"Next": "Run Fargate Task"
}
],
"Default": "Fail"
},
"Fail": {
"Type": "Fail"
}
}
}
@toricls
Copy link
Author

toricls commented Jun 18, 2021

Pros

Cons

  • Cannot use the "Retry" field of "Task" state, so
    • Need to implement an "Exponential back-off" logic
    • Need to implement to limit the maximum number of retries (so that I used the "TimeoutSeconds" field in the top-level of the state machine above to limit it somehow)

@toricls
Copy link
Author

toricls commented Jun 18, 2021

Note: On Fargate, the "ResourceInitializationError" error used in the example above only occurs on platform version 1.4.0 or later

@toricls
Copy link
Author

toricls commented Jun 18, 2021

This is the visualized version of the example state machine above :)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment