How to Create Amazon Lambda Function with the Container Image (Dockerfile)

Dogukan Ulu
4 min readAug 4, 2023

--

Tech Stack

  • Amazon Lambda
  • Amazon ECR
  • Docker

Overview

  • While creating a deployment package for our Amazon Lambda Function, we have two options: zip file and Dockerfile. Since the zip file has a size limit, using a Dockerfile (container image) will be a better approach.
  • We are going to create our Docker image on the local machine, then deploy our image to Amazon ECR.
  • We should first create a local directory for our image to build. Every step will be handled in this directory.
  • We are going to first create a Python script that will be used as the Lambda function in this directory.
  • We have to create a requirements.txt file for all the libraries used in the Python script.
  • In the end, we will create a Dockerfile which will be used to build the image.

Build the Image

First of all, we can start with the Python script. I will not explain how to create a Python script since it might differ really much depending on the use case of the Lambda function. For example, you can see the below repository.

First of all, we have to get the requirements.txt file ready. For the below imports as an example, we can create the following requirements.txt file.

import io
import os
import pandas as pd
import boto3
import pymysql
from sqlalchemy import create_engine
pandas
boto3
pyarrow
sqlalchemy
pymysql

One main thing to mention about the Python script is combining all methods under one main method. In our example, the Python script’s name will be lambda_function.py and the main function will be lambda_handler.

After getting the Python script and requirements.txt ready, we can create the Dockerfile as below.

# python3.9 lambda base image. 
# Python version can be changed depending on your own version
FROM public.ecr.aws/lambda/python:3.9

# copy requirements.txt to container root directory
COPY requirements.txt ./

# installing dependencies from the requirements under the root directory
RUN pip3 install -r ./requirements.txt

# Copy function code to container
COPY lambda_function.py ./

# setting the CMD to your handler file_name.function_name
CMD [ "lambda_function.lambda_handler" ]

If we have any other Python script, any data file, or any other type of file, we have to copy all of them with COPY command. This sample will illustrate the logic of creating a simple Docker image.

We can use all the commands which can be used in the Dockerfile such as ENV for environmental variables. But defining them while creating the Lambda function will be a better practice.

We got all the necessary files ready on our local machine. Now it’s time to create the image.

Amazon ECR

We have to create a dedicated repository for our image and we are going to push our image into this repository.

Amazon ECR -> Repositories -> Create Repository

After giving a specific name to our repository, we can click on Create repository. Be careful that our repo will be private. On the main page of our repository, we have to click on View push commands. Starting from this point, we should have AWS configured locally with aws configure command and Docker up and running.

After all, we should run the commands that appear under View push commands one by one (We should be located inside the main working directory). These will:

  • Retrieve an authentication token and authenticate your Docker client to your registry.
  • Build the Docker image.
  • Tag our image so we can push the image to the repository we created. If we modify the script or anything else, we should modify the tag.
  • Push the image to our newly created repository.

Pushing the image will take a while to complete. After the push, we can see our image with its tag (2.2 for example).

We created the image and pushed it to the AWS repository successfully. Now it’s time to create the Lambda function

Amazon Lambda Function

We have to create our Lambda function from the Docker image by following these steps:

Lambda -> Functions -> Create Function -> Container image

  • Function name: We can give whatever name we want to our function.
  • Container image URI: Browse for the Docker image we recently created and choose it.
  • Architecture: Depending on the machine we are using. (arm64 for Mac M1 chip for example)
  • Change default execution role: We can choose the suitable IAM role in case we need it.
  • We can leave advanced settings as default.

Then, we can click on Create function and we have our function ready now. We don’t have to add any extra layer for any dependency. In case we have to modify the script or requirements, we have to modify them locally. Then, we have to run the commands to build, tag, and push our image once more. This time, it will take a shorter period of time since some layers already exist in the repository.

Hope it helps, thanks for reading :)

Please reach out via Linkedin and Github, all comments are appreciated 🕺

--

--