How can I use Amazon Linux 2 AMI native binary packages with Lambda?

4 minute read
0

I want to create an AWS Lambda function that contains binary shared library dependencies and runs on an Amazon Linux 2 Amazon Machine Image (AMI). Is there a way that I can use Amazon Linux 2 AMI software packages without recompiling the package?

Short description

The Amazon Linux 2 AMI and Amazon Linux AMI that are used for Lambda runtimes don't support all the same native binary packages.

As a workaround, you can first build a Lambda container image using the Docker command line interface (CLI). Then, use the container image to run your Lambda function.

Resolution

Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you're using the most recent AWS CLI version.

The following instructions describe how to install a curl library into a Docker image (Lambda container image). Then, how to use the image to run a Python version 3.9 Lambda function. For other coding languages, adapt the steps accordingly.

1.    If you haven't done so already, install the Docker CLI.

2.    Create a project directory named lambda-project.

3.    Use a text editor to create a Dockerfile in the lambda-project directory that includes the following example code:

FROM public.ecr.aws/lambda/python:3.9

# Copy function code
COPY lambda_function.py ${LAMBDA_TASK_ROOT}

# Install the binary packages using yum install
RUN yum install -y curl

# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "lambda_function.lambda_handler" ]

4.    Use a text editor to create a lambda_function.py file in the lambda-project directory that includes the following example code:

import subprocess

def lambda_handler(event, context):
    my_output = subprocess.check_output(["curl","-X", "GET", "https://www.httpbin.org/get"], stderr=subprocess.STDOUT, shell=False)
    print(my_output.decode('utf8'))

Important: Make sure that you run all of the following commands (steps 5-10) in the lambda-project directory.

5.    Create the Lambda container image by running the following docker build Docker CLI command:

docker build -t hello-world

6.    Test the container image's setup by starting the container image locally. To start the container image locally, run the following docker run Docker CLI command:

docker run -p 9000:8080 hello-world 
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

7.    Authenticate Docker to your Amazon Elastic Container Registry (Amazon ECR) registry by running the following get-login-password AWS CLI command:

Important: Replace us-east-1 with your AWS Region. Replace 123456789012 with your AWS account ID.

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com

For more information, see Using an authorization token in the Amazon ECR User Guide.

8.    Create an Amazon ECR repository by running the following create-repository AWS CLI command:

Important: Replace hello-world with the name of your Amazon ECR repository.

aws ecr create-repository --repository-name hello-world --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE

For more information, see Step 3: Create a repository in the Amazon ECR User Guide.

9.    Tag your container image to push to your repository by running the following docker tag Docker CLI command:

Important: Replace 123456789012 with your AWS account ID. Replace us-east-1 with your AWS Region.

docker tag  hello-world:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest

10.    Push your container image to your repository by running the following docker push Docker CLI command:

Important: Replace 123456789012 with your AWS account ID. Replace us-east-1 with your AWS Region.

docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest

Your container image is now in your Amazon ECR repository. For more information, see Step 4: Push an image to Amazon ECR in the Amazon ECR User Guide.

11.    Create and test your Lambda function using the container image that you created. For instructions, see Creating Lambda functions defined as container images in the Lambda Developer Guide.


AWS OFFICIAL
AWS OFFICIALUpdated 2 years ago