将函数转换为 Lambda
convert a function to 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
。如果提示 select 蓝图,您可以 select 默认 hello-world
蓝图,然后继续上传您的部署包并根据需要填写其余字段。
然后您可以简单地通过返回主 AWS 管理控制台、select调用该函数并单击 test
来测试该函数。或者,您可以使用如下命令从命令行界面手动调用新的 Lambda 函数:
aws lambda invoke \
--region yourRegion \
--function-name yourFunctionName \
--payload '{"url"}' \
--invocation-type RequestResponse \
/tmp/response
这将执行您的函数并将响应输出到 /tmp/response
以供检查。
您需要以 lambda 函数的方式设置您的函数。正如在证明中所说:
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.
将代码作为 lambda 函数进行测试:
创建 lambda 函数的 CLI 命令:
aws lambda create-function \
--region us-west-2 \
--function-name HelloPython \
--zip-file fileb://deployment-package.zip \
--role arn:aws:iam::account-id:role/lambda_basic_execution \
--handler hello_python.my_handler \
--runtime python2.7 \
--timeout 15 \
--memory-size 512
我的这个功能在 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
。如果提示 select 蓝图,您可以 select 默认 hello-world
蓝图,然后继续上传您的部署包并根据需要填写其余字段。
然后您可以简单地通过返回主 AWS 管理控制台、select调用该函数并单击 test
来测试该函数。或者,您可以使用如下命令从命令行界面手动调用新的 Lambda 函数:
aws lambda invoke \
--region yourRegion \
--function-name yourFunctionName \
--payload '{"url"}' \
--invocation-type RequestResponse \
/tmp/response
这将执行您的函数并将响应输出到 /tmp/response
以供检查。
您需要以 lambda 函数的方式设置您的函数。正如在证明中所说:
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.
将代码作为 lambda 函数进行测试:
创建 lambda 函数的 CLI 命令:
aws lambda create-function \
--region us-west-2 \
--function-name HelloPython \
--zip-file fileb://deployment-package.zip \
--role arn:aws:iam::account-id:role/lambda_basic_execution \
--handler hello_python.my_handler \
--runtime python2.7 \
--timeout 15 \
--memory-size 512