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
python3 -m venv .venv
Installing the Python dependencies
pip install -r requirements-dev.txt
pip install -r requirements.txt
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
cdk bootstrap
Preview the preview of the compiled Cloudformation template ( baed on Python code)
cdk synth
Run PyTest tests
Run unit tests in tests/unit/test_cdk_multienv_setup_stack.py file :
pytest
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
cdk deploy
After deployment you will see the Outputs section :
✨ Deployment time: 43.47s
Outputs:
cdk-multienv.orderapiEndpointE2C47C71 = https://exampleyteye.execute-api.us-east-1.amazonaws.com/prod/
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
curl https://exampleyteye.execute-api.us-east-1.amazonaws.com/prod/order
Succesful response would return :
Transaction: id-12314124124124
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