AWS lambda api gateway deployment via AWS CDK
Prerequisites
- Linux environment( tested on MacOS M1 )
- Python 3.8
- CDK installed Instructions
- Code for this reference example
Creating Virtual Environment for installing the Python dependencies
Installing the Python dependencies
requirements-dev.txt file contains the PyTest PyTest is being used for a simple Unit Test use case against our Infrastructure
Initialising the CDK environment
Preview the preview of the compiled Cloudformation template ( baed on Python code)
Run PyTest tests
Run unit tests in tests/unit/test_cdk_multienv_setup_stack.py file :
This checks if API Gateway Method exists in compiled Cloudformation template & Lambda has Function name OrderGet and runtime of python 3.8
Deploy the stack to your AWS account
After deployment you will see the Outputs section :
In this output, the value of “cdk-multienv.orderapiEndpointE2C47C71” is the API Gateway Stage endpoint URL.
We have a “Get” method defined at path “/order” in cdk_multienv_setup/cdk_multienv_setup_stack.py
Testing the deployment
Succesful response would return :
This sends the request to API Gateway Endpoint, which forwards it to our Lambda function
Links :
https://docs.pytest.org/en/7.1.x/ https://docs.aws.amazon.com/cdk/v2/guide/work-with-cdk-python.html