AWS Step Functions — Are Active Executions Affected When Your State Machine Gets Updated?
Tags: aws, serverless, stepfunctions, lambda
Published: 2022-06-10 14:12:19

AWS Step Functions is a Serverless orchestration service that helps to build a workflow with various steps that connect different AWS services. These steps are defined in [State Machine](https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-state-machine-structure.html), which will be implemented in [Amazon States Language (ASL)](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html).

There are two types of workflows in Step Functions:

1. Express flow
2. Standard flow

[Among many differences](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html), Express flow can only run up to five minutes and Standard flow can run up to one year.

Since even five minutes is a long time, there is a possibility of having an active execution when the state machine is updated. In this article, I am going to discuss how such active executions are affected when the State Machine is updated and how to handle those scenarios.

---

Let’s assume we have a simple state machine with two Lambda functions as follows. There is a “Wait” state with a three-minute wait time in between Lambda calls.


![Two Lambda Functions with Wait State](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1pg2olr1waxcn20ijq4q.png)
_Fig 1: Two Lambda Functions with Wait State_

### Scenario 1
Here, we will run the above state machine, and while there is an active execution, we will update the state machine by adding a third Lambda function after the second Lambda function.


![State Machine updated with 3rd Lamba Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjb63h58woc4lr7okd1m.png)
_Fig 2 : State Machine updated with 3rd Lamba Function_

The result is that the active execution is not affected.

> The reason is when a new execution initialised, the Step Function keeps a ‘snapshot’ of the State Machine and any updates to the State Machine is not affect this ‘snapshot.’

### Scenario 2

Here, we will run the state machine in Fig 1, and this time we will keep the State Machine as it is, but, we will update the second Lambda function code.

The result is that the active execution is affected.

> The reason is although Step Function keeps a ‘snapshot’ of the state machine at the start of the execution, the Lambda functions in the state machine are referenced with their ARNs. This is the latest version of Lambda. So, if the Lambda functions are updated, it affects the running execution since it always refers to the latest version of Lambda.

#### How To Prevent Modifications To Active Executions When Lambda Functions Update?

There is a simple solution for this: **use Lambda versions.**

On deployment, create a Lambda version and in the state machine, refer to the Lambda function with the version in the ARN.

Example: _arn:aws:lambda:[region]:[accountId]:function:[functionName]:[Version]_

An example of this implementation is this CDK code snippet.

Previously (without Lambda versions):

{% embed https://gist.github.com/pubudusj/209b685f2f47e669a6ccc47f763d072b %}

Now (with Lambda versions):

{% embed https://gist.github.com/pubudusj/36df41c7b0d4cd6a838fd0708b8056a0 %}


#### Conclusion
When using Lambda functions within Step Functions, use the Lambda function versions to make sure any active executions are not affected by Lambda updates.

In contrast, if you need any active executions to be updated with the latest Lambda code, do not use Lambda function versions.

#### Resources
1. AWS Step Functions Express vs Standard workflows: [https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html)
2. Documentation — Amazon State Language:
[https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html](https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html)
3. Documentation — State Machine:
[https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-state-machine-structure.html](https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-state-machine-structure.html)