Send Post request to an external API using AWS Lambda in python
Question:
I want to send a post request to an external API (https://example.com/api/jobs/test) every hour.
The Lambda Function that I used is as follows:
Handler: index.lambda_handler
python: 3.6
index.py
import requests
def lambda_handler(event, context):
url="https://example.com/api/jobs/test"
response = requests.post(url)
print(response.text) #TEXT/HTML
print(response.status_code, response.reason) #HTTP
Test Event:
{
"url": "https://example.com/api/jobs/test"
}
Error:
START RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d Version: $LATEST
Unable to import module 'index': No module named 'requests'
END RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d
REPORT RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d Duration: 0.65 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
Any help would be appreciated.
Answers:
You need to install requests
module to your project directory and create a lambda deployment package. See this link for details.
In short, you need to create your index.py file on you development system (PC or mac), install Python & pip on that system; them follow the steps in the doc. To create lambda, choose the ‘Upload zip’ option instead of the ‘Edit inline’ one
Vendored requests
are now removed from botocore
.
Consider packaging your Lambda code with requirements.txt
using CloudFormation package or SAM CLI packaging functionality.
My older answer from before vendored requests
deprecation:
You may be able to leverage requests
module from the boto
library without having to install or package your function.
Consider this import:
import botocore.vendored.requests as requests
You need to install requests module
type : pip install requests
into your terminal, or after activating virtual environment if you are using
I want to send a post request to an external API (https://example.com/api/jobs/test) every hour.
The Lambda Function that I used is as follows:
Handler: index.lambda_handler
python: 3.6
index.py
import requests
def lambda_handler(event, context):
url="https://example.com/api/jobs/test"
response = requests.post(url)
print(response.text) #TEXT/HTML
print(response.status_code, response.reason) #HTTP
Test Event:
{
"url": "https://example.com/api/jobs/test"
}
Error:
START RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d Version: $LATEST
Unable to import module 'index': No module named 'requests'
END RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d
REPORT RequestId: 370eecb5-bfda-11e7-a2ed-373c1a03c17d Duration: 0.65 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
Any help would be appreciated.
You need to install requests
module to your project directory and create a lambda deployment package. See this link for details.
In short, you need to create your index.py file on you development system (PC or mac), install Python & pip on that system; them follow the steps in the doc. To create lambda, choose the ‘Upload zip’ option instead of the ‘Edit inline’ one
Vendored requests
are now removed from botocore
.
Consider packaging your Lambda code with requirements.txt
using CloudFormation package or SAM CLI packaging functionality.
My older answer from before vendored
requests
deprecation:
You may be able to leveragerequests
module from theboto
library without having to install or package your function.Consider this import:
import botocore.vendored.requests as requests
You need to install requests module
type : pip install requests
into your terminal, or after activating virtual environment if you are using