[PYTHON] [AWS] What to do when you want to pip with Lambda

Introduction

If you're writing a Lambda function in Python, you may want to use a third-party library inside your Lambda function. However, Lambda does not allow you to install libraries using pip. This time, I will introduce a method that is useful in such a situation.

Specifically, use the file upload function provided by Lambda. This means that you can use the library from within your Lambda function by uploading a third-party library when you upload the file.

This time I'll try using requests, Python's HTTP library.

Related article

Please refer to the following article for the basic method of creating a Lambda function.

procedure

  1. Create a working directory locally
  2. Install the library
  3. Write a Lambda function in your working directory
  4. Zip
  5. Upload the compressed file
  6. Setting the function to use

Creating a working directory

First, create a working directory. Let's call it workspace. After creating the directory, move into it.

$ mkdir workspace
$ cd workspace

Library installation

Once inside the workspace, use pip to install the library. Do the following:

$ pip install requests -t .

You can specify the installation location of the library by adding the t option. This time, it is installed in the current directory (workspace).

Creating a Lambda function

After installing the library, it's time to write the Lambda function. The file name should be ** lambda_function.py ** and the function name should be ** lambda_handler **. Remember the file and function names for later use.

lambda_function.py


import requests

def lambda_handler(event, context):
    res = requests.get("http://www.yahoo.co.jp/")
    return res.status_code

The code looks like the above. The content is as simple as GET the top page of Yahoo and return the status code. If the page can be acquired normally, 200 will be returned as the status code.

>>> from lambda_function import lambda_handler
>>> lambda_handler(None, None)
200
>>> 

Compress with Zip

Use the zip command to zip all the files in the workspace. The name of the file to be compressed is ** upload.zip **.

$ zip -r upload.zip *

File upload

After compressing with the zip command, upload the compressed file to Lambda. There is a "Code entry type" in the item "Lambda function code" in the function setting screen. The default is "Edit code inline", but select "Upload a .ZIP file" here. When you select it, the "Upload" button will appear, so upload the file you created earlier (upload.zip). スクリーンショット 2015-11-20 10.44.39.png

Setting the function to use

After uploading the file, set the function to be used in ** Handler **. Functions can be specified in the format "filename.functionname". The file name of the Lambda function created earlier was ** lambda_function **, and the function name was ** lambda_handler **, so specify "lambda_function.lambda_handler". スクリーンショット 2015-11-20 10.51.24.png

This completes the settings. Select the "Next" button and "Create function" button to create the function.

test

Try testing from the test screen. If the status code is returned, it is successful. Thank you for your hard work. スクリーンショット 2015-11-20 10.57.20.png

reference

Recommended Posts

[AWS] What to do when you want to pip with Lambda
Links to do what you want with Sublime Text
Let's summarize what you want to do.
What to do if you can't install pyaudio with pip #Python
What to do if you get a UnicodeDecodeError with pip install
What to do if you get an Undefined error when trying to use pip with pyenv
When you want to use it as it is when using it with lambda memo
What to do if you can't install with pip in babun environment
What to do if you get Could not fetch URL 443 with pip
Upload what you got in request to S3 with AWS Lambda Python
What to do if you can't pip install mysqlclient
Settings when you want to run python-mecab with travis
When you want to filter with Django REST framework
I want to AWS Lambda with Python on Mac!
Things to do when you start developing with Django
What to do when you can't bind CaboCha to Python
What to do if you get an error when installing python with pyenv
What to do when you want to receive files from a Windows client remotely
What to do when you get angry that libxml / xmlversion.h does not exist when you put lxml with pip
Solution when you want to use cv_bridge with python3 (virtualenv)
What to do if you get an OpenSSL error when installing Python 2 with pyenv
What to do with Magics install
Use aggdraw when you want to draw beautifully with pillow
Move what you installed with pip to the conda environment
What to do if you can't sort files with subscripts
What to do with PYTHON release?
What to do when an error occurs with import _ssl
When you want to register Django's initial data with relationships
[AWS EC2] Settings you want to do on Amazon Linux 2
What to do if you get an Import Error when importing matplotlib with Jupyter
I want to do ○○ with Pandas
ImportError: No module What to do when you are told
What to do if you don't want to use Japanese column names when using ortoolpy.logistics_network
Python | What you can do with Python
Make a note of what you want to do in the future with Raspberry Pi
[AWS] What to do when the ping command causes a "timeout"
When it is troublesome to copy what you built with vue
[Beanstalk] What to do when an error occurs with import uuid
When you want to print to standard output with print while testing with pytest
When you want to send an object with requests using flask
[TensorFlow] If you want to run TensorBoard, install it with pip
What to do if you can't build your project with Maven
What to do when you get "I can't see the site !!!!"
When you want to adjust the axis scale interval with APLpy
What you can do with API vol.1
I want to pip install with PythonAnywhere
I want to play with aws with python
Connect to s3 with AWS Lambda Python
What you can do with programming skills
[AWS] Do SSI-like things with S3 / Lambda
ImportError when trying to use gcloud package with AWS Lambda Python version
[Memorandum] What to do when a warning appears after executing pip list
What to do if you can't find well with grep's -f option
I want to do it with Python lambda Django, but I will stop
What to do if you couldn't send an email to Yahoo with Python.
Gist repository to use when you want to try a little with ansible
When you want to replace a column with a missing value (NaN) column by column
What to do if you get lost in file reference with FileNotFoundError
What to do if an error occurs when importing numpy with VScode
What to do if you get a TypeError with numpy min, max
What to do if you get an error when trying to load mnist