[Lambda] Make import requests available [python]

things to do

↓ If you do this image.png

↓ I want you to be like this image.png

↓ However, it will be like this image.png

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'requests'",
  "errorType": "Runtime.ImportModuleError"
}

Procedure outline

Create a Layer file

Work on the client. Just make pip work, zip the one with the external module and put it in from the aws admin console

For example, if you work on amazon linux2 ...

mkdir python/ 
cd python
yum -y install gcc gcc-c++ kernel-devel python-devel libxslt-devel libffi-devel openssl-devel
yum -y install python-pip
pip install -t ./ requests
cd python
cd ..
zip -r Layer.zip python/

Register the zip with Lambda

Work in the AWS Management Console. スクリーンショット 2019-09-29 21.43.44.png In "Create Layer", insert the Layer.zip file you created earlier and you're done.

Function preparation

Select Layers スクリーンショット 2019-09-29 21.48.17.png Click "Add Layer" and the layer you created earlier will appear, so select it. スクリーンショット 2019-09-29 21.48.30.png

Creating a function

lambda_function.py


import requests

def lambda_handler(event, context):
    #Try throwing a GET on the test site
    response = requests.get('https://httpbin.org/get', params={'foo': 'bar'})
    return response.json()

Execution result


{
  "args": {
    "foo": "bar"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "python-requests/2.24.0",
    "X-Amzn-Trace-Id": "Root=1-5f43a5ad-a04c8100f8c999999e8e2e"
  },
  "origin": "913.931.998.950",
  "url": "https://httpbin.org/get?foo=bar"
}

Recommended Posts

[Lambda] Make import requests available [python]
Make Opencv available in Python
[Python] Make the function a lambda function
[Python] Make pip available on macOS
python> link> from __future__ import print_function> Make Python 3.X print () available in Python 2.X
2. Make import easier
Summarize Python import
Make OpenCV3 available from python3 installed with pyenv
Make DHT11 available on Raspberry Pi + python (memo)
Make Lambda Layers with Lambda
A python lambda expression ...
Python from or import
python original module import
Retry with python requests
Import tsv with Python
Make ordinary tweets fleet-like with AWS Lambda and Python
Python Requests status code
python> Microsecond sleep> import time / usleep = lambda x: time.sleep (x / 1000000.0) / usleep (100)
How to import Python library set up in EFS to Lambda
Python> lambda> tiny functions / callback functions
Save images using python3 requests
How Python module import works
Operate TwitterBot with Lambda, Python
Dynamically import scripts in Python
Import vtk with brew python
Make iPython available on OSGeo4W
Aim python library master (18) requests
Query Athena from Lambda Python
Make Cython available on Windows.
Make a bookmarklet in Python
Make a fortune with Python
AWS Lambda with PyTorch [Lambda import]
[Python] Another way to import
python beautifulsoup requests glob find_all
Python # Snap7 Libray Import problem
How to use Python lambda
Make the morphological analysis engine MeCab available in Python 3 (March 2016 version)
Slack --APIGateway --Lambda (Python) --How to make a RedShift interactive app