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 .venvInstalling the Python dependencies
pip install -r requirements-dev.txt
pip install -r requirements.txtrequirements-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 bootstrapPreview the preview of the compiled Cloudformation template ( baed on Python code)
cdk synthRun PyTest tests
Run unit tests in tests/unit/test_cdk_multienv_setup_stack.py file :
pytestThis 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 deployAfter 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/orderSuccesful response would return :
Transaction: id-12314124124124This 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