“A step function can be used to control the AWS services using the amazon state language. AWS step function is also a serverless service, just like the lambda functions, but it can be used to perform multiple tasks in a sequence or in parallel. The step function is based on the state machine, and the task and the state machine are just the workflows of your application. A step function can be used to perform various tasks by invoking the lambda function, depending on your use case. In this blog, we will study how to invoke the lambda function with the step function.”
Creating Lambda Function
The first thing you need to do is to create a lambda function that will be invoked by the step function. In this blog, we will use the basic Hello from lambda code provided by AWS in order to invoke the lambda function with the step function.
The Hello World lambda function has the following code, which just returns static content when invoked.
def lambda_handler(event, context):
# TODO implement
'body': json.dumps('Hello from Lambda!')
Creating IAM Role for Step Function
In order to invoke the lambda function using the Step Function, first, you need to provide the step function’s necessary permissions. For this, you need to create an IAM role for the step function, which grants the step function permissions to invoke the lambda function. In order to create the IAM role, follow the given below steps.
- Go to your IAM dashboard
- On the left corner of your console, click on the Roles button
- Click on the create role button
- Select AWS services as the trusted entity type
- Under the use case option, select the Step function
- Then click on the next button
- In the add permission window, it will automatically select the required IAM policy. You just need to click on the next button
- Provide the role name whatever you want, and click on the create role button to create the role
Creating Step Function
After creating the IAM role for the step function, now create a step function that will be used to invoke the lambda function. For this, first, go to the step function console and click on the create state machine button to start creating the state machine. AWS provides the following two ways to create the state machine, and we will discuss both methods to create the state machine in this blog.
- Design your workflow visually
- Write your workflow in code (amazon states language)
Design Your Workflow Visually
This method is new and far easier than writing your workflow in code because you do not need to write any code. AWS provides the visual block for your workflow, and you just need to drag them and define their parameters. AWS automatically creates the ASL code for your workflow. The following steps define how we can create a step function visually.
- Go to your step function dashboard from the AWS management console
- Click on the create state machine button from the console
- Under the choose authoring method, select design your workflow visually
- Under the type section, select the standard option
- Click on the Next button
- Now, you will get the window where we will design the workflow for our state machine visually
- On your left side, it will show the AWS Lambda Invoke option. Click and drag it to the center of the console where it is written; drag the first state here
- On the right side of the window, you can see the configuration of the lambda invoke
- In the state name, type lambda invoke
- In the integration type section, choose the optimized
- Next, move to the API parameter section
- Under the function name, click on the choose an option tab, where you will find the lambda function which you want to invoke
- Under the payload option, choose to use the input state as payload.
- Under Next state, select the go to the end option
- Click on the next button in the upper right corner
- In the review generated code window, you can see that AWS automatically generates the state machine amazon state language definition. Also, generate the workflow of your step function
- Click on the Next button
- Now you will get the specify state machine setting window
- Provide a state machine name, whatever you desire
- Under the permissions, click on the choose an existing role checkbox
- Select the IAM role that we created in the previous step
- Scroll down to the end of the page and click on the create state machine button to create the state machine
Write Your Workflow in Amazon States Language
The second way to create the state machine is to write your workflow in code. You will write the workflow in amazon states language (ASL). While creating the state machine, choose to write your workflow in code in the define state machine section. You can use the following amazon states language code in order to invoke the lambda function.
In the above definition, you will use the ARN of your lambda function in the resource field. You can get the lambda function ARN from the lambda console.
Invoke the Lambda Function
After creating the state machine, you need to start the execution. Click on the step function, and it will show all the configurations of the step function there. In order to start the execution of the step function, click on the start execution button.
You can provide the name and the input to the execution you are going to start, but it is optional.
Under execution status, you can check the status of the step function invoking the lambda function. Go to the graph inspector and click on the Lambda Invoke, and on the right corner of the console, you will get the detail, input, and output tab. Click on the input tab, and you can see the input of the execution. Now click on the output tab, and you will see the output of the step function. That’s how your lambda function is invoked with the step function.
In this blog, we have studied how to invoke the lambda function with the step function. Both the step function and lambda function are serverless resources by AWS. A step function can also be used to control the other services of AWS like SNS, S3, and SNS, etc. We have learned the different ways to create state machines, i.e., designing your workflow visually and designing the workflow using the code. Designing the workflow visually is a new and easy way to get started with the AWS step functions.