Securely deploy your Lambda function using Python built with the same options as Amazon Linux

Problems caused by the Lambda for Python deployment environment

This is especially common when using libraries that use C extensions, but different build-time options can cause problems and errors. Especially the character code ... In the observed range, CircleCI environment uses ʻUCS2, and Lambda (Amazon Linux) standard uses ʻUCS4, so an error may occur. I don't remember what kind of error it was because it was a time of trial and error, but I have experienced that the Python code that I put in my Ubuntu environment with pyenv did not work on Lambda. ..

How to solve the problem

Use Amazon Linux

First, there is a method of installing and deploying the pypi library etc. in an environment independent with virtualenv on Amazon Linux. The easiest and most reliable. However, it is not good to set up one EC2 just for that purpose, and Jenkins etc. will be used, but the operation is also troublesome.

Build Python with the same options as Amazon Linux

You can use this anywhere. However, it is troublesome to take care not to pollute the existing environment, and CI as a Service is thrown away every time, so it takes time to build each time it is deployed. There is a way to burn the image using a service that allows you to bring in a Docker image, but it feels a bit exaggerated.

Install a package containing pre-built Python with the same options as Amazon Linux

This is my favorite this time. If you have rpm, deb, etc., which are built with the same options as Amazon Linux and provide an independent Python runtime, you can use them easily, quickly, and safely.

** So yes. I made. ** ** http://qiita.com/marcy-terui/items/9b3610bed4e5fcafd9b9 There are some options that I removed because I can't depend on some system global libraries, but there should be no problem because I didn't usually use them.

Installation method

yum

① Add repository

echo "
[bintraybintray-willyworks-rpm]
name=bintray-willyworks-rpm
baseurl=https://dl.bintray.com/willyworks/rpm/centos/\$releaserver/\$basearch/
gpgcheck=0
enabled=1
" | sudo tee -a /etc/yum.repos.d/bintray-willyworks-rpm.repo

② Installation

sudo yum install lamvery

apt

① Add repository

echo "deb https://dl.bintray.com/willyworks/deb trusty main" | sudo tee -a /etc/apt/sources.list

② Installation

sudo apt-get update
sudo apt-get install install lamvery

how to use

The following execution binaries (scripts) are available under / opt / lamvery / bin, so please use them by passing them through your PATH if necessary.

--Python 2.7.9 (as of March 18, 2016)

For reference, here is a description example when using it with CircleCI.

circle.yml


---
machine:
  environment:
    PATH: /opt/lamvery/bin:$PATH

dependencies:
  pre:
    - |
      echo "deb https://dl.bintray.com/willyworks/deb trusty main" | sudo tee -a /etc/apt/sources.list
      sudo apt-get update
      sudo apt-get install lamvery

Finally

Using lamvery inside virtualenv makes it very easy to deploy Lambda for Python functions. If you are interested, please refer to this as well. Of course, you can use it to use only Python.

About "Lamvery", a deployment and management tool for AWS Lambda http://qiita.com/marcy-terui/items/1617ab4e20e3339d1930

Lambda function deploy best practices with CircleCI + Lamvery http://qiita.com/marcy-terui/items/900b72efb38f9b26e8f0

That was the stemmer.

Recommended Posts

Securely deploy your Lambda function using Python built with the same options as Amazon Linux
Load an external library from your Lambda function using AWS Lambda Layers. The Python environment for Amazon Linux 2 is also in place. (Python3.6, Anaconda)
Deploy Python3 function with Serverless Framework on AWS Lambda
[Python] Make the function a lambda function
[AWS] Using ini files with Lambda [Python]
Building a Python3 environment with Amazon Linux2
Behind the flyer: Using Docker with Python
Working with OpenStack using the Python SDK
[Python] I tried the same calculation as LSTM predict with from scratch [Keras]
[AWS Lambda] Create a deployment package using the Docker image of Amazon Linux
Lambda function deploy best practices with CircleCI + Lamvery
When a local variable with the same name as a global variable is defined in the function