Git repo with all code completed:https://github.com/chrishart0/aws-cdk-sam-demo
When using AWS CDK for your infrastructure as code, it can be a pain to figure out how to efficiently test your Lambda functions. Deploying your code to AWS with cdk deploy for every change is slow, having to run a deploy for every code change is not ideal. Luckily it is straightforward to use AWS SAM to test your function locally.
For scenarios where local dev isn’t feasible, get faster deployments to Lambda with
cdk deploy --hotswap
Steps 0-2 help you set up a fresh CDK project. If you already have a CDK project which creates a lambda function and API Gateway, skip ahead to step 3.
- NPM Installed
- Python Installed
- If you don’t have AWS CDK installed follow this guide: https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html
- If you don’t have AWS SAM installed, follow this guide: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html
Note: If this is your first time using AWS CDK with Python, I recommend you try out the CDK Workshop from AWS: https://cdkworkshop.com/30-python/20-create-project/100-cdk-init.html
⚠️ Optionally: you can clone the completed repo here and skip to step 3
Create a directory for your project and cd into it
$ mkdir aws-sam-cdk-demo $ cd aws-sam-cdk-demo
Then, initialize your CDK project
$ cdk init app --language python
Create and source a python virtual environment
$ python3 -m venv .venv $ source .venv/bin/activate
Install CDK requirements
$ pip install -r requirements.txt
Run CDK synth to ensure everything is setup correctly
$ cdk synth
In this example, I’ll be making an API that provides a greeting from a list of greetings I have hardcoded.
Create a directory for the lambda code and create a file for the code
$ mkdir lambda $ touch lambda/greeting_generator.py
Go ahead and add some code to your function. You can copy the code from my function and tweak it as you see fit:< https://github.com/chrishart0/aws-cdk-sam-demo/blob/master/lambda/greeting_generator.py>
Run the command to install the the lambda and API Gateway constructs.
$ pip install -r requirements.txt
Open the stack file.
If you aren't familiar with CDK then the stack file may be confusing, this is where infrastructure is defined in a CDK project. See here for reference stack file. The stack file was created by
cdk init, it follows the naming scheme
- Example: Parent folder which init was run in is
- Example in GitHub: https://github.com/chrishart0/aws-cdk-sam-demo/blob/master/aws_sam_cdk_demo/aws_sam_cdk_demo_stack.py
Import lambda at the top of the stack file referenced above.
from aws_cdk import ( core, aws_lambda as _lambda, aws_apigateway as apigateway, )
Add code to your stack to generate a lambda function.
greeting_function = _lambda.Function( self, 'greeting_handler', runtime=_lambda.Runtime.PYTHON_3_9, code=_lambda.Code.asset('lambda'), handler='greeting_generator.handler', )
Add code for the API Gateway. Ensure the handler is the same as the variable for the lambda function.
greeting_apg = apigateway.LambdaRestApi( self, 'greeting_endpoint', handler=greeting_function, description="Greeting API endpoint", )
Tip: Add links back to the CDK docs in your code; this will save you a lot of time hunting for the right page in the future.
cdk synth to ensure everything compiles. You should now see more resources in the CloudFormation output in your terminal.
Here is where things start to get a little… nutty 🐿️ (get it, like the SAM squirrel mascot???)
If you already have a CDK application with a lambda function start here!
Synthesize a template and write it to
$ cdk synth --no-staging > template.yaml
Test the API with SAM
$ sam local start-api
You can also test individual invocations with simulated events, this is ideal if you are not using API Gateway.
$ sam local invoke functionName -e event.json
You can now use this very simple strategy to test your Lambda + API Gateway in your own CDK project or use this example project as a starting point.