Infrastructure as Code: How to Deploy an Python Lambda Function using the AWS Cloud Development Kit (CDK)
We can use the Python version of CDK to allow us to manage projects, bootstrap or migrate projects to different accounts.
In this tutorial, we re-created the Python Lambda function and API Gateway from the previous tutorial using "infrastructure-as-code" (with AWS CDK) and deployed it.
All of the resources we created last time including the Lambda and Gateway function we refer to as Infrastructure
Luckily there are tools that exist which allow us to create, deploy and maintain all of this infrastructure using code.
Most of the time this infrastructure is defined using markup formatting language such as json or yaml
But there are frameworks that allow us to do this in a scripting language such as Python or Typescript which is easier because you have thigns like type-referencing, IDE completion, loops, conditions, etc.
This is also great because instead of having to manually click things in our console, we end up with a bunch of code that we can use to deploy our infrastructure with.
Popular Infrastructure-as-Code (IAC) Frameworks include Terraform, Ansible, Pulumi and AWS CDK. This makes the most sense since we are already on a AWS stack.
CDK will allow us to find and deploy our Lambda function & API Gateway integration as code.
- 1. AWS CLI
- 2. NodeJS (with npm)
Once installed, try the following commands so that everything is setup to be working properly:
aws sts get-caller-identity
^ this prooves that you have AWS CLI configured and that you are using the correct account
npm --version
to check that you have the right node version installed
Install the latest cdk version:
npm install -g aws-cdk@latest
check that it is working by typing:
cdk --version
Next run a boostrap CDK
cdk bootstrap aws://{account#}/{region}
Once you run this it will create something called a cloud formation stack in your account for that region. A cloud formation stack is essentially logical grouping for your resources.
Create a new folder:
mkdir infrastructure
Navigate to that folder:
cd infrastructure
Type:
cdk init --language python
You will see that the last command made a bunch of files for us. When we deploy these files it will create another cloud formation stack in our account.
Now we have to add the Lambda function and API Gateway from the first tutorial to our resources stack.
First, add the below to the requirements.txt file:
aws-cdk.core
aws-cdk.aws_lambda
Install the modules using this command:
pip install -r requirements.txt
Once the dependencies have been installed we can move over our function from the AWS Lambda->functions console into a folder within /infrastructure called /compute.
In the infrastructure_stack.py file, import the lastest aws_lambda package from the documentation and create a new fucntion object:
from aws_cdk import aws_lambda
# The code that defines your stack goes here
random_drink_function =aws_lambda.Function(
self, # the logical resource that will be the owner of this lambda function
id="RandomDrinkFunctionV2", # give it a random name
code=aws_lambda.Code.from_asset("./compute/"), # where Lambda finds the code, pointed to our compute folder
handler="random_drink_lambda_handler", # specified handler, which is the python function and package we want Lambda to use when it is triggered
runtime=aws_lambda.Runtime.PYTHON_3_12 # tell it what runtime to use
)
Finally run:
cdk deploy
Head to the AWS Lambda function console to see if your new function with the code is stored.
Now that we have our Lambda function in the cloud, we can add our API integration
Update your infrastructure_stack.py file to include gatewayv2"
from aws_cdk import (
aws_apigatewayv2 as apigwv2,
aws_apigatewayv2_integrations as integrations,
)
Re-run:
pip install -r requirements.txt
Run:
cdk deploy
Go back to AWS Kambda -> Functions and you should find the API Gateway trigger already connected to your Lambda function. When you open the URL provided it should work.