Attach an API Gateway to an AWS Lambda Function Deployed with AWS CDK

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

Social Share Links

Send Tweet

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 an AWS 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 an AWS 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 World function has been deployed to AWS but there's a slight problem. The problem is, that apart from this test event over here inside the AWS console, I have no other way of actually calling this function, that is because we don't have any triggers that are associated with this function.

[0:15] For instance, I am not able to call this function via a GET request. In order to do that, we are going to attach an API Gateway so that our function can be called via an API. 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.

[0:38] Using API Gateway, we can create a RESTful API which can be backed, for instance, by an AWS Lambda function. The first million API calls are free within the AWS free tier so this is a perfect fit for our use case.

[0:52] Let's go back to our stack. To deploy an API Gateway in our CDK stack, first of all, I'm going to import apigateway from aws-cdk-lib/aws-apigateway. Next, I'm going to create a new endpoint which is going to be a new apigateway.lambda REST API which, again, this is a construct. The first argument is going to be this.

[1:14] Second argument, I'm going to call it endpoint, and the third argument are the props. There's only one prop that's actually required in order to create this API and that is the handler. A handler is an instance of IFunction.

[1:28] Handler is going to be equal to our HelloWorldFunction that we have defined over here. What's going to happen is that this construct is going to create a brand new API Gateway which is going to be backed by this HelloWorldFunction. Every single request, whether it's GET or POST, is going to trigger this Lambda function.

[1:47] We can actually see that if we open up the terminal and run cdk diff. This is going to show us that, first of all, those three lines are going to deploy quite a lot of stuff in our AWS account that we don't have to configure. Secondly, over here, we can see that any method that is going to be invoked via this API is actually going to trigger our HelloWorldFunction. This is exactly what we want.

[2:11] Let me go ahead, clear the terminal, and I'm going to run cdk deploy. After a successful deployment of our CDK stack, we can see our endpoint over here. If we just copy that and let's call this Endpoint. If I send a request to our newly created endpoint, I'm going to see a message "Hello World" over here, which is exactly the same message that we have defined inside of our Lambda function.

[2:36] In short, our CDK stack now creates an API Gateway which is backed by our serverless AWS Lambda function.