Is there a way in AWS Step functions to define a timeout for the whole function using the TimeoutSeconds
field, but still take custom action (trigger a lambda) when the step function level timeout is reached ?
Here is a sample function definition,
{
"Comment": "An example of the Amazon States Language for fanning out AWS Batch job",
"StartAt": "Wait 60 secs",
"TimeoutSeconds": 10,
"States": {
"Wait 60 secs": {
"Type": "Task",
.
.
.
"End": true,
"Catch": [
{
"ErrorEquals": [
"States.All"
],
"Next": "Notify failure"
}
]
},
"Notify failure": {
"Type": "Task",
\\ Push message to an sns topic
}
}
}
I want a way to call the Notify failure
step, when the step function level time out is reached.
This is probably best handled by using the Execution Status Change Events that AWS Step Functions Workflows will emit to EventBridge. You can author Rules to match events like the following, then have EventBridge send to your SNS topic or invoke a Lambda function.
{
"version": "0",
"id": "315c1398-40ff-a850-213b-158f73e60175",
"detail-type": "Step Functions Execution Status Change",
"source": "aws.states",
"account": "012345678912",
"time": "2019-02-26T19:42:21Z",
"region": "us-east-1",
"resources": [
"arn:aws:states:us-east-1:012345678912:execution:state-machine-name:execution-name"
],
"detail": {
"executionArn": "arn:aws:states:us-east-1:012345678912:execution:state-machine-name:execution-name",
"stateMachineArn": "arn:aws:states:us-east-1:012345678912:stateMachine:state-machine",
"name": "execution-name",
"status": "TIMED_OUT",
"startDate": 1551224926156,
"stopDate": 1551224927157,
"input": "{}",
"inputDetails": {
"included": true
},
"output": null,
"outputDetails": null
}
}
Alternatively, you could break this into two state machines, with the core logic (and timeout) in one that you invoke from an "outer" state machine using the Step Functions integration with itself (aka "nesting"). And put the compensating logic in that outer workflow.