带有API网关的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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/353507.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

获取焦点

win32 API:HWND SetFocus&#xff08;HWND hWnd&#xff09;MFC直接CWnd::SetFocus();参数&#xff1a;hWnd&#xff1a;接收键盘输入的窗口指针。若该参数为NULL&#xff0c;则击键被忽略。返回值&#xff1a;若函数调用成功&#xff0c;则返回原先拥有键盘焦点的窗口句柄。若…

树的问题?

题目&#xff1a; 一个深度为L的满k叉树有如下性质&#xff1a;第L层上的结点都是叶子结点&#xff0c;其余各层上每个结点都有k棵非空子树。如果按层数顺序从1开始对全部结点编号&#xff0c;问&#xff1a; &#xff08;1&#xff09;各层的结点数目是多少&#xff1f; &a…

HibernateTemplate

org.springframework.orm.hibernate5.HibernateTemplate 这是spring-orm-4.3.4.RELEASE.jar包中的一个类&#xff0c;这个类是对Hibernate进行了封装&#xff1b; 这是可以进行注入的属性&#xff0c;需要注入sessionFactory属性&#xff0c;因此我们需要创建一个sessionFactor…

使用电脑无线网卡分享网络命令

netsh wlan set hostednetwork modeallow ssidYourWifiName keyYourWifiPasswordnetsh wlan start hostednetworknetsh wlan show hostednetworkpause

STL_queue

STL__queue_的应用 调用的时候要有头文件&#xff1a; #include<stdlib.h> 或 #include<cstdlib> #include<queue> 详细用法: 定义一个queue的变量 queue<Type> que 查看是否为空范例 que.empt…

第四章: 4.1 logging模块 | 正则表达式

修改json数据然后写入json文件中 f open(1234.json,r,encodingutf-8) data f.read() data1 json.loads(data)data1[status] 1f1 open(1234.json,w,encodingutf-8)json.dump(data1,f1) hashlib md5值的用法 #加入下面这个就可以 password input(请输入密码:)m hashlib.md…

Java EE 6 VS Spring 3:Java EE杀死了Spring? 没门!

介绍 几天前&#xff0c;我在听Java Spotlight Podcast的第85集 。 在这次演讲中&#xff0c; Bert Ertman和Paul Bakker讨论了从Spring迁移到Java EE的问题。 基本上&#xff0c;在他们的介绍中&#xff0c;他们说&#xff0c;如今&#xff0c;选择Spring而不是Java EE是没有意…

蓝桥杯 n进制小数

n进制小数 将任意十进制正小数分别转换成2,3,4,5,6,7,8,9进制正小数&#xff0c;小数点后保留8位&#xff0c;并输出。例如&#xff1a;若十进制小数为0.795&#xff0c;则输出&#xff1a; 十进制正小数 0.795000 转换成 2 进制数为: 0.11001011 十进制正小数 0.795000 转换成…

状态栏编程(显示系统时间和进度条)

原文地址&#xff1a;http://welkangm.blog.163.com/blog/static/19065851020127941446182/ 显示系统时间 1、 在状态栏中设置两个新的栏位Timer和Progress。首先到ResourceView中编辑String Table&#xff0c;增加IDS_TIMER(时间),PROGRESS(进度)。然后在MainFrame中修改ind…

使用保险柜管理机密

您如何存储秘密&#xff1f; 密码&#xff0c;API密钥&#xff0c;安全令牌和机密数据属于秘密类别。 那是不应该存在的数据。 在容易猜测的位置&#xff0c;不得以纯文本格式提供。 实际上&#xff0c;不得在任何位置以明文形式存储它。 可以使用Spring Cloud Config Server或…

winScp中文乱码设置

打开一个putty窗口(图一)&#xff0c;左上角鼠标左键点击&#xff0c;弹出设置界面&#xff0c;选择Change Settings&#xff0c;在图二界面的window->translation&#xff0c;Remote character set选择UTF-8&#xff0c;Apply应用即可。 转载于:https://www.cnblogs.com/ya…

键盘布局的改进之道

原文地址&#xff1a;http://www.cppblog.com/huaxiazhihuo/archive/2013/06/29/201380.html好久没上博客了&#xff0c;自己的那么一点微末道行也不敢拿出来丢人现眼。实际上&#xff0c;过去的几年&#xff0c;真的是让C和MFC害惨了&#xff0c;一直自个儿固步自封&#xff0…

如何将二维数组作为函数的参数传递

如何将二维数组作为函数的参数传递声明&#xff1a;如果你是得道的大侠&#xff0c;这篇文章可能浪费你的时间&#xff0c;如果你坚持要看&#xff0c;我当然感觉很高兴&#xff0c;但是希望你看完了别骂我&#xff01;如果你发现我这篇文章有错误的话&#xff0c;你可以提出批…

鼠标爱心显示

/**爱心start**/(function(window,document,undefined){var hearts [];window.requestAnimationFrame (function(){return window.requestAnimationFrame ||window.webkitRequestAnimationFrame ||window.mozRequestAnimationFrame ||window.oRequestAnimationFrame ||window…

多台电脑共用一个耳机、音箱

台式机电脑声卡一般有三个插孔&#xff0c;一个是麦克风&#xff0c;一个是耳机&#xff0c;另一个就是LineIn输入口了&#xff0c;买一根AUX线&#xff0c;一头插入笔记本的耳机插口&#xff0c;另一头插入台式机linein口&#xff1b;在控制面板的声音中选择线路输入&#xff…

liferay开发文档_Liferay –简单主题开发

liferay开发文档实际上&#xff0c;Liferay的6.1版本已经走了很长一段路&#xff0c;该版本完全支持JSF和IceFaces。 我一直在努力学习它的绳索&#xff0c;因为我希望使其成为我们团队中的标准协作工具。 好的软件应用程序可以解决问题&#xff0c;但是好的软件应用程序不仅可…

ACRush 楼天城回忆录

利用假期空闲之时&#xff0c;将这几年 GCJ &#xff0c; ACM &#xff0c; TopCoder 参加的一些重要比赛作个回顾。首先是 GCJ2006 的回忆。 Google Code Jam 2006 一波三折&#xff1a; Google Code Jam 2006 是我第一次到美国参加现场的程序设计比赛。 Google Code Jam 2006…

Matlab计时函数

1. cputime 显示Matlab启动后所占用的CPU时间&#xff1b;eg: t0 cputime; 你的程序&#xff1b;timecputime-t0;2. tic&#xff0c;toc 秒表计时&#xff0c;tic是开始&#xff0c;toc是结束&#xff1b;eg: tic; 你的程序&#xff1b;toc;3. clock&#xff0c;etime 前者显示…

列表,字典表达式以及三元表达式

1.三元表达式条件成立时的返回值 if 条件 else 条件不成立时的返回值三元表达式的意义就是让一些简单的if判断写成一行&#xff0c;减少代码量def max2(x,y): if x > y: return x else: return yx10y20res x if x > y else yprint(res)2.列表生成式…

JUnit 5 –条件

最近&#xff0c;我们了解了JUnit的新扩展模型以及它如何使我们能够将自定义行为注入测试引擎。 我向你保证要看情况。 现在就开始吧&#xff01; 条件允许我们在应该执行或不应该执行测试时定义灵活的标准。 它们的正式名称是“ 条件测试执行” 。 总览 本系列中有关JUnit 5…