Make and Deploy a Python App Docker Image to AWS Lambda

Contents:

Developer for NOCODING AI
5 min readAug 9, 2022

What is a docker image and why use it?

Docker is an open platform for developing, shipping, and running applications. It enables you to separate your applications from your infrastructure so you can deliver software quickly [1]. Docker is used to create, run, and deploy applications in containers. A Docker image contains application code, libraries, tools, dependencies, and other files needed to make an application run. Docker images have multiple layers, with the Docker image being a read-only image and a writable layer added on top of it.

When a Docker image is deployed to a Docker environment, it can be executed as a Docker container. The best advantage of Docker images is their reusability in any host environment. Developers can just take the static image layers from a project and use them in another without wasting time recreating the image from scratch [2]. On the other hand, a Docker container is a virtualized runtime environment that is isolated from the underlying hardware. A Docker image is like a snapshot/record of a Docker container at a specific point in time.

Create a Docker image with AWS and test it locally

This tutorial assumes that you have already:

Alright, now that we have everything set up, let’s get our hands dirty with writing some code.

AWS provides sample SAM applications that have most of the things configured, such as a template file, and Dockerfile, which makes it super easy to build images and deploy them to Lambda. The AWS Serverless Application Model (AWS SAM) is an open-source framework for building serverless applications. It provides shorthand syntax to express functions, APIs, databases, and event source mappings. With just a few lines per resource, you can define the application you want and model it using YAML. During deployment, AWS SAM transforms and expands the SAM syntax into AWS CloudFormation syntax, enabling you to save time and build, test, and deploy serverless applications faster [3]. Here is a command to run to initialize a python lambda app (with everything already configured):

sam init

After running the above command, you’ll be asked to make a few selections. Here is what it will look like and what options you should go with for this tutorial:

This command creates a directory with the name that you provided as the project name. The contents of the project directory are similar to the following:

medium-sam-app-tutorial/
├── README.md
├── events/
│ └── event.json
├── hello_world/
│ ├── __init__.py
│ ├── app.py #Contains your AWS Lambda handler logic.
│ ├── Dockerfile #Contains commands to build the image.
│ └── requirements.txt #Contains any Python dependencies the application requires, used for sam build
├── template.yaml #Contains the AWS SAM template defining your application's AWS resources.
└── tests/
└── unit/
├── __init__.py
└── test_handler.py

There are three especially important files:

  • template.yaml: Contains the AWS SAM template that defines your application’s AWS resources
  • hello_world/app.py: Contains your actual Lambda handler logic. This is where write the main body of your application.
  • hello_world/requirements.txt: Contains any Python dependencies that the application requires, and is used for sam build that will be explained shortly. Feel free to include any other libraries you’d like to install to this .txt file
  • Dockerfile: Contains commands to convert this Python app to an executable Docker image. This is where you write/modify the logic for creating the image

Next, enter the app by running the cd medium-sam-app-tutorial command (paste your app’s name instead of medium-sam-app-tutorial). Build your app:

cd medium-sam-app-tutorial
sam build

and here is the prompt you’ll see if your buildis success:

To run your Docker image locally, run the following command:

sam local invoke

and you should receive this message on your console:

Additionally, you can configure and pass an event file with parameters.

sam local invoke -e events/event.json

You can look up other possible options and parameters that you can pass here.

Deploy the Image to Lambda

Deploy your image with the --guidedoption to configure some of the settings:

sam deploy --guided

and you’ll be prompted with configuration questions. You can press Enter for all the prompts for the sake of this tutorial. But you can also customize them. And that’s it, you’ve successfully created a Docker image of your python app and deployed it on Lambda. If you go to your Lambda functions console and search the name of the app (in my case medium-sam-app-tutorial), you’ll see the deployed app:

If you enter the function, you’ll notice that there is a default REST API trigger attached to the function. You can make requests to the provided endpoint and communicate with the Lambda function from a frontend client:

Additional Info

Add anything additional here!

Contributed by Intizar Tashov :)

--

--