How to make use of Docker and SAMCLI to package Lambda

How to make use of Docker and SAMCLI to package Lambda

Did you ever face a scenario that your AWS Lambda function is dependent on Python package other than AWS SDK? then I am going to present you with options that you can utilize to package your Lambda function.

I want to keep this post scope to Lamda function and it dependent Python packages, so that I can target a particular issue as below,

Executed a Lamda function with test payload

"errorMessage": "Unable to import module 'lambda_function': No module named 'requests'"

On my hunt to resolve the issue, I hit the AWS documentation page and it says to create a deployment package.

What is a Package?

Its a ZIP archive of our Lambda function and its dependencies, you can either upload the zip file or provide the S3 path to Lambda Function.

Using Docker

Install Docker on your machine.

Please follow the link for Docker Installation on Windows.

Pull and run the Docker image which replicates a live Lambda execution environment on AWS.

docker run -it --rm --entrypoint bash -e ODBCINI=/opt/odbc.ini -e ODBCSYSINI=/opt/ lambci/lambda:build-python3.7

You can play with Python build based on the run time selected for your Lamda function.

Install the required packages into a folder and copy the lambda function code to the same folder

bash-4.2# pip install --target ./package requests 

bash-4.2# cd package/


bash-4.2# touch lambda_function.py

create a zip archive

bash-4.2# zip -r9 ../func.zip .

upload the zip file to S3 bucket

bash-4.2# aws s3 cp ./func.zip s3://<bucket-name>/func.zip (execute aws configure to configure credentials)



Using SAM ( Serverless Application Model)

To set up a SAM environment you can follow the link SAM setup.

You need to follow the below template to arrange your files,

E:\sam_deploy                        directory structure to follow                 

|   template.yaml
|
\---alpha_app
        app.py
        requirements.txt
        __init__.py


app.py is your Lamda function.

requirements.txt is where you list the python packages that your lambda depends.

template.yaml has a much bigger scope, here I defined what is required to generate a package.

you can grab a sample structure from Git.

Now, you completed the directory set up and SAMCLI is installed, run the below command to build the dependencies from the root directory.

sam build --use-container --

to package application/lambda function, run the command

sam package --s3-bucket <bucket_name>

an autogenerated name will be given to the zip archive, rename to your preferred name with aws s3 mv command

aws s3 mv s3://<bucket_name>/ea43833dad4ea5207774e9fbb34ced32 s3://<bucket_name>/<package-name>

Conclusion

I finally narrowed down the approach to easily build python packages and while installing or setting up you will come across the interconnect between Docker, SAMCLI, and WSL ( Windows Subsystem for Linux).

Powershell can be utilized to run the commands it is integrated into VS code IDE.

Please comment, if you face any kind of issue with the above approach.

Follow me @ LinkedIn

Data Engineering @ GitHub

要查看或添加评论,请登录

社区洞察

其他会员也浏览了