In this project, we will show you how you can create a simple AWS Step Function that invokes 2 lambda functions accordingly. Here is a basic block diagram which explains it better:
Lists of services we are going to use:
- Create 2 Lambda Functions
- Creating an IAM role for Step Function to invoke Lambda Functions
- Creating a Step Function
Let’s start by first creating our Lambda functions where we will have some simple code where we will construct a response object.
Step 1: Create Lambda Functions
1: Go to “Lambda” from the search bar.
2: Click on “Create Function” on the upper right corner.
3: We will name them as “Skillcurb-ProcessPurchase” and “Skillcurb-ProcessRefund”
4: Click Create
5. Here in these 2 Lambda functions we will simple code as shown below:
Skillcurb-ProcessPurchase:
import json
import datetime
import urllib
import boto3
def lambda_handler(message, context):
# TODO implement
#Log input message
print("received message from step function")
print(message)
#Construct response object
response = {}
response['TransactionType'] = message['TransactionType']
response['Timestamp'] = datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
response['Message'] = "Hello from process purchase lambda"
return response
Skillcurb-ProcessRefund:
import json
import datetime
import urllib
import boto3
def lambda_handler(message, context):
# TODO implement
#Log input message
print("received messsage from step fn")
print(message)
#Construct response object
response = {}
response['TransactionType'] = message['TransactionType']
response['Timestamp'] = datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
response['Message'] = "Hello from process refund lambda"
return response
Step 2: Creating an IAM Role
1: Go to “IAM” from the search bar.
2: Go to “Roles” and click on “Create Role”
In the “Use Case” search for “Step Function” and select it and click Next.
3: In the “Permission policies” section you will be able to see the AWS Lambda Role policy attached to it. Click Next.
- Name your role, here we will name it as “StepFunctionsLambda”.
- Click on “Create Role”. You will be able to see your role created on the Roles page.
Step 3: Create a Step Function
- Type “Step Functions” from the search bar.
- Click on “Create state machine”.
- We will be creating the step function with our own code so you need to define that by selecting “write your workflow in code. Since we want to write our “Amazon State Language” which is a structured language used to define your state machine, a collection of states that can do work (Task states), determine which states to transition to next (Choice states), stop an execution with an error (Fail states).
- In the definition,we will provide the code to invoke the lambda functions. Make sure to provide your “ARN” to a particular response. You can get the information from the Lambda function. On the left hand side you can see the data flow of our state machine.
Our code snippet:
{
"Comment": "A simple AWS Step Functions state machine invokes Lambda Functions.",
"StartAt": "ProcessTransaction",
"States": {
"ProcessTransaction": {
"Type" : "Choice",
"Choices": [
{
"Variable": "$.TransactionType",
"StringEquals": "PURCHASE",
"Next": "ProcessPurchase"
},
{
"Variable": "$.TransactionType",
"StringEquals": "REFUND",
"Next": "ProcessRefund"
}
]
},
"ProcessRefund": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
},
"ProcessPurchase": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
}
}
}
- Click Next.
- Name your state machine. Here we named the default name which is “MyStateMachine”.
- In the Permissions section, choose an existing role which we have created earlier namely “StepFunctionsLambda”
- Click on “Create State Machine”.
- Next, click on “Start Execution” and here type in the name as “PurchaseType” and in the Input type: “TransactionType”: “Purchase” and then click on start execution.
- Here, you will be able to see the graph inspector and you will be able to see the step input and step output of each function.
- Since our Input event was purchased, the process transaction was process purchase. The output will be:
You can try another execution with the Transaction Type as Refund, you will be able to see that the process transaction will be Process Refund.
You can use AWS Step Functions using other Amazon services as well like Amazon DynamoDB, S3 bucket and others. Please leave a comment if you think that this project was helpful or if you have any questions.
SkillCurb Youtube Channel:
Please visit us on our youtube channel skillcurb and watch the hands on Lab for this project.