Steps To Create AWS Lambda Functions
Step 1: Create the Lambda Functions
We will create the lambda functions which will be used to connect to our state machine which will be created further. Here we will have 4 Lambda functions in this example:
- add_numbers – It takes { key1 : X, key2 : Y} as input and returns { number : Z }
- greater_than – It returns { final_number : rand(500,1000) }
- less_than – It returns { final_number : rand(0,500) }
- final_state – It takes { final_number : C }
Step 2: Click on Create Function under Lambda Console. Select Author from Scratch, and next give add_numbers as the name of your function, under Runtime select Python from the dropdown (or any other preferred Runtime language of your choice). Then click on Create Function.
Step 3: When the function is created, hover down to the Code section and replace the Function Code with the following code and then click on Deploy.
Python3
import json def lambda_handler(event, context): number_1 = int (event[ 'key1' ]) number_2 = int (event[ 'key2' ]) return { "number" : abs (number_1 + number_2)} |
After this, we need to make 3 more Lambda Functions named as greater_than, less_than, and final_state using the above-mentioned steps.
Below are the three different codes for three different functions:
Greater_than
Python3
import json import random def lambda_handler(event,context): final_number = random.randint( 500 , 1000 ) return { "final_number" : final_number} |
Less_than
Python3
import json import random def lambda_handler(event,context): final_number = random.randint( 0 , 500 ) return { "final_number" : final_number} |
Final_state
Python3
import json import boto3 import subprocess import os import sys import subprocess # pip install custom package to /tmp/ and add to path subprocess.call( 'pip install inflect -t /tmp/ --no-cache-dir' .split(), stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL) sys.path.insert( 1 , '/tmp/' ) import inflect def lambda_handler(event,context): inflect_engine = inflect.engine() number = inflect_engine.number_to_words(event[ 'final_number' ]) message = { "DelMeMessage" : "The StepFunctions Result is %r" % number} client = boto3.client( 'sns' ) response = client.publish( TargetArn = "SNS-TOPIC-ARN" , Message = json.dumps({ 'default' : json.dumps(message)}), MessageStructure = 'json' ) |
After completing these steps, all our functions will be created successfully.
How To Use AWS Step Functions For Serverless Workflows?
AWS step functions process that calls the AddNumbers service, First, we will add input numbers, and then it will be decided whether the output number is greater than or less than the given constraints and the final state will be decided upon the results. While we could have one Lambda function call the other, we worry that managing all of those connections will become challenging as the AddNumbers application becomes more sophisticated. Plus, any change in the flow of the application will require changes in multiple places, and we could end up writing the same code over and over again.