Creating an API Gateway using AWS CDK with Lambda Integration.

Hamza Shabbir
4 min readSep 18, 2022

--

Amazon API Gateway is a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and build secure APIs. Using API Gateway you can create RESTful APIs and WebSocket APIs. In this article, we will see an how we can use AWS Cloud Development Kit (CDK) to create a REST API with a Backend Lambda Integration.

AWS CDK

First, we will create a CDK application in an empty directory and activate the virtual environment and install dependencies.

cdk init app — language python
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
cdk synth

Once your stack is synthesizing properly it means our environment is correctly set up. Now we will create our API Gateway and Create a resource (path) with GET method. Our API Gateway will have a Lambda Function Integration. The client request will be sent to Lambda Function Via API Gateway and Lambda will process it and return a valid response i.e. 200.

Client — Server (Lambda) Architecture via API-Gateway

First, we will create a function that will return the Lambda function and a second function to create the IAM Role that will be assigned to the Lambda function.

Function to return the Lambda function
Function to create the IAM Role for the Lambda function.

Now create the IAM Role from the lamdarole function. Then create the Lambda function from the createfunc function and the lamrole object will be passed as a parameter to the createfunc function. The function will take the Path “./resources” and handler name and function name( handler name.function name) as parameters.

Now we will create an API Gateway with GET method and the Lambda function created will have an Integration with the API Gateway. A resource “id” is added which will be in the URL path of API Gateway.

Complete Stack File Code

Now right the code for the Lambda Function. Create a new folder in our case “resources” in the project directory. Remember to create it outside the folder containing the stack.py file.

📦apigateway
┣ 📂apigateway
┃ ┣ 📂__pycache__
┃ ┃ ┣ 📜__init__.cpython-38.pyc
┃ ┃ ┗ 📜apigateway_stack.cpython-38.pyc
┃ ┣ 📜__init__.py
┃ ┗ 📜apigateway_stack.py
┣ 📂cdk.out
┃ ┣ 📜ApigatewayStack.assets.json
┃ ┣ 📜ApigatewayStack.template.json
┃ ┣ 📜cdk.out
┃ ┣ 📜manifest.json
┃ ┗ 📜tree.json
┣ 📂tests
┃ ┣ 📂unit
┃ ┃ ┣ 📜__init__.py
┃ ┃ ┗ 📜test_apigateway_stack.py
┃ ┗ 📜__init__.py
┣ 📜.gitignore
┣ 📜README.md
┣ 📜app.py
┣ 📜cdk.json
┣ 📜requirements-dev.txt
┣ 📜requirements.txt
┗ 📜source.bat
┣ 📂resources
┃ ┗ 📜apilambda.py
Lambda Function Code

Now run the following commands. “cdk synth” convert the CDK Stack file to Cloud Formation Template and “cdk deploy” will deploy your Cloud Formation Template to AWS.

Note: If you are deploying for the first time you will need to bootstrap your account. For more information see the following link. https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html

cdk synth 
cdk deploy

Once cdk is deployed you will get a public url endpoint something like this with your resource in the url path.

https://abcd.efghapi.us-east-1.amazonaws.com/prod/{id}

When you will click this URL you will see the response coming from the Lambda Function.

API Gateway Response

If you find this Blog Helpful consider following me on GitHub for more projects related to AWS CDK.
https://github.com/hamzashabbir11

--

--

Hamza Shabbir

Backend Developer @Markaz Technologies (YC-W22) 🚀☁️ | AWS Community Builder👷‍♂️