How to Invoke the Step Function from Lambda
Introduction
In this article, we will talk about how to use invoke AWS Step Function and run a server-less workflow from AWS Lambda Functions.
AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you.
AWS Step Functions is a low-code, visual workflow service that developers use to build distributed applications, automate IT and business processes, and build data and machine learning pipelines using AWS services.
Prerequisites
The IAM role you will be using should have access to create Step Function and Lambda. Also, it should have access to trigger Step Function from Lambda. If you don’t have one, create a new IAM role.
AWS Lambda
AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. Lambda can be triggered from over 200 AWS services and software-as-a-service (SaaS) applications.
Whenever code is deployed within AWS Lambda it creates an executable package that is executed whenever the Lambda function is invoked. In this case, Boto3 Library was imported to trigger Step Function.
The output of the lambda function will be used as input for the step function. Based on the use case one can change the input, modification of this input is recommended.
import boto3
import jsonsf = boto3.client('stepfunction', region_name = 'us-east-1')input_dict = {'key': 'value'}response = sf.start_execution(
stateMachineArn = 'arn:aws:states:us-east-1:xxxxxxx:iam_role',
input = json.dumps(input_dict))
Step Function
Step Functions manages state, checkpoints, and restarts for you to make sure that your workflows run in order and as expected. Built-in try/catch, retry, and rollback capabilities deal with errors and exceptions automatically based on your defined business logic.
In our scenario, the input which we will be receiving from Lambda should be in the form of JSON. In the Amazon States Language, a path is a string beginning with $
that you can use to identify components within JSON text. It means that using the $ sign one can check for the respective component (Or Variable).
Step Function Code for reference:
{
"Comment": "StepFunction WorkFlow",
"StartAt": "First State",
"States": {
"First State": {
"Type" : "Choice",
"Choices": [
{
"Variable" : "$.Key",
"Ispresent" : true,
"Next": "As per your requirement make the changes"
}
]
}
}
}
In the above code, we are reading input received from Lambda by using the $ sign, make sure whatever Key you are passing from Lambda, the same key should be used in Step Function. In Step Function we have different “Types” available like Choice (used in the above example), Task, Parallel, etc.
Once the State machine code is complete, the workflow of your code will be available like:
With the help of workflow, one can visualize whole state machine code (JSON) more easily.
Conclusion
Both Lambda Function and Step Function are used for Orchestration purposes. This understanding is an essential step while designing serverless-based architectures using AWS Lambda and AWS Step Function. One can integrate multiple services of AWS like SQS, SNS, Glue, S3, etc with Lambda and Step Function and can automate their architecture.
In this particular blog, we have just learned how to invoke the step function from lambda, similarly one can invoke lambda from the step function or Glue Job from the lambda/Step Function. Based on the use case step function JSON code will be changed.
I hope this blog was helpful for all the Cloud Engineers working on AWS who are facing such issues and looking for a permanent solution.
Just try this out, and ping me if you have any questions:
You can DM me on Linkedin.
Till then Happy Learning!!
References
- https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create.html
- https://docs.aws.amazon.com/lambda/latest/dg/welcome.html
- https://docs.aws.amazon.com/step-functions/latest/dg/welcome.html
- https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/stepfunctions.html
- https://docs.aws.amazon.com/step-functions/latest/dg/how-step-functions-works.html
- https://docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-paths.html
- https://docs.aws.amazon.com/step-functions/latest/dg/batch-job-notification.html