I am looking for a full round-trip example of:
- creating a custom docker image (that lambda will use - I have a working Dockerfile)
- pushing the image to the AWS container registry
- Having AWS lambda manually invoked that will pull the image from the AWS container registry (and instantiate the container)
- and invoke a task in the container that will run (a specific bash script)
- then have all resources released (container removed, lambda completes), after container runs
Is this possible? Is there an example anywhere?
I managed to deploy a Lambda for instantiating Docker containers in this manner:
0.Make sure Docker Desktop is installed on your PC and is running (https://docs.docker.com/engine/install/)
1.Set up your AWS account credentials by running
aws configure
in the terminal2.Prepare a python script, named main.py. Include an output function in your script similar to the following:
3.Prepare a list of python modules you need to have installed inside the container and put them in a file named requirements.txt.
4.Prepare a SAM template, named template.yaml, of a similar format to:
(Refer to https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html for a list of other properties you might need for your lambda function.)
5.Prepare a Dockerfile in a similar format:
In the terminal, run
sam build -u
to build the Docker image.In the terminal, run
sam deploy -g
then follow on-screen steps to generate a configuration file called samconfig.toml and deploy the lambda function and ECR Docker image to AWS.Once deployed, in the Lambda console on the AWS site you can test the new lambda function. As the lambda has been deployed as a container image, each time it is invoked the Docker container will be instantiated.