AWS Lambda Terraform Tutorial with API Gateway

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video you're going to learn  how to build an API in AWS using a   serverless framework you're going to use  Lambda function with the API Gateway all   to be built with terraform I'm your host  Sam Gabriel and let's get started [Music]   all right so what we're going to do here is we  are going to see how Lambda Lambda works with   the API Gateway uh we're gonna show a demo of how  this is all going to get built using terraform and   then we're going to spend some time going through  the configuration to see how all the magic works   so let's go ahead and figure out how  Lambda works with the API Gateway   so you have a client and this client wants to  access your API so we put an API Gateway and   you define a method so of course your API will  have many methods so for this demo we're going   to have a get method and behind it you're going  to have your Lambda function that's going to get   triggered by the API Gateway and this function  is going to have a very simple role it's going   to create a currency conversion it's written  in Python and you basically give it an amount   let's say 100 and convert 100 let's say Canadian  dollars to US dollars or vice versa or whatever   currency you want to convert so what happens is  the client request comes in to the API Gateway   the API Gateway invokes the Lambda function and  we get the result back through the API Gateway   and that gets passed over back to the client and  one thing to note when I was looking through this   I was thinking in my head how can I use Lambda uh  you know for for apis when Lambda can only last   for 15 minutes and the answer is really the API  Gateway that is always on ready to take requests   and then invoke that Lambda function in the back  end all right so let's go ahead and take a look   at the demo see how this all works let's go to my  visual studio code here and let's see not this one this one over here okay so we have uh a  a repo a GitHub repo and I have a bunch   of instructions here to walk us through how  this all works I have a main dot uh pi and   this is where the function code lives and we're  going to go through that in the configuration   section but for now let's go ahead and go  through these instructions and run the demo   all right first things first we need to create  a requirements.txt file this is to allow us   this is to allow us to uh package up a zip file  that we're going to upload into AWS this is what   the Lambda function needs to to get created so  we're going to do that we're going to create   actually before that I always use pip and to uh  to help me create a virtual environment when I'm   developing with python right so once I'm done  developing you end up with a pip file here and   this pip file will contain all the dependencies  that you might need now once I've done that I   want to run this pipn requirements command that  will push all those requirements to a text file   called requirements.txt that we're going to bundle  in that zip file that we need for for Lambda to   work okay so now we have that created perfect  let's move to the next section here I want to   install all of my dependencies in a folder  called package so let's go ahead and do that okay so now we should have inside the  infrastructure folder here we should have   a folder called package and all my dependencies  are over here all right perfect now let's go into   let's change directory in here so let's clear  that for a second and now we're inside the package   directory and let's go ahead and zip it so let's  zip that and now we are ready to go back into um in here and let's zip my main.pi this is where  I showed you the the python function lives and   we're going to zip that as well and that will all  be packaged in this my deployment package.zip file   that we're going to use with Lambda all right  so now we need to start working with terraform   and the first thing we need to do is export our  environment variables AWS environment variables to   get us going and to do that I need to have an AWS  account so if you're using the tekkenades Academy   subscription you get access to AWS accounts and  you can launch an AWS account very easily here   as you can see in the AWS environment lecture so  you can just click launch AWS environment and that   is going to find an available AWS account and  give you the details to log into this account   all right so we have uh all the necessary  information to log into this account also   gives you the actual environment variables  that we can use with terraform so I can copy   that and by the way this lasts for three hours  and they will self-destruct at the end of three   hours this is plenty of time of course to to  play with our lab here so copying that over   and we are going to paste this in here and  now we have this shell ready to go with uh   initializing terraform and running terraform all  right so let's go ahead and run terraform in it so this is going to initialize terraform and  get everything ready for us and now let's run   terraform plan to see what is going to get created  and by the way if you do not really know terraform   very well I have a terraform 101 course that you  can access in the Tekken 8 Academy and work with   that and I'll put a link to it in the description  of this video where you can access it all right   we ran terraform plan let's expand this a little  bit and see what happened here you can see a bunch   of resources are getting deployed and API Gateway  deployment API Gateway integration with Lambda API   Gateway method the get method that we mentioned  an API Gateway resource of course and a rest API   we have the API Gateway stage that we're going to  deploy and we'll get a URL at the end of running   terraform to be able to access the function  AWS Cloud watch log group to be able to see   our logs in cloudwatch and I am policy that is  needed for Lambda and I am role for Lambda and   in I am role policy attachment for Lambda  Lambda function itself here and uh this   is the file name that we need for the Lambda  function this is the zip file we just created   so we're going to push that over and Lambda  permissions as well for the API Gateway to   access Lambda so these are the resources 12 to  deploy so let's go ahead and run terraform apply run tear from apply here and this should run the  plane again and if everything is good we like   it we'll hit yes and we'll find the end point at  the end of this run so it takes about 10 seconds   or 15 seconds to get everything uh ready to go  and we'll have that URL at the end to uh to uh   to player play around with our API and make some  conversions for uh for different currencies okay   looks like it is complete endpoint URL is this  one so I can just open this in a browser window   and as you can see here we are automatically  getting a result and the whole conversion happens   in the parameters at the top here you can see the  amount is 100 and from currency from US dollars to   Canadian dollars and the result is 136.34 cents uh  this all works fine of course you can make changes   here you can say instead of a hundred dollars you  can say two hundred dollars hit enter and you get   your result back right away and we're hitting the  API function so that all works very well we've   deployed everything using terraform now let's  go ahead and take a look at the configuration   how all this magic happens all right so let's  go back and um a look at the main function   remove this for now now this is the python code  that is run with Lambda and any Lambda function   has to have this Lambda Handler okay and basically  it's listening for events and what happens is I   can grab the parameters that we saw in the URL  the amount the from currency the two currency   doing this with the event query string parameters  and again catching the amount from currency to   currency and saving them in these variables and  then I'm also using the logger or logging python   library to send some logs that we will see in  cloudwatch shortly so here you can see that I'm   logging out the input parameters the amount the  from currency the two currency I'm also running   this convert currency function at the bottom here  which takes on the amount the firm currency the   two currency and all it's doing it's creating a  class of currency rates which is using the Forex   python library and once you do that again you send  that over to a convert function in this class or   convert method in this class with these different  parameters and you get the result the result gets   passed back in here we log the result out to  cloudwatch and then the Lambda function itself   needs to respond with Json so here we respond with  a status code of 200 and the body needs to also be   in Json formats so we're running json.dumps to  take on the result and put it here in this Json   format the logger finally responds back with this  particular Json response as well so we can see it   in our logs and that is it very simple python  Lambda function that gets generated through the   API Gateway now let's go ahead and take a look at  our infrastructure and particularly the main.tf   file here and as you can see we're defining the  terraform block with the required providers is the   AWS provider pinning the version which is always  good practice and we are going to run in Us East   one we have a bunch of resources so let's focus on  the AWS Lambda specific resources that are needed   the first one is the AWS Lambda function and here  you define a few things such as the name of the   function you want to give it the runtime is python  3.9 the role there is a role here that's going to   be associated with this particular function AWS  Lambda function the Handler here is very important   to understand that it's a combination of the name  of the file main which is the name of the file   here main.pi all right so Main and then Dot and  then the Lambda Handler is the main function here   that is handling the events right so main dot  Lambda Handler is what you want to Define here   and then the file name that we're pushing up  there and this is the my deployment package.zip   that we just created at the beginning of this  video where we're zipping the dependencies and   also the main.pi file and shipping it out to  AWS depends on these are a few things that   we want this Lambda function to depend on a few  resources the AWS IM role policy attachment and   the AWS Cloud watch log group okay we're creating  a a cloud Watch Law Group so we can see the logs   from Lambda and we're giving it this name or  path here retention is 14 days then we're looking   at an AWS IM policy and this policy is called  Lambda logging in the path at the root path and   the description is that this is an IM policy for  logging from a Lambda function so you can see here   the policy we allow creating log group creating  a log stream and putting or updating log events   so that is what's going to allow Lambda to post  logs into cloudwatch and then we're creating   this AWS IM role policy attachment where we're  taking on the AWS IM role called Lambda roll   and the policy uh AWS IM policy that we just  created up here and we're creating those as an   attachment and then we have the AIM role here that  we just mentioned the Lambda role and in here we   are allowing access to the Lambda service okay so  we're able to to do things with Lambda of course   all right so that kind of uh covers the Lambda  portion of the terraform config now let's take   a look at the API Gateway portion remember the  API Gateway is what allows the client to hit   the API Gateway that talks to the Lambda in the  background all right so we need to create a rest   API with the AWS API Gateway so we do that we call  it example API the AWS API Gateway resource is   what we Define here you have a rest API ID which  calls on this particular API Gateway rest API   resource and also the parent ID is here as well  path part is a variable you can see this variable   over here under variables the endpoint path  we're going to use is conversion so if you   look at the actual API call it's under it's  slash Dev slash conversion so really that's   the end point that we're hitting uh conversion  okay so that's the path that we defined here there's also the AWS API Gateway method that  we're using is get it's a get method so we're   able to use the browser to easily get the result  here as opposed to a post method which you might   need to do a little bit more work maybe use  Postman or create some front end to uh to   make the post call all right so we've got the  method ready we're not doing any authorizations   authorization is none to keep it simple the AWS  API Gateway Integrations where we're integrating   the API Gateway with the Lambda so here we're  again talking about the rest API ID resource ID   the HTTP method that we just defined here and the  integration HTTP method is post this is mandatory   when you're using the type AWS proxy so AWS  proxy is the way to to go there was another   method or type before but recommendation from  AWS now is to use AWS proxy and the URI here   is the AWS Lambda function uh invoke iron so  Arn so that's the one that was defined here as   well okay so that's really where the glue happens  between the API Gateway and the Lambda function   now we need some permissions to allow the  API Gateway to talk to the Lambda function   so allow execution from API Gateway actions to  Lambda invoke function and so on and so forth   now the source Arn is very important here  and I didn't actually mention it I should   have mentioned it before we ran terraform  I do mention it in the instructions here   and that is in the file terraform.tfrs you  need to put your account ID and this account   ID you'll see if you go to terraform Tia vars  is this one here which I had ahead of time this   account ID you get when you're running  the um let's see when you're running the AWS environment when you've ran the  AWS and environment you can find the   account ID right here it spits it out  if you are using your own AWS account   or your work account you can always see  the ID at the top right corner of your   AWS account and I'll show you that  in a minute when we when you launch   the AWS console all right so back to our main.tf  now we have this Arn everything here is fine it's   all variables that have been defined or resources  that have already been defined for the source Arn   uh the this resource AWS API Gateway deployment  is needed to be able to finally deploy the uh the   the API Gateway rest API Gateway so that we can  access it from our browser so you can see here   the trigger redeployment we've seen the life cycle  is create before destroy this is important because   if we make an update to this endpoint you need to  be able to tell AWS to create the new deployment   before destroying the old one because the default  in terraform is to destroy first then create   now also that depends on is is also very important  to to make sure that you have the API Gateway   method is created before this Gateway deployment  is created and also the Epi Gateway integration   finally the last resource is the API Gateway stage  and we call this stage Dev of course you can call   it task Dev whatever you want or prod and this  really reflects here when we look at the URL uh   you get Dev here and then of course the the  conversion is the endpoint that we are hitting   so this pretty much sums it up when it comes to  the configuration from a terraform perspective   of course we have our outputs here which  output the the URL for our endpoint I also   added a a parameter here to be able to show us  you know an example for a currency conversion   the variables we kind of looked at this really  quickly the region the account ID the Lambda   function name we call it currency converter  and the endpoint path all right so now let's   go ahead and open the AWS console and take a look  at what we have there I am going to go back to my go back to my lab environment my AWS  account environment and copy the URL and go back to my incognito window   let's paste that here and I'm going to  need my I am username matches this one and my password which is this one okay and let's sign in all right so now we have our  AWS console here and as I promised before if   you want to look at your account ID you can click  at the top right here and you'll see the account   ID right here all right so let's go ahead and  take a look at Lambda I want to look at Cloud   watch and I want to look at the API Gateway so  let's get those loaded first of all let's take   a look at the AWS Lambda console and I want  to show you a couple of things the function   name here is currency converter so click on  that and you can see the uh the trigger here   for the currency converter is the API Gateway  and this will load the actual function itself   in this kind of like an IDE window here where you  can make changes and when you're done changes you   deploy those changes and see the difference  or see what happens you can also look at the   monitoring section this pulls up Cloud watch logs  or you can view the logs directly in cloudwatch   if we go and take a look at Cloud watch  itself you'll see that there is a logs   group here that we can take a look at this logs  group got generated of course by terraform and   uh and Lambda has access to write to it so if  we go here we can see the the log stream here   the last log that we saw and we can see  the different if I make this a bit bigger you can see here the logs that we saw in  the python code the input parameters 100   is the ml from US dollars to Canadian  Dollars the result is right here the   response with Json is also available right  here so all this is showing us of course   here's another stream where we ran the 200 US  dollars and the result and so on so all those   all this is very useful to be able  to see your Vlogs here in cloudwatch   and finally the API Gateway is that example  API that we talked about and if I open that   you'll be able to see a nice diagram showing you  here's the get method that we talked about and   the endpoint is conversion so at this point  here you can see the client making a request   to through the method request here and that  goes to a Lambda proxy that invokes the Lambda   function and then you get a response back to the  client so it's very nice to see that you can also   have a test here so you can run a test parameters  here you can see the conversion run test   and you can see some of the logs here as well and  I've used this quite a bit to troubleshoot until   I got to the place where it's actually working so  you can see here you can run these tests straight   from the API Gateway itself and see the response  from from Lambda uh this is everything I wanted   to show you in this video I hope this has been  useful to sum up we basically were able to run   serverless and create an API using aws's  API Gateway in conjunction with Lambda and   build everything with terraform thank you for  watching and I will see you in another video
Info
Channel: TeKanAid
Views: 13,810
Rating: undefined out of 5
Keywords: AWS Lambda Terraform Tutorial, api gateway, aws api gateway, aws lambda, aws lambda function, api gateway lambda, aws cloud, amazon web services, Amazon api gateway, api gateway aws, api gateway microservices, api gateway tutorial, api gateway aws tutorial, api gateway terraform, lambda terraform, lambda function terraform, api gateway explained, aws lambda tutorial, aws lambda example, aws lambda python, aws lambda function example, aws lambda function terraform
Id: UllPQzVXYtU
Channel Id: undefined
Length: 24min 3sec (1443 seconds)
Published: Wed Dec 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.