使用python中的AWS Lambda将Post请求发送到外部API

时间:2017-11-02 14:33:04

标签: python http-post aws-lambda

我想每小时向外部API(https://example.com/api/jobs/test)发送一个帖子请求。

我使用的Lambda函数如下:

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

测试事件:

 {
 "url": "https://example.com/api/jobs/test"
}

错误:

 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  

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:6)

您需要将requests模块安装到项目目录并创建lambda部署包。有关详细信息,请参阅this链接。

简而言之,您需要在开发系统(PC或Mac)上创建index.py文件,安装Python&点到那个系统;他们按照文档中的步骤操作。要创建lambda,请选择“上传zip”选项,而不是“编辑内联”一个

答案 1 :(得分:2)

您可能可以利用requests库中的boto模块,而不必安装或打包功能。

考虑此导入: import botocore.vendored.requests as requests

相关问题