Amazon API Gateway is a fully managed service that makes it easy for developers to publish, maintain, monitor, and secure APIs at any scale. Create an API to access data, business logic, or functionality from your back-end services, such as applications running on Amazon Elastic Compute Cloud (Amazon EC2), code running on AWS Lambda, or any web application.
AWS CDK (Cloud Development Kit) is a framework that allows developers to use familiar programming languages to define AWS cloud infrastructure and provision it. CDK provides the Constructs cloud component that covers many of the AWS services and features. It helps us to define our application infrastructure at a high level.
In this post, we will create an API Gateway using AWS CDK and connect it to lambda which we were developed in AWS Lambda Using CDK blog.
There are two types of integration of API Gateway with Lambda that can be done. We can have the Lambda proxy integration or the Lambda custom integration.
Lambda Integration With Proxy
A very common practice is to use Amazon API Gateway with AWS Lambda as the backend integration. The LambdaRestApi construct makes it easy.
Edit the file lib/cdk-greetapp-stack.js to create an AWS Gateway resource as shown below.
// defines an AWS API Gateway resource With Proxy
const apiGatewayWithProxy = new gateway.LambdaRestApi(
this,
"greetApiWithProxy",
{
handler: greet,
}
);
The above code defines a Gateway REST API that routes all requests to the specified AWS Lambda function greet.
After adding the above code, make sure the resources are as expected by using the command cdk diff
and once, verified, deploy the stack using cdk deploy
.
Once deployment is done, go to the AWS console and you see greetApiWithProxy API
Now, if you check the Resources, you can see the proxy resource and ANY method, which means all requests are routed to AWS Lambda function greet.
Click on ANY and click on Test and add the below details and you can see the result as shown below.
Lambda Integration Without Proxy
Let us create one more API without proxy integration to lambda. In this, implementation, we need to explicitly define the API model and make proxy as false.
// defines an AWS API Gateway resource Without Proxy
const apiGatewayWithoutProxy = new gateway.LambdaRestApi(
this,
"greetApiWithoutProxy",
{
handler: greet,
proxy: false,
}
);
const greetResources = apiGatewayWithoutProxy.root.addResource("greet");
const greetResource = greetResources.addResource("{name}");
greetResource.addMethod("GET");
The above code defines a Gateway REST API that routes the GET requests to the resource /greet/{name} of AWS Lambda function greet.
After adding the above code, make sure the resources are as expected by using the command cdk diff
and once, verified, deploy the stack using cdk deploy
.
Once deployment is done, go to the AWS console and you see greetApiWithoutProxy API
Now, if you check the Resources, you can see the proxy resource /greet/{name} and GET method.
Click on GET and click on Test and add the below details and you can see the result as shown below.
Conclusion
In this blog post, we saw how to create an AWS API Gateway by using AWS Cloud Development Kit. We also saw various ways in which we can integrate the API Gateway with Lambda. We saw how to create a proxy resource and custom resource. The code repository link is here