Create Lambda Step Functions in the AWS Console

Lukas Ruebbelke
InstructorLukas Ruebbelke

Share this video with your friends

Send Tweet
Published 6 months ago
Updated 5 months ago

Step functions are a chain of functions where each function consumes the output of the previous function as its input.

If you understand functions, then you understand Lambda functions. If you understand Lambda functions, then you know how to create Lambda step functions.

With this in mind, you will learn how to chain together your Lambda functions, without writing a single line of code, by designing the workflow visually within the AWS console.

Instructor: [0:00] In this lesson, I'm going to show you something incredibly powerful and deceptively simple. As a matter of fact, we are not going to write any code at all, because we have all the tooling required within the AWS console to accomplish what we need.

[0:18] In this lesson, I'm going to show you how to create a step function. The premise here is that we can string any number of Lambda functions together where the output of one function becomes the input of another.

[0:31] To demonstrate this, we'll take the JSON output from this NCAA rankings Lambda, and feed it into this PDF generator Lambda, which will then save those rankings into a PDF. Let's take a look at the step function service and see what it will take to create a state machine.

[0:52] We're going to design our workflow visually and we're going to use this first option. We will go with an Express state machine because we need to call our state machine synchronously, which we will do in just a moment. Inside our visual designer, we are going to define our first state, which is going to be a Lambda invocation.

[1:17] Let's go ahead and give this a state name and then choose the function name that we want this to call. We're going to choose the NCAA latest. Go ahead and select that. Our next state is going to be a new state, not an end state. We'll drag another Lambda in.

[1:47] Give this a name of print stats. We will go down and choose our second Lambda that we want to call, which is PDF generator latest. Then we will choose, "Go to End." Finally, click Next. What this does is generate a JavaScript, or JSON structure that defines our state machine.

[2:15] This is really nice because you can then check this into version control and do some really interesting things with it. Let's go ahead and click Next and we will name our state machine. In this case, College State Machine. We'll allow this to create a new role, default log level.

[2:36] Then we will create the state machine. Once this is created, we will test the execution of the state function.

[2:47] Before we do that, let's hop into S3 and clear out all of the PDFs that have been generated up to this point. This will make it easier for us to test whether or not we have created PDFs and also make it easier for us to find them.

[3:03] Now that we have cleaned out this S3 bucket, let's hop back into our step functions and we will start an execution. Once this is complete, we will see that in the output, we have a URI to a PDF. We'll paste this into the browser and view it. Well, it looks like something has been created.

[3:29] Let's take a look in S3 and refresh. Baboom! Here we go. We'll go ahead and delete this one more time and then we'll take a look at how we can call this from the command line. It's important to know that you can execute a state machine in a bunch of different ways.

[3:49] In fact, it wasn't really designed to be called directly from a user, but rather to be triggered to kick off some long-running asynchronous task based on some other external event. That said, we are going to approach this using the CLI.

[4:06] We are going to go AWS step functions, list state machines, and we're going to copy the ARN of the state machine that we just created. From here, we are going to call AWS step functions. We're going to synchronously make a call via start sync execution.

[4:27] We will pass in the ARN that we copied into the state machine ARN argument. What you'll see here is the output of this call. In this output, we can copy this URI and easily see if it's real by hopping into the browser and verifying that yes, indeed, we do have a PDF. Yes!

[4:53] If we go back into our S3 bucket and refresh it, we can see that our PDF is indeed here. To recap the concept that I'm introducing here. By using step functions, we can create a state machine that allows us to string multiple Lambdas together and create some pretty interesting workflows.

[5:16] In this case, we were able to successfully attach the screen scraper Lambda to the PDF printer Lambda in a matter of a few minutes. We can execute a workflow where it goes and grabs the stats off of the NCAA website, converts it into a JSON structure, that gets passed into the PDF generator. The PDF generator interprets that data structure using PDF kit and then creates a new PDF and saves it off into S3.

[5:48] This demonstrates a very powerful, and I would say on some level, a very easy way to string Lambda functions together to create complex workflows. If you understand functions, you understand Lambda functions.

[6:06] If you understand Lambda functions, you understand how to string them together into step functions and build out really powerful workflows.