I want to invoke an AWS Lambda function whenever a state (execution event) changes in AWS Step Functions. How can I do that?

Use Amazon CloudWatch Events to trigger a Lambda function that responds to an API call event change from your Step Functions state machine. Follow these steps to set up this configuration:

1.    Create a Step Functions state machine.

2.    Create a Lambda function.

3.    Create a CloudWatch Event that watches the Step Functions event pattern and invokes the Lambda function. 

Before you begin this procedure, you must:

  • Confirm that the event change that you want to trigger the Lambda function is a supported API action.
  • Create an AWS Identity and Access Management (IAM) role that grants the Step Function state machine the permission to perform any required actions. For example, if you want your state machine to invoke a Lambda function, then you must create an IAM role that includes the permission to invoke a function.
  • Verify that your AWS Management Console is set to the correct AWS Region. Your Step Functions state machine, Lambda function, and CloudWatch Event must all be in the same AWS Region.

Create an AWS Step Functions state machine

1.    Open the AWS Step Functions console.

2.    Choose Create state machine.

3.    To define a custom state machine, choose Author from scratch. To use an available template, choose Templates. For an example template, choose Hello world.

4.    For Name, enter a name for your state machine.

5.    For IAM role, select the IAM role that you created for your state machine.

6.    Choose Create state machine.

Create an AWS Lambda function

1.    Open the AWS Lambda console.

2.    Choose Create function.

3.    To create a custom Lambda function, choose Author from scratch. To use an available template, choose Blueprints, select a template, and then choose Configure.

Note: For an example template, choose hello-world-python3.

4.    For Name, enter a name for your function.

5.    If the Runtime option is available, select the runtime for your function.

6.    For Role, choose an existing role or create a role that allows the Lambda function to execute.

Note: For this tutorial, the role must allow the Lambda function to create CloudWatch Logs. For an example, you can choose Create a custom role, then apply the default lambda_basic_execution IAM role. If your Lambda function needs to access other AWS resources, you must add the corresponding permissions.

7.    Choose Create function.

After the function is created, you can update the function code using the AWS Lambda console. For your Lambda function's Code entry type, select Edit code inline to modify the code directly in the console, or select Upload a .ZIP file to upload a deployment package.

To complete this tutorial, follow these steps to modify the function code:

1.    Open the function that you created in the AWS Lambda console.

2.    Under Function code, for Code entry type, select Edit code inline.

3.    Replace the function code with the following: 

import json
def lambda_handler(event, context):
    print("Received event: " + json.dumps(event))

4.    Choose Save.

Create a CloudWatch Event that watches the Step Functions event pattern and invokes the Lambda function

1.    Open the Amazon CloudWatch console.

2.    In the navigation pane, choose Events.

3.    Choose Create rule.

4.    For Event Source, select Event Pattern.

5.    For Service Name, select Step Functions.

6.    For Event Type, choose either All Events to watch all events, or AWS API Call via CloudTrail to customize which events to watch.

7.    Choose Add target*. Confirm that Lambda function is selected in the target drop-down menu.

8.    For Function*, choose the Lambda function that you created.

9.    Choose Configure details.

10.    For Name, type a name for your CloudWatch Event.

11.    For State, verify that Enabled is selected.

12.    Choose Create rule.

Verify that your setup is working as expected

1.    Open the AWS Step Functions console, then choose the state machine that you created.

2.    Choose Start execution.

3.    Open the Amazon CloudWatch console, then choose Logs from the navigation pane.

4.    Open the log stream generated by the Lambda function.

5.    Verify the event details in the log stream.

Note: It might take one to two minutes after you start execution on the state machine for the log stream to appear.  


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center.

Published: 2018-10-24