This example app builds a simple flask application that will run successfully at AWS Lambda or another container registry service like ECS. Notice the /app/run.sh
shell script which tests for the presence of a Lambda environment variable. If the variable is present the shell starts gunicorn as a daemon and then calls the AWS Lambda Runtime Interface Client (RIC). Otherwise the shell runs gunicorn the same way as in 2-flask-lambda-container.
Follow the instructions below to build and run the application at AWS Lambda.
- Build your container locally:
docker build -t flask-dual-app:latest .
- Tag your container for deployment to ECR:
docker tag flask-dual-app:latest \
<aws-account-id>.dkr.ecr.us-east-1.amazonaws.com/flask-dual-app:latest
- Login to ECR
aws ecr get-login-password --region us-east-1 \
| docker login --username AWS \
--password-stdin <aws-account-id>.dkr.ecr.us-east-1.amazonaws.com
- Create a repository at ECR
aws ecr create-repository \
--repository-name flask-dual-app \
--image-scanning-configuration scanOnPush=true \
--region us-east-1
- Push the container image to ECR
docker push <aws-account-id>.dkr.ecr.us-east-1.amazonaws.com/flask-dual-app:latest
- Create the Lambda function (note: you'll have to create an execution role first).
aws lambda --region us-east-1 create-function \
--function-name flask-dual-app \
--package-type Image \
--role arn:aws:iam::<aws-account-id>:role/supernova-execution-role \
--code ImageUri=<aws-account-id>.dkr.ecr.us-east-1.amazonaws.com/flask-dual-app:latest
- Invoke the Lambda function
aws lambda --region us-east-1 invoke \
--function-name flask-dual-app \
--invocation-type Event \
--payload '{ "foo": "bar" }' \
outfile.txt