This is a CDK Python project to show how to deploy AWS Lambda function with a custom container.
It is useful to use the custom container when you need to more than two different run time enviroments for AWS Lambda function. For example, if you try to use a python package wrapping Java package (e.g., KoNLpy) in the AWS Lambda function, you would need both Python and Java run time.
The cdk.json
file tells the CDK Toolkit how to execute your app.
This project is set up like a standard Python project. The initialization
process also creates a virtualenv within this project, stored under the .venv
directory. To create the virtualenv it assumes that there is a python3
(or python
for Windows) executable in your path with access to the venv
package. If for any reason the automatic creation of the virtualenv fails,
you can create the virtualenv manually.
To manually create a virtualenv on MacOS and Linux:
$ python3 -m venv .venv
After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.
$ source .venv/bin/activate
If you are a Windows platform, you would activate the virtualenv like this:
% .venv\Scripts\activate.bat
Once the virtualenv is activated, you can install the required dependencies.
(.venv) $ pip install -r requirements.txt
At this point you can now synthesize the CloudFormation template for this code.
(.venv) $ export CDK_DEFAULT_ACCOUNT=$(aws sts get-caller-identity --query Account --output text) (.venv) $ export CDK_DEFAULT_REGION=$(aws configure get region) (.venv) $ cdk synth
Use cdk deploy
command to create the stack shown above.
(.venv) $ cdk deploy
To add additional dependencies, for example other CDK libraries, just add
them to your setup.py
file and rerun the pip install -r requirements.txt
command.
Delete the CloudFormation stack by running the below command.
(.venv) $ cdk destroy
cdk ls
list all stacks in the appcdk synth
emits the synthesized CloudFormation templatecdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk docs
open CDK documentation
Enjoy!
When you run Test
in the lambda function, you can see the result like this:
You can create the container image with an AWS base image for Lambda by following the instructions.
-
On your local machine, create a project directory for your new function.
-
Create a directory named
app
in the project directory, and then add your function handler code to theapp
directory. -
Use a text editor to create a new Dockerfile.
In this project, you can find the examplary application in thecustom_container/app
directory.$ cd custom_container/app $ tree ./ ./ ├── app.py ├── Dockerfile └── requirements.txt 0 directories, 3 files
-
Build your Docker image with the docker build command. Enter a name for the image. The following example names the image
hello-world
.docker build -t hello-world .
-
Start the Docker image with the
docker run
command. For this example, enterhello-world
as the image name.docker run -p 9000:8080 hello-world
-
(Optional) Test your application locally using the runtime interface emulator. From a new terminal window, post an event to the following endpoint using a curl command:
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
In the following commands, replace 123456789012
with your AWS account ID and set the region
value to the region where you want to create the Amazon ECR repository.
- Authenticate the Docker CLI to your Amazon ECR registry.
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
- Create a repository in Amazon ECR using the
create-repository
command.aws ecr create-repository --repository-name hello-world --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
- Tag your image to match your repository name, and deploy the image to Amazon ECR using the docker push command.
docker tag hello-world:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest