Nesting multiple lambda functions in a single step function in AWS

Exemplifi
3 min readMay 8, 2024

--

Introduction

With technology growing at a rapid pace and IT systems becoming even more complex , automating workflows is crucial for enhancing efficiency. AWS offers powerful tools like AWS Lambda and AWS Step Functions to streamline and automate services. AWS Lambda executes code in response to events without managing servers, while AWS Step Functions orchestrates complex workflows. In this insight post we explain how to integrate multiple Lambda functions within a single Step Functions state machine, providing a practical approach to utilizing these AWS tools effectively.

1. Define Your Lambda Functions

First, define the specific tasks each Lambda function will handle. In our example, let’s assume we have three Lambda functions:

  • ProcessData: This function processes incoming data.
  • CheckQuality: This function checks the quality of processed data.
  • LogData: This function logs the results of the data processing.

Each function should be created in the AWS Lambda console or using the AWS CLI. Ensure each Lambda function has an appropriate execution role with permissions to access necessary AWS services (e.g., S3, DynamoDB) and is configured to be invoked by Step Functions.

2. Create an IAM Role for Step Functions

Create an IAM role that your Step Functions state machine will use. This role needs to have policies allowing it to invoke your Lambda functions.

  • IAM Console: Navigate to IAM and create a new role. Select AWS service as the trusted entity and choose Step Functions.
  • Attach Policies: Attach the AWSLambdaRole policy, or create a custom policy with permissions like lambda:InvokeFunction that targets your specific Lambda functions.

3. Define the Step Functions State Machine

Design a state machine in Amazon States Language (ASL) that orchestrates how your Lambda functions are invoked. Let’s define a workflow that processes data, checks its quality, and then logs the results. We’ll also add error handling and a choice state that branches based on the quality check.

4. Write the ASL Definition

Here is an example of an ASL definition for a state machine:‍

5. Deploy the State Machine

Deploy your state machine:

  • AWS Management Console: Use the console to paste your ASL definition and create the state machine.
  • AWS CLI: Use the CLI to deploy using a command like aws stepfunctions create-state-machine — definition file://myStateMachine.json — role-arn arn:aws:iam::123456789012:role/MyStepFunctionsRole.

6. Monitoring and Logging

Set up logging:

Testing the State Machine

After deploying, test the state machine to ensure it works as expected:

  • Trigger the state machine with test data.
  • Monitor the execution in the Step Functions console to see if it executes as planned.
  • Review logs in CloudWatch if any issues occur.

Conclusion

This example provides a basic yet robust template for orchestrating multiple Lambda functions with AWS Step Functions, including branching logic and error handling. Customize it according to your specific business logic and AWS environment. By following these steps developers can help automate and scale their process to achieve greater efficiencies in their workflows. If you enjoyed this post, please follow us LinkedIn, Facebook and X for more.‍

Originally published at https://www.exemplifi.io.

--

--

Exemplifi

We build secure, accessible and functional enterprise and government websites