How to Start an AWS Step Function Workflow From Lambda | STEP BY STEP GUIDE

Be A Better Dev
16 Dec 201909:13

Summary

TLDRThis AWS tutorial demonstrates how to initiate a Step Function from a Lambda function. The video guides viewers through creating a Step Function instance and a Lambda function in Python, setting the necessary permissions, and writing code to interact with Step Functions. It showcases the process of defining a unique transaction ID using the UUID library, crafting a JSON input, and invoking the Step Function's start execution API. The tutorial concludes with testing the Lambda function and verifying successful Step Function instances.

Takeaways

  • 😀 The video demonstrates how to start a Step Function instance from a Lambda function in AWS.
  • 🔗 The presenter has already created a Step Function instance and predefined some AWS Step Language (ASL) code for a simple task involving an SQS queue.
  • 📝 The ASL code takes an input and communicates with the SQS 'SendMessage' API, posting a JSON object with a transaction ID and type derived from the Step Function input.
  • 🆔 The Step Function input includes a unique transaction ID and a type, both of which are strings.
  • 👉 The presenter copies the Amazon Resource Name (ARN) of the Step Function for use in the Lambda function creation process.
  • 🤖 The Lambda function named 'step function invoker' is created in Python 3.6 or 3.7, with permissions set to access the Step Function.
  • 🔑 It's important to use the least privilege model for permissions, but the presenter uses full access for simplicity in this tutorial.
  • 💾 The Lambda function's code uses the 'boto3' library to interact with AWS services, specifically the Step Functions client.
  • 🔄 A unique transaction ID is generated using the 'UUID' library to ensure the Step Function instance has a unique key.
  • 📑 The input for the Step Function is defined as a JSON object, with the transaction ID and type hardcoded as 'purchase' in the example.
  • 🚀 The 'start_execution' API of the Step Functions client is called with the state machine ARN, a unique name (transaction ID), and the JSON payload as parameters.

Q & A

  • What is the main topic of the video?

    -The main topic of the video is demonstrating how to start a Step Function instance from a Lambda function in AWS.

  • What is ASL in the context of the video?

    -ASL in this context refers to Amazon States Language, which is used to define the state machine for Step Functions in AWS.

  • What is the purpose of the SQS SendMessage API in the Step Function?

    -The SQS SendMessage API is used in the Step Function to post a JSON object containing a transaction ID and type to an SQS queue.

  • What are the inputs required for the Step Function in the script?

    -The Step Function requires an input containing a transaction ID and a type, both of which are strings.

  • What is the ARN used for in the Lambda function?

    -The ARN (Amazon Resource Name) is used to reference the Step Function that the Lambda function will interact with.

  • Why is it important to use the least privilege model when creating an IAM role for the Lambda function?

    -Using the least privilege model ensures that the Lambda function has only the permissions it needs to perform its tasks, enhancing security by minimizing potential access to sensitive resources.

  • What is the role of the UUID library in the Lambda function code?

    -The UUID library is used to generate a unique transaction ID for each Step Function instance, ensuring that each instance has a unique identifier.

  • How does the Lambda function interact with the Step Functions client?

    -The Lambda function uses the boto3 library to create an instance of the Step Functions client and then calls the start_execution API to start a Step Function instance.

  • What is the significance of the 'name' field in the start_execution API call?

    -The 'name' field serves as the unique identifier for the Step Function instance, allowing AWS to distinguish between different instances.

  • How does the Lambda function handle the input for the Step Function?

    -The Lambda function constructs a JSON object with the required transaction ID and type, converts it to a string using the JSON library, and passes it as the input to the start_execution API call.

  • How can you verify that the Lambda function has successfully started the Step Function instances?

    -You can verify the success by checking the logs in the Lambda function and by looking at the Step Functions console to see the instances and their progress.

Outlines

00:00

🚀 Starting a Step Function from Lambda

This paragraph introduces the video's objective to demonstrate initiating a Step Function instance from within a Lambda function. The presenter has already set up a Step Function instance and predefined ASL code for a task that interacts with the SQS 'send message' API. The input for the Step Function consists of a transaction ID and a type, both of which are strings. The presenter emphasizes the importance of using unique identifiers for Step Function instances and proceeds to show how to create a Lambda function with the necessary permissions to start the Step Function.

05:01

🛠 Creating and Testing the Lambda Function

The second paragraph details the process of creating a Lambda function named 'step function invoker' in Python 3.6 or 3.7. The function requires specific permissions for accessing the Step Function service, and the presenter advises using the least privilege model. The Lambda function's code is written in an external editor, with the presenter explaining the need for libraries such as 'json', 'moto3', and 'uuid'. The code generates a unique transaction ID using the 'uuid' library and constructs a JSON input object for the Step Function. The Lambda function then uses the 'boto3' Step Function client to start the execution of the Step Function, passing the state machine ARN, a unique name, and the JSON payload. The paragraph concludes with instructions on testing the Lambda function by creating a test event and verifying successful execution in the Step Functions console.

Mindmap

Keywords

💡AWS

AWS stands for Amazon Web Services, which is a comprehensive cloud computing platform provided by Amazon. It offers a wide range of services including computing power, storage, databases, and more. In the context of the video, AWS is the platform where the user is demonstrating the integration of various services like Lambda and Step Functions.

💡Lambda

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. It automatically scales your application by running code in response to events. In the video, the user is showing how to integrate Lambda functions with Step Functions for event orchestration.

💡Step Functions

AWS Step Functions is a service that enables you to coordinate multiple AWS services into serverless workflows so you can build and update apps quickly. The video is focused on demonstrating the process of starting a Step Function instance from a Lambda function.

💡Event Orchestration

Event orchestration refers to the process of managing the flow of events in a system to ensure that they are processed in a specific order or manner. In the script, the user explains how to use Lambda to orchestrate events within Step Functions for a more automated workflow.

💡ASL

ASL stands for Amazon States Language, which is a JSON-based language specific to AWS Step Functions that lets you define your serverless workflows. The script mentions predefined ASL as part of the setup for the Step Function.

💡SQS

SQS stands for Amazon Simple Queue Service, a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. The video discusses using SQS to send messages as part of the Step Function's task.

💡JSON

JSON is a lightweight data interchange format that is easy for humans to read and write and for machines to parse and generate. In the video, JSON is used to structure the input for the Step Function, containing a transaction ID and a type.

💡IAM Role

An IAM Role in AWS is an AWS identity that you can use to permit the AWS Services to access resources securely. The script mentions using an existing IAM role with Step Function access permissions for the Lambda function.

💡UUID

UUID stands for Universally Unique Identifier, which is a 128-bit number used to uniquely identify some object or entity. In the script, the UUID library is used to generate a unique transaction ID for the Step Function instance.

💡Start Execution API

The Start Execution API is a method provided by AWS Step Functions to initiate the execution of a state machine. The video demonstrates using this API within a Lambda function to start a Step Function instance with specific input.

💡Moto3

Moto3 is a library that allows you to use boto3, the Amazon Web Services (AWS) SDK for Python, to create mock AWS services for testing without actually making API calls to AWS. The script mentions using the Moto3 library, presumably for testing the Lambda function's interaction with AWS services.

Highlights

Introduction to integrating AWS Lambda with Step Functions for event orchestration.

Demonstration of starting a Step Function instance from a Lambda function.

Creation of a Step Function instance named 'step function from lambda' with predefined ASL code.

Explanation of the Step Function's task to communicate with the SQS Send Message API.

Details on the JSON object structure for the transaction ID and type from the Step Function input.

Copying the ARN for use in creating the Lambda function to start the Step Function instance.

Creating a new Lambda function named 'step function invoker' in Python 3.6.

Importance of using the correct permissions for the Lambda function to access Step Functions.

Using the least privilege model to assign only necessary permissions for the Lambda function.

Writing code in the Lambda function to interact with Step Functions using the boto3 client.

Generating a unique transaction ID using the UUID library for the Step Function instance.

Defining the input object for the Step Function with a hardcoded 'purchase' type.

Calling the Step Function's start_execution API with the necessary parameters.

Conversion of the Python dictionary object to a JSON string for the API call.

Testing the Lambda function with a test event to ensure successful Step Function execution.

Verification of multiple successful Step Function instances in the AWS console.

Exploration of individual Step Function instances to track progress and success.

Encouragement to check out additional videos on Step Functions and related AWS services.

Transcripts

play00:00

hello everyone and welcome back to AWS

play00:02

simplified in my previous videos I've

play00:04

shown you how to integrate lambdas into

play00:06

step functions as part of event

play00:08

orchestration but in this video I want

play00:10

to show you how to start a step function

play00:12

instance from a lambda so let's jump

play00:14

right into it so here we are in the step

play00:17

function console so I've actually

play00:18

already created a step function instance

play00:20

and it's called step function from

play00:22

lambda and I predefined some ASL also

play00:26

some step function code that does a

play00:28

fairly trivial task so let's just take a

play00:30

look at this really quick so it takes an

play00:33

input and it's going to be communicating

play00:35

with the sqs send message API and here

play00:39

is the URL to the Q that I've created in

play00:41

advance of this video and the body that

play00:44

it's going to be posting to this sq bus

play00:45

it's going to be a JSON object that has

play00:48

a transaction ID and the value is going

play00:50

to be coming from the input of the step

play00:52

function and the type same deal so

play00:55

there's a type key and the value is

play00:57

going to be coming from the input so

play00:58

that's to say our step function input is

play01:01

going to contain a transaction ID which

play01:03

is a string and a type which is also a

play01:05

string okay so it's a very

play01:06

straightforward application here so this

play01:09

all looks well and good I'm going to

play01:11

copy the AR n because we're gonna need

play01:13

it when we create our lambda function

play01:15

and actually try to start an instance of

play01:17

the step function so this is all fine

play01:19

here now let's go over to lambda and

play01:21

actually create our lambda function

play01:23

that's going to do this okay going to

play01:27

click on create function now we're going

play01:30

to be doing this from scratch get a name

play01:33

my function names step function invoker

play01:38

and we're going to be doing this in

play01:41

Python 3.6 3.7 rather and then this is

play01:48

actually quite important the permission

play01:50

section so let's scroll down here so I'm

play01:52

going to be using on an existing role

play01:54

that actually has the step function

play01:56

access associated with it however if you

play01:59

are doing this from scratch you need to

play02:01

go out and create a role that has the

play02:03

permission that you need I'm using full

play02:05

access but I encourage you to use lease

play02:07

privilege models so only use the

play02:09

permissions that you need for this

play02:10

exercise so you actually need the

play02:13

functions start execution policy and you

play02:16

need to associate that with a role so

play02:18

I've already done that here except that

play02:19

I'm using the full access policy so I

play02:21

get all those permissions for free so

play02:24

after that's done you can go ahead and

play02:25

click on create a function in the bottom

play02:27

right here you can sometimes take a bit

play02:29

that was pretty quick that's great okay

play02:32

so that was created successfully so

play02:34

let's scroll down here so now we can

play02:35

actually write our code to interact with

play02:37

step functions so you can do this in the

play02:40

editor if you choose I'm gonna do it in

play02:41

sublime just because I'm a little bit

play02:43

more comfortable there so let's switch

play02:44

over to sublime the first step I want to

play02:47

do here is just paste in the AR n that

play02:49

corresponds with the step function that

play02:51

we created in the previous step and

play02:53

we're gonna need that in a moment or so

play02:54

when we interact with the client library

play02:56

that needs an AR n that's an input and

play02:59

then let's just take whatever we have

play03:01

here as a templates I get rid of the

play03:05

stuff we don't need

play03:07

alright perfect okay so let's get to it

play03:09

so we're gonna need the JSON library so

play03:12

that's so let's leave that as it is I'm

play03:14

gonna need the moto3 library and we're

play03:16

also going to need the UUID library so

play03:19

we're gonna be using a function from

play03:20

this library that generates a random

play03:22

hash of values and this will make sense

play03:24

in a moment it's actually the first step

play03:25

here so I'll probably have to get into

play03:26

it right away and then the other thing

play03:29

that we need is an instance of the boat

play03:31

of three step function client so we need

play03:34

to say client is equal to boat o 3 dot

play03:36

clients then you say step functions so

play03:41

now we have a reference to that client

play03:42

ok so the first thing that we need is

play03:46

the transaction ID but before we do that

play03:48

let's just recall what the input to this

play03:51

step function is going to look like so

play03:52

the input is going to be a JSON that

play03:55

looks like this so we're gonna have

play03:57

transaction ID and then something excuse

play04:02

me there we go foo and then we have type

play04:06

and that can be either purchase or

play04:09

refund we're just gonna hard code this

play04:11

is purchase here just in this example so

play04:13

just a reminder that's what our input is

play04:14

going to look like ok so the first thing

play04:16

we need to do is actually generate a

play04:17

value that corresponds with the

play04:19

transaction ID and this can be anything

play04:21

you want to be a random string of values

play04:23

it could be something meaningful like a

play04:25

customer ID plus a date

play04:27

it could be absolutely anything the only

play04:29

condition though is that this value

play04:31

whatever you're using as the key for the

play04:33

step function instance needs to be

play04:35

unique you cannot have two step function

play04:38

instances that correspond with the same

play04:40

key value now since we're using

play04:42

transaction ID is the kind of identifier

play04:44

in this example it makes sense to have a

play04:47

unique value here so let's actually do

play04:49

that right now through the UUID library

play04:51

so let's say our transaction ID and

play04:53

we're going to store this in a variable

play04:54

is equal to uu ID dot u u ID 1 and what

play05:00

this thing will generate is just a

play05:01

random string of characters separated by

play05:04

dashes so it'll look like like something

play05:06

like this and many many more of these

play05:10

variables I think it's limited to 4

play05:13

delimiter so probably 30 or so

play05:14

characters so that's what it's gonna

play05:17

look like so we need to actually convert

play05:19

this to string since UUID 1 actually

play05:21

returns an object so now we have a

play05:23

string instance of that so this is going

play05:25

to be the value that we populate into

play05:27

the body of the step function it's also

play05:29

going to be the name field or the

play05:31

identifier of the step function instance

play05:33

as well okay so let's proceed to the

play05:35

next step and actually define our input

play05:37

object so let's say our input is equal

play05:40

to a json and it's gonna look similar to

play05:42

what i have above here on line 10 and

play05:44

let's say the transaction ID is equal to

play05:50

what we just defined so that's going to

play05:51

be that UUID and we're gonna say the

play05:54

type surrounded in quotes of course and

play05:58

we're gonna say purchase okay so now we

play06:00

have a JSON sorry dictionary object in

play06:03

Python that corresponds to this object

play06:06

here that we're looking at okay so next

play06:09

we need to actually call the step

play06:11

function API it's called the start

play06:13

execution API and we can store the

play06:15

result in a response object if we want

play06:17

but we can say response is equal to

play06:20

client dot start

play06:22

underscore execution and we have a

play06:26

couple things that we need to pass in

play06:28

here okay so the first one that's

play06:29

absolutely mandatory state machine AR N

play06:31

and you can see now why I copied this

play06:35

from the previous step because we need

play06:36

to paste this in here so let's do that

play06:38

all right now based on

play06:40

make sure you put a comma at the end

play06:41

here let's just Center this guy all

play06:44

right cool

play06:45

and the second field that you need is

play06:47

the name so this is the unique

play06:49

identifier of this step function

play06:51

instance so it's going to be transaction

play06:53

ID which is the same value that we're

play06:55

populating in the JSON payload and the

play06:59

third value is the actual JSON payload

play07:01

that you want to pass in and this can be

play07:03

used throughout your step function for

play07:04

orchestration or to identify certain key

play07:07

values to determine what you want to do

play07:09

in this step function so for our

play07:11

purposes is this JSON object for yours

play07:13

it can be anything you like so let's say

play07:15

input is equal to a now there's a key

play07:17

step here recall that this is a Python

play07:19

dictionary object this API requires a

play07:21

string so we need to string Phi and

play07:23

convert this dictionary object into a

play07:25

JSON and so the way we do that is just

play07:28

use the JSON library JSON dump s and

play07:31

pass in that object the input object was

play07:34

which corresponds to the dictionary

play07:35

object we created previously okay so

play07:39

everything looks good here let's copy

play07:41

this out you can go back to our staff

play07:43

function he's not in click on save in

play07:47

the top right in order to test it out we

play07:49

need to create a test event you can put

play07:52

whatever you want in the event name

play07:53

leave this as default we're not reading

play07:55

off the input so it doesn't matter if

play07:56

you were reading off the input in your

play07:58

lab well you may want to change this but

play07:59

we don't care so we don't need it

play08:01

clicking on create in the bottom right

play08:03

okay and let's click on test now to see

play08:05

if this worked okay you can see that

play08:08

it's exceeded a whole bunch of jargony

play08:10

we don't care about let's click this a

play08:12

few times test plus whatever how many

play08:14

times you want okay so it looks like

play08:16

it's succeeding from the logs here let's

play08:18

head over to step functions and actually

play08:20

verify that there's multiple instances

play08:22

so we can see eight instances have been

play08:24

successful if we actually click into

play08:26

here now now we can look at the separate

play08:28

instances that correspond to each click

play08:31

of the button on that lambda function so

play08:33

these values all kind of our first

play08:35

glance may look at the same but you can

play08:36

see here ad 580 0 so these are all

play08:38

unique values that have all their own

play08:40

unique key so if we click on one of

play08:43

these we can see what the step function

play08:44

was doing and its progress so you can

play08:47

see through the green box here that it

play08:49

was successful at this step and you can

play08:51

see here

play08:53

you got a 200 okay when we were

play08:54

communicating with the SQS sendmessage

play08:57

api sofa lee found this video useful I

play08:59

have quite a few videos on step

play09:01

functions and set function orchestration

play09:03

including dynamo and SQS and us and us

play09:05

how they suggest to check those out I'll

play09:07

put a playlist in the pop up that's

play09:09

coming up in a second thanks so much

play09:11

guys and I'll see you next time

Rate This

5.0 / 5 (0 votes)

Related Tags
AWSLambdaStep FunctionsEvent OrchestrationCloud ComputingPythonAPI IntegrationSQSUnique IdentifiersCode Tutorial