在上一篇文章中,我向您展示了如何创建和部署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

带有API网关的AWS Lambda相关推荐

  1. aws lambda_带有API网关的AWS Lambda

    aws lambda 在上一篇文章中,我向您展示了如何创建和部署AWS Lambda. 我们将继续这项工作,并只考虑更新该lambda的代码. 我们还将使用AWS API Gateway将REST端点 ...

  2. API网关和AWS Lambda进行身份验证

    当Foreach最初涉足微服务领域时,我们并没有真正构建微服务. 我们以为我们做到了,但是我们所有的服务中总存在一些逻辑. 当然,每个服务实际上应该只专注于自己的任务,而不应该专注于属于另一个微服务的 ...

  3. aws lambda_API网关和AWS Lambda进行身份验证

    aws lambda 当Foreach最初踏上微服务之路时,我们并没有真正构建微服务. 我们以为我们做到了,但是我们所有的服务中总存在一些逻辑. 当然,每个服务实际上应该只专注于自己的任务,而不应该专 ...

  4. aws lambda_AWS API Gateway和AWS Lambda示例

    aws lambda The purpose of this article is to present the most relevant details and not-so-straight s ...

  5. Day 30 - 实作 Amazon API GateWay 整合 AWS Lambda 与 Dynamodb

    Day 30 - 实作 Amazon API GateWay 整合 AWS Lambda 与 Dynamodb Amazon API GateWay 简介 Amazon API Gateway 是由 ...

  6. AWS API Gateway与AWS Lambda代理集成构建REST API

    项目地址 https://github.com/JessicaWin/aws lambda分支为自动创建API Gateway REST API资源的部署方式 apigateway分支为自定义API ...

  7. aws lambda_Express.js和AWS Lambda —无服务器的爱情故事

    aws lambda by Slobodan Stojanović 由SlobodanStojanović Express.js和AWS Lambda -无服务器的爱情故事 (Express.js a ...

  8. 使用AWS Lambda在Go中构建RESTful API

    在本文中,我们将学习使用AWS Lambda在Go中设计,构建和部署RESTful API. 在开始之前,让我给您简要介绍一下AWS Lambda. 什么是AWS Lambda? AWS Lambda ...

  9. 使用 Go 和 AWS Lambda 构建无服务 API

    原文地址:How to build a Serverless API with Go and AWS Lambda 原文作者:Alex Edwards 译文出自:掘金翻译计划 本文永久链接:https ...

最新文章

  1. 微软、海思、任天堂等50多家知名公司源代码泄露,人人均可公开访问
  2. struts2 的form 标签theme属性
  3. 响应式web之@media screen
  4. [置顶]       spring + jstl 实现java国际化的配置步骤
  5. .net如何获取文件夹中的文件_access递归列出文件夹中的文件
  6. “跨界养猪”这件事,华为正式回应了
  7. C++编译原理 (转载)
  8. 使用 Async / Await 来编写简明的异步代码
  9. UGUI ScrollRect使用
  10. 数据挖掘常用的基本技术,主要有哪些?
  11. 省市区三级数据-MySQL
  12. 【代码质量】C/C++代码静态分析与常用分析软件工具
  13. 魂斗罗进化革命+塞班JAVA版_魂斗罗进化革命电脑版
  14. 安卓快手批量取关软件v2.0
  15. 【新能源】新能源之锂电池产业链梳理
  16. 万字长文!终于讲透了Python中的多线程和多进程!
  17. 如何使用格式工厂截取音乐或视频的片断
  18. 计算机专业b区大学,b区考研学校,b区考研学校排名。
  19. Android Context 到底是什么?
  20. 图论学习--6 平面图(思维导图)平面概念 对偶图 平面图嵌入算法

热门文章

  1. JDK 6中新增的Java Console类功能概览
  2. Redis 的 4 大法宝,2018 必学中间件
  3. 《四世同堂》金句摘抄(七)
  4. ssm使用全注解实现增删改查案例——DeptMapperImpl
  5. (归并排序 快排 堆)
  6. Python变量名的定义规则与定义方式
  7. 2015蓝桥杯省赛---java---B---7(牌型种数)
  8. c遗传算法的终止条件一般_KDD比赛之遗传算法(举例理解)
  9. spring(6) 渲染web视图
  10. 对Servlet容器的补充