AWS API-Gateway Using CDK

I am a Lead Engineer from India. Love to blog about serverless and help teams design and develop serverless architecture. An AWS cloud practitioner. Blogs about AWS Services utilising AWS CDK, CloudFormation, Terraform.
Search for a command to run...

I am a Lead Engineer from India. Love to blog about serverless and help teams design and develop serverless architecture. An AWS cloud practitioner. Blogs about AWS Services utilising AWS CDK, CloudFormation, Terraform.
No comments yet. Be the first to comment.
This series will cover provisioning Aws resources using Cloud Development Kit.
Amazon simple storage service (S3) is object storage built to store and retrieve unlimited amounts of data. We can use it to store various kinds of data. We can have data lakes to push the data into S3, we can collect the click events of a web applic...
Transformers, the backbone of many state-of-the-art NLP models such as BERT, GPT has revolutionized the way we approach natural language understanding tasks. One key innovation in transformers is their ability to handle entire sequences of tokens sim...

When an User prompts the questions to the LLM, it produces the response based on the training data. The pre-trained LLM data is its parametric data. It might not have an context specific to our query. LLM still produces an answer confidently. It is t...

In the fast-evolving field of artificial intelligence, Large Language Models (LLMs) have rapidly transformed how we approach language processing. These models, such as ChatGPT, Claude, and Bard, have proven their utility across a range of application...

Amazon S3 is a powerful, scalable storage service, but when multiple clients or processes attempt to write to the same S3 object simultaneously, things can get tricky. Since S3 doesn’t natively handle concurrent writes or provide object locking, it’s...

When working with distributed systems in a microservice architecture, we must always architect our services with failure in mind. When the transaction happens between a microservice, we have to handle the failures. The Saga pattern can be used in a f...

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.
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.

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.

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