python - 将函数转换为 Lambda

标签 python aws-lambda

我让这个函数在 python 中按预期工作。 如何将其转换为 AWS Lambda 函数?

def mymailgun(url):    
    import urllib2
    myfile=urllib2.urlopen(url)

    import requests
    print requests.post("https://api.mailgun.net/v3/XXX.mailgun.org/messages",
                        auth=("api", "key-XXX"),
                        files=[("attachment", myfile)
                               #("attachment", open("files/test.txt"))
                               ],
                        data={"from": "Excited User <excited-user@example.com>",
                              "to": "XXX@gmail.com",
                              "cc": "YYY@yahoo.com",
                              "bcc": "ZZZ@hotmail.com",
                              "subject": "Hello",
                              "text": "Testing some awesomness with attachments!",
                              "html": myfile})

最佳答案

您的代码必须遵循 Lambda 函数编程模型,看来您需要稍微修改代码以适应。您的 Python 代码必须将其中一个函数标识为处理程序。这是按如下方式完成的:

def handler_name(event, context): 
    ...
    return some_value

来自官方手册:

event—AWS Lambda uses this parameter to pass in event data to the handler. This parameter is usually of the Python dict type. It can also be list, str, int, float, or NoneType type.

context—AWS Lambda uses this parameter to provide runtime information to your handler. This parameter is of the LambdaContext type. Optionally, the handler can return a value. What happens to the returned value depends on the invocation type you use when invoking the Lambda function:

If you use the RequestResponse invocation type (synchronous execution), AWS Lambda returns the result of the Python function call to the client invoking the Lambda function (in the HTTP response to the invocation request, serialized into JSON). For example, AWS Lambda console uses the RequestResponse invocation type, so when you test invoke the function using the console, the console will display the returned value.

  • If the handler does not return anything, AWS Lambda returns null.

  • If you use the Event invocation type (asynchronous execution), the value is discarded.

进行这些更改后,第一步是将代码连同任何依赖项一起打包到部署包中。为此,您必须创建一个

.zip file in the following fashion.

首先,为您的包创建一个目录。在这种情况下,您可以将其称为 MailgunScript 或类似名称。将所有 Python 源文件保存在此目录的根级别。

您可以使用 pip 命令将任何所需的库(如 requests 和 urllib2 库)安装到您选择的目录中:

pip install requests -t /absolutePathTo/MailgunScript
pip install urllib2 -t /absolutePathTo/MailgunScript

最后,您必须从该目录的内容而不是目录本身创建一个.zip 存档。

您现在已准备好将部署包转换为 Lambda 函数。登录您的 AWS 管理控制台并选择 Create a Lambda Function。如果系统提示您选择蓝图,您可以选择默认的 hello-world 蓝图,然后继续上传您的部署包并根据需要填写其余字段。

然后,您只需返回主 AWS 管理控制台,选择函数并单击测试,即可测试该函数。或者,您可以使用如下命令从命令行界面手动调用新的 Lambda 函数:

aws lambda invoke \
--region yourRegion \
--function-name yourFunctionName \
--payload '{"url"}'  \
--invocation-type RequestResponse \
/tmp/response

这将执行您的函数并将响应输出到 /tmp/response 以供检查。

关于python - 将函数转换为 Lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968387/

相关文章:

python - 模块 'pytextrank' 没有属性 'parse_doc'

python - 每当我尝试添加 <int :pk> in urls. py 时,在 Django 中找不到页面 (404)

python - 为 Python 2.6 添加 SSL 支持

python - 如何将代码转换为 AWS Lambda 函数?

python - matplotlib svg save - contourf 水平周围的浅色边框

javascript - Python生成的动态刷新页面

amazon-s3 - TemplateURL 必须引用您有权访问的有效 S3 对象

amazon-web-services - CloudFormation 更新支持 "Refer to Resources in Another Stack"

python - 在 AWS Lambda 上运行 Selenium 时出现问题

amazon-web-services - AWS : Can Lambda permission policy have a source from target-group with wildcards?