日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

aws lambda_带有API网关的AWS Lambda

發布時間:2023/12/3 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。