山西有哪些做网站的公司,班级优化大师下载安装app,中国菲律宾数据,logo网站有哪些在上一篇文章中#xff0c;我向您展示了如何创建和部署AWS Lambda。 我们将继续这项工作#xff0c;并只考虑更新该lambda的代码。 我们还将使用AWS API Gateway将REST端点添加到AWS Lambda。 因此#xff0c;在继续之前……#xff08;如果尚未使用#xff09;#xff… 在上一篇文章中我向您展示了如何创建和部署AWS Lambda。 我们将继续这项工作并只考虑更新该lambda的代码。 我们还将使用AWS API Gateway将REST端点添加到AWS Lambda。 因此在继续之前……如果尚未使用请按照上一篇文章中的说明进行操作以确保您具有正在运行的AWS Lambda实例。 步骤1更新您的Lambda 将以下内容粘贴到update-lambda.sh #!/bin/bash### Create the lambda package
zip -j helloworld.zip *.pyfunction_namehelloworld
package_filehelloworld.zip### Update the lambda code
aws lambda update-function-code \--function-name $function_name \--zip-file fileb://$package_file 或Java #!/bin/bash### Create the lambda package
mvn packagefunction_namehelloworld
package_filetarget/lambda-java-example-1.0-SNAPSHOT.jar### Update the lambda code
aws lambda update-function-code \--function-name $function_name \--zip-file fileb://$package_file 使脚本可执行文件chmod x update-lambda.sh并更新lambda ./update-lambda.sh 。 第2步传递一些东西给您的lambda 现在我们知道了如何更新云中的lambda让我们进行更改以便我们可以将某些内容作为参数传递。 与其说“你好世界” 我们希望它向任何人问好。 在Python中 def lambda_handler(event, context):return Hello {}!.format(event[to]) 或类似于Java中的以下内容 package example;import com.amazonaws.services.lambda.runtime.Context;public class Hello {public String lambdaHandler(Request request, Context context) {return Hello request.getTo() !;}
}class Request {private String to;public void setTo(String to) { this.to to; }public String getTo() { return to; }
}步骤3告诉Lambda与任何人打招呼 aws lambda invoke --invocation-type RequestResponse --function-name helloworld --payload {to: whomever} output.txt 你应该看到谁你好 在输出文本中 步骤4让我们添加其余的API 将以下脚本粘贴到诸如create-api.sh的文件中更改该文件的执行权限然后执行该脚本。 深吸一口气…… 注意此脚本期望定义AWS_REGION和AWS_ACCOUNT_ID环境变量 #!/bin/bash
set -eregion$AWS_REGION
account_id$AWS_ACCOUNT_IDecho Creating a new API and capturing its ID ...
api_id$(aws apigateway create-rest-api \--name HelloWorldAPI \--description Hello World API \--output text \--query id)
echo API ID is: $api_idecho Storing the API ID on disk - well need it later ...
echo $api_id api_id.txtecho Geting the root resource id for the API ...
root_id$(aws apigateway get-resources \--rest-api-id ${api_id} \--output text \--query items[?path/].[id])
echo root_id$root_idecho Creating a resource for the /hello path
resource_id$(aws apigateway create-resource \--rest-api-id ${api_id} \--parent-id ${root_id} \--path-part hello | jq -r .id)
echo Resource id is $resource_idecho Creating the GET method on the /hello resource
aws apigateway put-method \--rest-api-id ${api_id} \--resource-id ${resource_id} \--http-method GET \--authorization-type NONE echo Integrating the GET method to lambda. Note that the request tempalate uses API Gateway template language to pull in the query parameters as a JSON event for the lambda.
aws apigateway put-integration \--rest-api-id ${api_id} \--resource-id ${resource_id} \--http-method GET \--type AWS \--request-templates { application/json: {\n #foreach($param in $input.params().querystring.keySet())\n \$param\: \$util.escapeJavaScript($input.params().querystring.get($param))\ \n #end\n } } \--integration-http-method POST \--uri arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${region}:${account_id}:function:helloworld/invocationsecho Creating a default response for the GET method
aws apigateway put-method-response \--rest-api-id ${api_id} \--resource-id ${resource_id} \--http-method GET \--status-code 200 echo Creating a default response for the integration
aws apigateway put-integration-response \--rest-api-id ${api_id} \--resource-id ${resource_id} \--http-method GET \--status-code 200 \--selection-pattern .*echo Adding permission for the API to call the lambda for test so we can use the console to make the api call
aws lambda add-permission \--function-name helloworld \--statement-id apigateway-helloworld-get-test \--action lambda:InvokeFunction \--principal apigateway.amazonaws.com \--source-arn arn:aws:execute-api:${region}:${account_id}:${api_id}/*/GET/helloecho Adding permission for the API to call the lambda from any HTTP client
aws lambda add-permission \--function-name helloworld \--statement-id apigateway-helloworld-get \--action lambda:InvokeFunction \--principal apigateway.amazonaws.com \--source-arn arn:aws:execute-api:${region}:${account_id}:${api_id}/api/GET/helloecho Creating a deployment
aws apigateway create-deployment \--rest-api-id ${api_id} \--stage-name api echo All done! you can invoke the api on https://${api_id}.execute-api.${region}.amazonaws.com/api/hello?towhomever步骤5调用API 脚本的最后输出提供了可以粘贴到浏览器中的URL。 您应该看到“你好你好”的回答。 一旦您点击进入浏览器。 步骤6清理 您可以使用上delete.sh创建的delete.sh脚本删除lambda。 删除api粘贴以下脚本并照常执行。 #!/bin/bash
echo Reading API id that I store in my create-api script
api_id$(api_id.txt)echo Removing the permissions from the lambda
aws lambda remove-permission \--function-name helloworld \--statement-id apigateway-helloworld-get
aws lambda remove-permission \--function-name helloworld \--statement-id apigateway-helloworld-get-testecho Deleting the API
aws apigateway delete-rest-api \--rest-api-id ${api_id}步骤7放松……结束 … 目前 翻译自: https://www.javacodegeeks.com/2016/05/aws-lambda-api-gateway.html