Transcript for:
AWS Step Functions and Lambda Integration

alright folks welcome back to AWS simplified the place where I teach you everything there is to know about AWS and today's video is going to be on AWS step functions and specifically I'm going to show you how to integrate step functions using lambdas so let's do a quick overview of what we're going to do today so we're going to build out a transaction processor state machine the theme of this application is going to be credit card transactions credit card transactions can come in two flavors through either a purchase or a refund so our starting of input event is going to look something like this so we're going to have a JSON object that corresponds to a event type and the event type is going to be one of either a purchase or a refund in this example here I'm just showing you the purchase from there that's going to be fed into the input of the state machine and one of the first decisions is going to be a choice task so we're going to have a process transaction choice where we need to make a decision on which direction in the state machine we want to go so depending on the input type so in this case we have a purchase we want to go in the left direction so we want to go in the left here towards the purchase processor and conversely if the input is a refund type that we want to go in the right direction and that's going to be pointing it towards the refund processor so both of these processors are going to be backed by two independent lambda functions right so we have the lambda functions that are going to be responsible for parsing the input from there we're going to process the input and return the results back from the lambda function into the state machine and that just going to be the terminal state of our state machine so let's head over the console and learn about how to set this thing up all right folks here we are in the AWS console so I'm going to start this out by going to create our two lambda functions so let's head over to lambda and do that now and click on create function we're going to do this from scratch this guy is going to be called processed purchase as a reminder we're going to have two functions one is going to be called processed purchase and the other one is going to be called process refund and we're going to be using Python 3.6 and we just need a very basic role here so we can just leave the default option create a new role with basic lambda permissions and we're gonna click on create this usually takes a minute or so so I'll come back once it's all done alright folks so after a few moments the lambda function was successfully created so let's set everything up so the first thing we need to do is actually write our code and paste it in here so I'm going to go to sublime text to actually do that so in this function we have a process purchase function takes a message and context the message is going to be eat the basically the json object that corresponds to the input that's fed into the step function and as a reminder from the overview slide in the beginning the input event is going to look like this we're gonna have a key that says transaction type and a value that's purchased so the processing basically is three main steps first one just for debugging purposes is basically just log the input message so we can see what we're actually dealing with so let's just say received message from step functions and print out the message second is constructor response object and we're just going to call this response and we are going to set the key transaction type to be the basically the input that was passed in to us so I'm going to set the value to whatever the value of the input was so we need to say message transaction type and then let's set some more fields here just to prove that the processing is actually coming from lambda so time stamp and what are gonna be using dates time it's timed on now and then we want to do some processing on the date/time object so we want it in year-month-day and then our minute second okay that looks good and let's just set another cute little message here cute little property rather and let's just say hello from lambda and inside the process purchase function and the last step is basically just return the object alright so that looks good so let's just take all that come over to lambda dump it in one thing you need to make sure to do is to change the handler name so we need to change the handler name to the name of our handler function paste that bear all right save everything else here all the options here don't touch them everything is fine by default you don't need to touch it so that about wraps it up for the process purchase function I'm going to go ahead and create a identical copy of this except I'm going to call it the process refund function so I'm going to come back once that's all set up all right folks here we are back at the main section in the console so you can see here that I created two functions now so we have the process purchase and process refund for all intensive purposes these things are exactly identical you may be asking yourself why do you have two lambda functions why can't you just use one lambda function you totally could this is just an example for this demonstration so before we head over to I am we actually need to create a role for this for the step functions to use let's just go ahead and grab the AR ends of the lambda functions because we're gonna need it in one of the next steps so you can see here after I click into the lambda function up at the top right here's the RN just going to copy that bring it over here paste that there I'm going to go back to the refund processor and do the exact same thing and the reason we need this is because once we specify the step function we need to actually give it a target of which lambda function we are pointing two dots so we need the AR n in order to do that so now I have those two AR ends put aside let's head over to I am where I actually need to create a all so step functions can invoke our lambda functions so I'm heading over to I am then I go to roles I'm gonna click on create role and there is a default policy or default role here that we can use yes so if you look over here there's step functions I'm gonna click on that and you're gonna go to permissions and you can see here by default it has a lambda role that's associated with it so if we expand this out we can see what it actually is giving us as JSON please and then you can see that it's just basically giving your step functions the ability to invoke your function so as a reminder by default and at least in the example we're using the lambda is going to be invoked synchronously from the step functions perspective okay nothing else needs to be touched here you can leave this as default I'm gonna click Next I'm not going to be touching tags that's fine krei role we're going to say step function lambda role and I'm gonna click on create okay awesome so here's our lambda role so now we can actually go finally and create our state machine so let's go to step functions okay and we're gonna click on get started and Bob okay so here's the default screen that I was looking for that last one kind of threw me off I think it thought that it was the first time we were creating a function so it decided to put us through the wizard so we're gonna click on create state machine and we're gonna call this the transaction processor States machine this is the default view that you get when you're defining your step function so step functions are written in actually says it right here so Amazon States language it's a proprietary language that I believe is specific to Amazon it's actually a little bit there's a little bit of a learning curve but once you get used to it it's it's pretty straightforward and the neat thing is that as you're making changes here you can refresh over here and the corresponding representation of you code will be shown in this kind of visual area so I've actually prepared one of these in advance and I just want to walk you through what's actually happening so let's actually look at this in sublime and then we'll paste it over and take a look at what's actually happening so we're basically saying here's where we have a star dot section so we're saying we want to start at this step the process transactions done and we have multiple states here so we're starting at the process transaction and the type of state is a choice state so we have multiple choices here so we have an array that corresponds to two choices one here and one here and what we're basically saying is I want to make a choice on which direction to take in this lambda function based on this variable and this variable with this special with this special syntax here with a dollar sign dot something basically corresponds to the input so as a reminder our input it looks like this so we have a JSON object and the key is the transaction type and the value is the value whether or not it's a purchase or a refund so what this is basically saying here is based on this variable the transaction type based on the actual value of that variable I want to make a decision so if the string is equal to purchase then I want to go to this step and purchase process purchase corresponds to this State down here okay and similarly we have the opposite side of that choice so if the string equals refund and then we want to point it to the process refund step and if we look down here of what these are actually doing we can see that it's a task and this is the resource that it's pointing it to so the resource in this case is a lambda function and this is we're going to paste in our a our ends that we gathered from the previous step and we can see here that this is a terminal State so once this is processed processed successfully we are done and basically the process purchase is pretty much an ax clone of the process refund okay so let's take this and paste it over here and by the way I'm gonna be making this code and the lambda function code that I wrote in the previous step available on the description section of this video and if we just click on refresh after we've pasted it in we can see kind of a good representative visual representation of what our state machine is going to do so at the start it's going to go to this decision task and then based on the input remember the input is going to be either refund or purchase it's going to make a decision to either go left or go right and once that process is either of those choices it will be done it'll be in its terminal state so one thing that you can see here is we have this red dotted line it's indicating to us that we need to give it a valid AR n which is fine because we just did that in the previous step so for the process purchase sake that projects purchase process purchase is down here so that's the RN cool and process refunds so let's take that and put that here okay that looks good just a quick little refund refresh make sure everything's good looks good no other options we need to actually give it the roll which I believe is yep so it's in this next step so luckily we created the role that we already need so we just just can select choose an existing iam roll and then I guess it's just defaulting so it knows that we just created one recently so it's giving us the iam roll so we're gonna use that and then we're gonna click on create state machine okay awesome so our state machine is actually created now so we can actually run a instance of this duct function so let's go to start execution and you can give it a random UUID or whatever idea you want here and I'm just going to adhere to what I said in the previous step of what the inputs gonna look like it's gonna look like oops random so transaction type is purchase and then we're going to click on start execution so you can see here it's kind of giving you a view of what is the lambda for what these state machine is actually doing so the blue corresponds to what the next step is and you can see that after it was successfully processed it turned green so part of the cool part about the step functions is that you can actually see what it was doing and this is a real-time update of when this stuff is actually being processed so if you have a use case where you know something is stuck at a certain state or you're trying to figure out for instance where a credit card transaction is at its life cycle in this step function step functions by default kind of give you a good visual cue as to what's happening so if we kind of look at this a little more we can see okay we took this this route we started that process transaction based on the event type we branched off to the process purchase step and then we came to the terminal step so let's let's click on process transaction and we can see some of the input and the output that came out of it click on input I can see what was given to it as the transaction type and then click on output well it was just kind of forwarding that message down there was no exception so that's good and then if we click on process purchase so this is where that timestamp and where that custom message that I defining the lambda function should show itself so we can see something similar so we see that this was the lambda function that it invoked we can see the input that was handed to it and the output and there it is so there's the output from the lambda function we're giving it back what the transaction type was we set up the timestamp and this is exactly what came out of it and then here's the cutesy little message that I kind of prepared so let's just do that real quick again for the refund just to show you how it kind of takes this path instead go to new execution set this to random to and refund click on start execution you can see we took this left branch already and in a moment or so we should see that it processed successfully and have a similar output to what we saw in the previous so there we go process refund input was refund perfect output was exactly what we expected so that about wraps this video up thank you so much for watching and if you have any topics you'd like me to cover drop me a message in the comment section below and don't forget to Like and subscribe look forward to next video we're all be doing a high-level overview of step functions and some of their capabilities thanks so much and I'll see you next time