1. 9
    Attach an API Gateway to a lambda function deployed with AWS CDK
    3m 29s
⚠️ This lesson is retired and might contain outdated information.

Attach an API Gateway to a lambda function deployed with AWS CDK

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 6 months ago

Serverless technologies like AWS Lambda allow us to build our applications out of small, independent functions that can be called based on events such as an API call.

By default, it's not possible to call a lambda function from the Internet - it's safely stored within AWS cloud, following the principle of least privilege.

In order to create a REST API to call a lambda function we need to use API Gateway. Luckily, with AWS CDK, we can create a LambdaRestApi in 3 lines of code and this is exactly what we're going to learn in this quick lesson!

Instructor: [0:00] Our Hello Lambda function has been deployed to AWS, but there's a slight problem. The problem is that apart from the test event inside of the AWS console -- I can do that over here. Click on test and call this function -- there's no other way of calling that function.

[0:14] This is because we don't have any triggers associated with this function. For instance, I am not able to call this function via a GET request. In order to do that, we're going to attach an API gateway so our function can be called via an API.

[0:26] A quick note about Amazon API Gateway. Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale. To make it simple, API Gateway allows us to call our Lambda function from the Internet. This is honestly exactly what we want.

[0:42] First, open up the terminal, enter run npm install --save @aws-cdk/aws-apigateway. Hit Enter. Once this is done, go to our stack and import * as apigateway from '@aws-cdk/aws-apigateway'. Let me save that.

[0:57] Next, we are going to use the API Gateway to create a REST API for our Lambda function. To do that, type New, API Gateway, Lambda REST API. We can see that over here that this is going to define an API Gateway REST API with AWS Lambda proxy integration.

[1:11] This is exactly what we want because we would like to be able to call a Lambda function through this API Gateway and this is going to configure a bunch of things for us.

[1:19] Next up, the first argument is going to be this because I am creating this construct within the context of this CDK stack. Next up, I'm going to call it an endpoint and lastly the only prop that is required here is the handler. The handler in this sense means the default Lambda function that handles all requests from this API. I'm going to set this handler to our Hello Lambda function.

[1:39] Now, the REST API is ready to be deployed. Before we do that, let's go to our Lambda function. Inside of this body, I'm going to also add a "You've hit event path." This event path is going to change depending on the asterisk that we are going to hit in order to call our Lambda function.

[1:55] Next, open up the terminal and run cdk diff to see what we are about to deploy. We can see that over here in the Resources section that just because we added those three lines, all those resources are going to be created and configured for us.

[2:07] This is the power of CDK, because what we wanted to have is a Hello Lambda function that can be triggered via the Internet and all those things are going to be configured and we don't have to deal with that.

[2:17] Next, run cdk deploy in order to deploy this stack to AWS. Because we are about to allow the API gateway to invoke this Lambda function, CDK is going to ask us whether we are OK to deploy those changes and yes, we are OK to do that. I'm going to hit Yes.

[2:31] Once this is done, we can see in the Output section that our endpoint has been created and it is available under this address. Once we visit this address in a browser, we can see that our Lambda function has been successfully executed.

[2:42] Also, we see that you've hit /, because if I add, "Hello world," over here, it's going to tell us that you've hit /hello/world. This is why we added this part over here. Right now, we're able to see two things.

[2:55] First up, we were able to attach an API Gateway in order to call our Lambda function from the Internet. Also, this Lambda function can behave differently, depending on the type of event that was sent to it. For instance, we can modify the behavior of this function depending on the path that we've decided to hit.

[3:10] One last thing, if we go to cloud formation to our stack and we first call down, we are able to see all those different resources that were created for us. If I scroll down to this Hello Lambda function, click on it, we can see in the designer that our Lambda function has an API Gateway attached to it. If I scroll down a bit, we are going to be able to see the address of our endpoint.

Matt
Matt
~ 4 years ago

In order to see the body returned by your lambda function in the browser, the Content-Type needs to be changed from plain/text to application.json

Leaving it as plain/text will just download a file to your computer instead of displaying the message in the browser. I believe this step was skipped in this lesson.

Matt
Matt
~ 4 years ago

Also, if you do not see the api endpoints listed when scrolling down, you need to click on the "API Gateway" box within the designer.

Todor
Todor
~ 4 years ago

Running "cdk diff" with the packages at version 1.55.0 works fine. However the newest (08.08.2020) version 1.57.0 would throw an error:

$ cdk diff

unable to determine cloud assembly output directory. Assets must be defined indirectly within a "Stage" or an "App" scope"

Tomasz Łakomy
Tomasz Łakomyinstructor
~ 4 years ago

Thanks for the notice, I’ll take a look as soon as I can :)

Tunca Tunc
Tunca Tunc
~ 4 years ago

Tomasz, thanks for the great course. I have to do yarn build, after that cdk diff shows diff.

Markdown supported.
Become a member to join the discussionEnroll Today