‫Now we really wish to have API Gateway
‫in front of our function, just like we've done from before.
‫So we'll have to go ahead and modify that template,
‫that yaml file, to add some API Gateway related stuff.
‫For this, very easy, we go back to our examples
‫and we should be looking for an API Gateway.
‫For example, there's a API Gateway authorizer,
‫it's not exactly what I want.
‫I'm more looking for a microservice type of stuff,
‫so let's just scroll down.
‫And we get microservice endpoint python three,
‫that sounds about right. So let's close these things
‫and so here we have a lambda function,
‫a template dot yaml, and let's open these two files
‫to see how they work. So the lambda function itself
‫is now very simple, it's just going to return
‫a little bit more stuff and it uses a DynamoDB table,
‫so we don't need DynamoDB right now.
‫But what I want to do is just copy this whole,
‫this whole document and we'll edit it as we need.
‫So let's go and copy this.
‫And we go to our app dot py, and I'm going to paste it.
‫So we have a lambda handler function and this is great,
‫for now I'm not going to need DynamoDB
‫nor table name, we'll use this in the next lecture,
‫for now just remove it. And there's a respond function
‫to return either error or result,
‫and I'll keep this function because it works great,
‫and this is exactly the kind of format
‫that the API Gateway expects us to have,
‫so I'll keep this respond function.
‫And for the lambda handler,
‫I'm going to remove all the comments.
‫We can print to the log what event was received.
‫I'll remove any operation
‫and the only thing I'll do right now
‫is say respond and there's no error, so I'll say none
‫and the result is equal to 'hello world!'.
‫Okay, so we're just returning hello world,
‫but this time in a format that the API Gateway likes,
‫which is JSON format with a a statusCode,
‫a body, and a header.
‫Next, we have to modify that template, that yaml,
‫to include some API Gateway related stuff.
‫So let's go back, and here we have the template, that yaml,
‫but now we have more stuff in it,
‫so let's see what we're going to need to have.
‫If we take a look at our lambda function,
‫now what was added in it is there was an event
‫right here that was added, and this event adds an API.
‫So let's just copy this event, one by one,
‫and so we're creating an API,
‫so we'll call it 'HelloWorldSAMAPI'
‫and the type of it is API
‫and the path is going to be 'hello'
‫and the method is going to be 'GET'.
‫So we're saying okay whenever we go
‫to slash hello with the GET method,
‫we should invoke our lambda handler.
‫So we didn't add much things, we just added this event
‫and modified the app dot py code,
‫but already we're going to have an API Gateway
‫installed for us by the SAM framework.
‫So let's go ahead and run our commands.
‫So we don't need to recreate the bucket,
‫because the bucket was already created for us.
‫But I will need to run the cloud formation package again.
‫So I'll run the cloud formation package,
‫and it did upload the artifacts
‫and updated my generated templates,
‫so now if we look at the generated template itself,
‫the again the s3 url was updated for us automatically.
‫Let's close this and now we're going to run
‫the cloudformation deploy command.
‫And let's do this right now, up cloudformation deploy.
‫And now we're waiting for the changes to be created
‫and when after it's going to be created,
‫it's going to be executed to our cloudformation.
‫So now let's go to cloudformation,
‫here it is. And if I refresh,
‫I see that now there is an update in progress.
‫So now a lot more things are going to be created.
‫I'll just pause until things are done.
‫The update is now complete, but now it turns out
‫that we have a lot more resources being created.
‫Remember before we only had two resources,
‫and now we have lambda function permissions,
‫IAM Role, API Gateway Stage, Deployment, and RestAPI.
‫So there's a lot of stuff that was created
‫for us automatically from again, a quite a simple
‫and readable template, that yaml file, like this.
‫So this is the whole power of SAM,
‫it really allowed us to generate
‫a complex cloud formation template
‫that was executed by cloud formation
‫and generated all these things for us.
‫Now, not just talk, let's just go
‫and see in API Gateway what happened.
‫So if we type API Gateway and go here,
‫we're going to see our API Gateway,
‫now there's a Hello World SAM API that was created for us.
‫And it turns out that we have a GET resource
‫on the hello path, right here,
‫so I click on GET, and we can see how
‫it was been configured, obviously we can
‫have more configurations parameters available in SAM,
‫but that was pretty minimal what we have.
‫And if I click on 'TEST', and click on 'Test' right here,
‫we should be seeing a internal server error,
‫so something is wrong in my function.
‫And so it turns out that, yes indeed, I forgot
‫a return keyword, basic mistake, but I'll keep it
‫because it's fine. I'll have a return missing,
‫and so that's okay, more practice for us.
‫Now we're going to run the commands again,
‫so I'll run the package command and deploy command,
‫all at once, one after the other,
‫so it goes a bit quicker. But so we see that we
‫can quickly iterate on the errors I make
‫in my cloud formation in my Serverless and Python files
‫just using this cloud formation package
‫and cloud formation deploy commands.
‫So now, basically, after this update we should be all good,
‫so let's go back to our cloud formation
‫to see if the update is complete.
‫Yes, the update is complete. So now if I go back
‫to my API Gateway and re-test our function,
‫now we get a good response called 'hello world!',
‫so now I've fixed my problem and we have an API Gateway.
‫We could even see that the stage was deployed,
‫so we have a Prod stage, and now we have an Invoke URL,
‫and so if I go to the slash Prod slash hello,
‫then we get 'hello world!'
‫and everything is working just fine.
‫So that's it, we've just added an API Gateway stage
‫to our SAM file in no time.
‫Let's see how we can add DynamoDB now.