Transcript for:
API Gateway to Lambda Functions

alright guys welcome back today we're gonna be doing an episode on API gateway to lambda functions I'm going to show you how to create a restful api gateway endpoint that's going to invoke a lambda function and return some JSON all the way back to the client so let's get started so I'm gonna start this out by creating a lambda function and I'm gonna call this lambda function the transaction Handler and I'm gonna code up the lambda function so that I can take an event input that looks like this this is the default events that you get when you set up a rest endpoint and invoke a lambda function through API gateway there's a bunch of other parameters that come above and below this but since we're gonna be passing in the parameters through the query string this is the only real section that we care about from the event object from there I'm going to set up a transactions resource and I'm going to set up a get API and that API is going to be configured such that it forwards requests to the lambda function and I'm gonna code up the land of function such that it returns a hard-coded response in your applications you may choose to do something with the query string parameters like maybe call a database or do something in an s3 but for this purpose I'm just going to show you how to return a response back to the client and then from there I'm going to poke the endpoint and just basically invoke it from my browser and you're gonna see that the response is gonna come all the way back the other side so that's what we're gonna do today let's it over to the console and get started all right folks here we are in the console so let's get into it having over to the lambda section so let's start this up by creating our lambda function so going to create a function we're gonna be doing this one from scratch we calling this transaction process and this is going to be a Python 3.6 application and for permissions I just want some basic CloudWatch permissions because I'm gonna be printing stuff to logs and I just want to verify that I have that permission so luckily if you select create a new role with basic lambda permissions the cloud watch permission policy is included in that so that's kind of a free gift for you so you can go ahead and click on create function sometimes it takes a minute or so so I'll come back when it's all done alright so after waiting a few moments the function was successfully created so let's head down to the code section where we can read our code alright so we got this boilerplate stuff here I can actually write in sublime and then copy and paste everything back into this IDE over here so switching to sublime text so this is the kind of boilerplate that we are starting with so this function is going to consist of four steps so the first one is we need to parse out the query string parameters and then just print it out so we can see what's happening so far so it's query string parameter string parameters I'm going to be passing the transaction ID a transaction type and an amount so let's try and capture that so firstly transaction ID and that's equal to the events query string parameters and transaction ID I'm going to copy that twice come back here and say transaction type and just change that to type and then amount so transaction amounts and change that to amounts and I think let's just print out all three so transaction ID I've gotten quotes all right again twice okay and then transaction type and then finally amounts all right so that's good for step one so second step is to construct the body of the response and it's gonna be like a basic JSON object here so instructs the body of the response object so let's just create an empty object and then start filling in some values so we're gonna be returning the values that were originally passed in to us and then I'm gonna be adding a separate field at the bottom so let's start with transaction response transaction ID and we'll set it to what was passed in so transaction ID and copy that's change this to type and then change this to transaction type copy that set that to amounts and change this transaction amounts and copy that again and I'm just gonna add a fun little message here so message and let's say hello from lambda land okay and then we need to construct a HTTP response object so let's do that and we're gonna be filling in the body with this transaction response object that I just created so first thing we need is a status code I should let you set this to empty first and set the status code it's a 200 and then set the headers initially as empty I make a minor syntax error on line 25 when I come back to fix it later and on the headers we need to set the content type to be application JSON and then finally we want to set the actual body body and we need to convert our object into a string so we're gonna use the JSON library and use JSON dump s and set our response object that we created up over here and then the last step is just return it and we do that all right so that looks good can take this copy it all out move on over here and paste this in all right set that is safe all right so lambdas ready to go so let's head over to the API gateway section and create our resource so going to API gateway I'm gonna click on get started and they kind of give you this tutorial thing if this is your first time here I'd encourage you to actually check this out and try this on your own it's really interesting and get to learn a lot about the capabilities of API gateway we're gonna be using like a very very simple approach here so we're gonna be using rest that's good we don't want this example or we select new API I'll just call this transaction api's no description is fine and point type this largely matters for kind of network connectivity reasons not going to go into what these all mean but you know you leaving this as default just so you know what that means create API all right cool and then we're gonna create a resource that resource is going to be called transactions leaving everything here is defaults and from there we need to create a method and like I said in the intro we're gonna be creating a get method click on checkbox and then this is asking us where do we want to send this request to so you can use the lambda function like we're going to do in this case you can send it to an HTTP endpoint which could be like an ec2 instance running maybe nginx or something and you want to forward the request all the way there could be a mock could be a separate AWS service there's lots of capabilities here so lambda functions good enough for us and this is important so you want to use lambda proxy integration so this is going to inject the HTTP request into the lambda function event parameter and that's what we're kind of parsing out on the lambda side so I'm gonna check that and this is asking us which lambda function do we want to hook it up to and we just start typing there we go so transaction processor click on that guy the vault emotes good click on save you're about to give API gateway permission to invoke your lambda that's fine okay and then in order to actually get this running we need to deploy it so while I'm still on the get section I'm gonna go to deploy API and we don't have a stage typically most people do but we need to create one for this exercise and a stage is useful if you want to kind of logically separate your api's into maybe like a test environment or a production environment so I'm gonna say this is a test environment and just leave this stuff blank I'm gonna click deploy and there we go so this this function is now deployed and connected to the lambda function ready to serve track traffic so actually let's go expand this out and you click on the get function and you can see this is the URL endpoints so I'm gonna take this and I'm gonna open up a separate tab and we're gonna hit this API and we're gonna pass in those query string parameters so I think I said transaction ID equals 5 and type equals purchase and amount equals 500 so if this works correctly we should see a JSON response that has these same values mirrored 5 for the transaction ID type for purchase and amount being equal to 500 and we should also see that extra hello from lambda line message all right and there we go so again we passed in the transaction ID type and amount and we got the same transaction ID type and amount back and also the response message hello from land to land so let's just quickly head over to the cloud watch logs to verify everything looked as I thought it would go to cloud watch logs transaction processor extreme and there we go exactly what I did so logging the transaction ID the transaction type and the transaction amounts so that about wraps this video up and thank you so much for watching and hopefully you did find this useful and if you're already using or considering using the API gateway with lambda functions I'd love to hear about your use case in the comment sections below thanks again and don't forget to Like and subscribe I'll see you next time