hi my name is noah gift and i'm going to help you master aws lambda functions in python and
then use these lambda functions inside of aws step functions to build pipelines
really one of the most powerful things you can do with just a little bit of code
and for the most part it's a no code low code solution all right let's go and get started
and i'm going to go to aws lambda which is right here and i'm going to create a new function and
and so a lambda function there's really not much to it if you understand how to write a
python function you can write a lambda function i'm going to just say author from scratch here
i'm going to pick the runtime will be python39 and we'll call this function name here we'll call this
let's just call this one random fruit as well that'll be kind of fun and
we can now later if i wanted to integrate with other parts of aws you could change permissions
and things like that but basically if you just want to build a simple lambda function
uh you just need to select the runtime and name it that's it so let's go ahead and try this out
okay so the random fruit here notice that there's a trigger and a destination so
if it really fulfills that same concept right input unit of work return in our case we can just
focus on the unit of work here first and a lambda function they'll just give you this they'll give
you a structure like here we go lambda handler and and the event is the input to the landa handler
so we can actually easily build out a function here that does some work so so what i could do
is let's do the same thing let's let's kind of take our code that we did but from
before and actually build out something similar so i'll say from random import choices like
this and uh inside of this i can i can process the event and so the the events would be um
maybe uh let's call this um fruits so we'll put in a fruit event
is equal to so we'll say fruits is equal to event fruit so basically i'm going to
look for the word fruit in the event and then i'll say basically if the fruit is equal to
let's say cherry then i will say return and we will actually just take we'll just borrow
this code that they already gave me here i can just do this i can say if it's a cherry then
let's go through here and let's dump out something like
f string
uh i love
i love
something like that there we go so we we can we can put this f string in there
i'll put the single quotes to keep things the same
and then we can say basically otherwise we'll do a different return statement
and we can just take this code here and we can say
something like new things or let's do f string as well no thanks no thanks
i don't like
i guess i do need to do a double quote here like this no thanks i don't like
fruit something like that okay so we've got we we basically have a a function that accepts an event
we are going to look for the word cherry if it's cherry i said i love this fruit otherwise the
default value will be a return back that i don't like this this fruit so i need to build basically
um really almost nothing now because all i need to do is is take a a test payload here
to to to make this work so i'm going to click on deploy this will deploy the
function then i'll click on test and i'll create a new event and we'll call this um
actually i don't even need if i go back here i don't even need random
choices here because we're not even going to do a random choice we're going to do a manual
choice so i'm going to redeploy this i'm going to go to test and we'll call this cherry and
we'll we'll go through here and we'll call this
let's do this let's say fruit
and we'll call this cherry
and we'll put this called this fruit so let's go ahead and format this looks good let's save it
now if i test this it should return back this message i i might even actually add one more
thing to hear that just that gives us a little bit of debugging information that says like you know
this is the event something like that so that i can see what's actually passed in as the events
which can be handy so we'll deploy one more time okay now we've got that let's test it
there we go so we see that this is the event that was passed in and it says look body i love cherry
right and now if i want to make another test event and we make one create a new one
and we'll call this one pair what we'll do is we'll add this and we'll add pair we'll save it
and we can see that they'll get a different result this time if i if i test pair no thanks
i don't like pair right so this is this concept of something comes in you do some work and return it
is really fundamental to all software engineering to distribute computing to community tools
and the reason why i think this is really goes in well with what we're going to
cover with chameleon tools is it's kind of a similar concept and what you can do actually
is you can chain these things together and you can actually build you know multiple lambda functions
that work together so similarly with command line tools right you can chain things together
and build multiple things so let's build it now that we've got random fruit working
let's build another function and we'll call this one like the chef and so this will be
the the cook that is going to cook things or the chef that's going to cook things together
and so what we can say
here
is we we can basically process that that that event that we had before earlier and so
we could basically say inside of this thing like um we can call this like the the meal
is equal to events and uh and and i think we we need to just be able to process
what actually comes back from the event i think it could just be the whole event actually so
let's just let's just make the meal the event and we can say if um if yes or i think it was if um
yeah we'll just say if um like actually let's double check let's just save this for a second
and i'll go back to my other function to make sure i understand what it returned it was
we have um fruit here where was it's random fruit so if i go to this what is it returning i love
so we'll say if love yeah so if it returns back in the body here this i guess i could
i could make it a little bit more structured but let's just keep it simple here
and we'll say if love
is in the meal then we can return back some other payload right so we can do this and we can say
basically
let's
let's do this let's make a salad
there we go otherwise we can return back
basically a different option so we can say return here and we can do
make this come back a little bit there we go and we can say let's make a barbecue
right so we have two different options like if someone says hey i like this fruit
then they make a salad otherwise they make a barbecue and we could we could deploy
this and we could test it out because we can just do so if i if i test this thing again
let's test it with the positive events we can assume that the payload will look like this so
i can i can literally put that into um into my test function so i can configure test function
and just do this like that format the and let's see what happens if i run this if i say oh we'll
call this um meal or salad or whatever we'll test this let's let's make a in this case we need to
parse the body so that's what we need to do so let's go we'll say if the event would be body
body if and now let's go ahead and deploy it let's test it uh the function oh wait
let's test it again there we go let's make a salad right so basically we were able to to to interpret
that payload do some stuff with it and then do one operation and now if i want to do the second
the negative so we want to create a new one we can say what happens if i say
what's the second thing that we we do here so if i want to test against then the pair no thanks
so let's just take this payload put this in here and we'll call this one
event name would be barbecue we save it we test it there we go let's make a barbecue right so
we have a couple different cases so we can kind of chain these things together now all i need
to do to to put these things together would be to go to the step function service of amazon
which coordinates basically state operations and we can say design your workflow visually this is
what we want and this is really cool is that in a sense you're kind of a a low code or no code
coder and so we can put a comment that says like you know this simulates what a chef would do and
so we can grab a function right here and then we can say after that function we want another
function that goes like this and so what this first one will be the um the the fruit processing
and then the next one will be the meal processing there we go meal processing processing
and what's what's what's awesome about this is how explicit all these steps are and how easy it is
to to process you know a pipeline like this because of this concept of function
with inputs outputs all that so what we do is we just go here and we go to
we need to find the function right here and we can go the the one we just did which was called um
random fruit i think which is called random yeah random fruit right there and we want to use the
state input as the payload right so we basically are going to accept something from the user
and then the next state will be the meal processing so now we go to meal processing and
what do we do we pick the the chef here so let's go ahead and pick this there we go chef latest
and then we can actually go through here and uh we've got that all working and
we we've got it so we've got actually all of this code is all set up for us
and we can even check that in if we wanted to into our into our project later but for now we'll
just click this here's my state machine we'll just call this uh mule meal generator and say
you can even log it if you want we'll say all logging that looks good
great state machine perfect this looks good it's creating it now it's pretty easy to test
because for this random fruit if i go to the the i just grabbed something like this if you remember
the the pear or the fruit now to execute it click on start execution and i just put in the same
payload right so it could be what do i need to pass it let's so if i pass in pair it should start
a chain reaction let's go ahead and do that there we go here we here we see it's processing awesome
it succeeded look we can see what the input is right and we can see what the output is no thanks
i don't like peer now it's processing this input says i don't like pair step output let's make a
barbecue so so pretty cool pretty cool concept of right is chaining things together that really
is how linux works right or how the linux command line works is the same thing as is you have inputs
processing and you chain it to other things a lot of linux command line tools work that way like
you know touch or make or you know ls or all these things you you basically chain them together