在上一篇文章中,我向您展示了如何创建和部署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 *.py
function_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 package
function_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 -e
region=$AWS_REGION
account_id=$AWS_ACCOUNT_ID
echo "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.txt
echo "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_id
echo "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/invocations
echo "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-test
echo "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