aws lambda_带有API网关的AWS Lambda
aws lambda
在上一篇文章中,我向您展示了如何創建和部署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_name="helloworld" package_file=helloworld.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_name="helloworld" package_file="target/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 it's ID ..." api_id=$(aws apigateway create-rest-api \--name HelloWorldAPI \--description "Hello World API" \--output text \--query 'id') echo "> API ID is: $api_id"echo "Storing the API ID on disk - we'll 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_id"echo "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/hello"echo "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/hello"echo "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?to=whomever"步驟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
aws lambda
總結
以上是生活随笔為你收集整理的aws lambda_带有API网关的AWS Lambda的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在电脑上玩全民打怪兽(打怪兽游戏怎么玩)
- 下一篇: junit5 动态测试_JUnit 5