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

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

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

相关文章

git pull不同步_git回退版本,再返回最新分支git pull失败的解决经验

本文转载自【微信公众号&#xff1a;羽林君&#xff0c;ID&#xff1a;Conscience_Remains】总述一篇解决gti分支切换问题的文章&#xff0c;大家应该都有过这种情况&#xff0c;就是git最新的代码进行编译的时候&#xff0c;发现最新代码有bug&#xff0c;有些不确认问题点&am…

junit5 动态测试_JUnit 5 –动态测试

junit5 动态测试在定义测试时&#xff0c;JUnit 4有一个很大的弱点&#xff1a;它必须在编译时发生。 现在&#xff0c;JUnit 5将解决此问题&#xff01; Milestone 1 刚刚发布 &#xff0c;它带有全新的动态测试&#xff0c;可以在运行时创建测试。 总览 本系列中有关JUnit 5…

ioc spring 上机案例_通过实例解析Spring Ioc项目实现过程

0. Ioc主要是实现一个控制反转&#xff0c;耦合性大大降低。1. 建maven项目建立一个空的maven项目&#xff0c;然后pom.xml添加spring-context的依赖:org.springframeworkspring-context5.2.7.RELEASE2. 创建pojo java对象package com.aca;public class Hello {private String …

C++ 11 深度学习(十)原始字面量

你是否曾经为了各种json格式无法写入string中而烦恼&#xff0c;为了各种转义而烦恼。如下图 c11为我们带来了全新的解决方法 其新特性为使用. R"(xxxxxxxxxxxx)" ,此种形式可以使得以原有形式进行表现出来

java日期时间转日期_Java时间和日期指南

java日期时间转日期长期以来&#xff0c;正确处理日期&#xff0c;时间&#xff0c;时区&#xff0c;夏令时&#xff0c;and年等一直是我的烦恼。 本文并不是一个全面的指南时域&#xff0c;请参阅日期和时间在Java中 -更详细&#xff0c;但略有下降&#xff0c;ekhem&#xff…

linux过滤端口抓包_Linux抓包工具tcpdump使用总结,WireShark的过滤用法

tcpdump与WireShark是Linux下的两个常用&#xff0c;功能强大的抓包工具&#xff0c;下面列出这两个工具的简单用法。tcpdump用法tcpdump用法&#xff1a;sudo tcpdump -i ens33 src 192.168.0.19 port 80 -xx -Xs 0 -w test.capsudo tcpdump -i ens33 src port 80 -xx -Xs 0 -…

C++ 11 深度学习(十一)final和override

1. final C 中增加了 final 关键字来限制某个类不能被继承&#xff0c;或者某个虚函数不能被重写&#xff0c;和 Jave 的 final 关键字的功能是类似的。如果使用 final 修饰函数&#xff0c;只能修饰虚函数&#xff0c;并且要把final关键字放到类或者函数的后面。 1.1 修饰函数…

交流伺服系统设计指南_交流设计

交流伺服系统设计指南软件设计至关重要。 它是应用程序的基础。 就像蓝图一样&#xff0c;它为所有背景的聚会提供了一个通用平台。 它有助于理解&#xff0c;协作和发展。 设计不应仅视为开发的要素。 它不应该仅仅存在于开发人员的脑海中&#xff0c;否则团队将发现它几乎无…

【前缀和与差分】

前缀和 前缀和的作用&#xff1a;快速计算数组中某一段区间内的总和 1.需要两个额外的数组&#xff0c;来存储原始数据的数组 和 计算过前缀的数组。其原理为前缀和的数组中每个元素用来保存前i个原数组中的和&#xff0c;下一个元素更新就采用s[i] s[i] - 1 a [i] 来持续更…

allergro音乐术语什么意思_rit(这是音乐术语)什么意思?

是渐慢的意思常用的音乐表情术语&#xff1a;速度标记largo——广板lento——慢板adagio——柔板grave——壮板andante——行板andantino——小行板moderato——中板allegretto——小快板allegro——快板vivo——快速有生气vivace——快速有生气presto——急板常用的音乐表情术…

英文连词_连词我们…讨厌

英文连词最近&#xff0c;我写了与实现相关的名称&#xff0c;并提供了一些示例&#xff0c;这些示例由于方法名称与主体之间的紧密关系而导致方法名称不正确。 有一会儿&#xff0c;我们有以下代码&#xff1a; boolean isComplexOrUnreadableWithTests() { return (complex…

Ubuntu系统手动安装英伟达驱动程序

屏蔽开源驱动nouveau 安装过程会询问是否屏蔽&#xff0c;手动屏蔽也有多种操作方式&#xff0c; sudo gedit /etc/modprobe.d/blacklist.conf 加参数到最底下回车另起一行内容为 blacklist nouveau options nouveau modeset0 保存再终端更新内核命令 sudo update-initr…

workbench拓扑优化教程_workbenchds拓扑优化分析.ppt

workbenchds拓扑优化分析形状优化基础 指定Shape Optimization 将执行形状或拓扑优化 Shape Optimization是一个优化问题,其结构能量在减少结构体积的基础上的最小化 另一种观点就是Shape Optimization尽量得到关于体积比率的最好刚度. Shape Optimization尽可能的找寻可以在对…

maven 父maven_Maven神秘化

maven 父maven由于我的Android开发的背景下&#xff0c;我比较习惯到Gradle &#xff0c;而不是Maven的 。 尽管我知道Gradle基于Maven&#xff0c;但我从未调查过幕后发生的事情。 在过去的一周中&#xff0c;我一直在尝试了解细节并找出Maven的不同组成部分。 什么是Maven M…

【WebRTC---序篇】(一)为什么要使用WebRTC

1.1.1自研直播客户端架构 一个最简单的直播客户端至少应该包括音视频采集模块,音视频编码模块,网络传输模块,音视频解码模块和音视频渲染模块五大部分。如下图所示 1.1.2拆分音视频模块 在实际开发中,音频和视频处理完全是独立的。如下图所示,经过细分后,音频采集与视频…

python实现lenet_吴裕雄 python 神经网络TensorFlow实现LeNet模型处理手写数字识别MNIST数据集...

importtensorflow as tftf.reset_default_graph()#配置神经网络的参数INPUT_NODE 784OUTPUT_NODE 10IMAGE_SIZE 28NUM_CHANNELS 1NUM_LABELS 10#第一层卷积层的尺寸和深度CONV1_DEEP 32CONV1_SIZE 5#第二层卷积层的尺寸和深度CONV2_DEEP 64CONV2_SIZE 5#全连接层的节点个数F…

DFS(深度搜索最简单的应用)

全排列数字 #include<iostream>using namespace std;const int N 10;int n 3; //最终输出 int path[N]; //记录当前使用过的数 int st[N];void dfs(int u) {if (u n){for (int i 0; i < n; i)printf("%d ", path[i]);puts("");return;}for (…

lucene学习笔记_学习Lucene

lucene学习笔记我目前正在与一个团队合作&#xff0c;开始一个基于Lucene的新项目。 虽然大多数时候我会争论使用Solr还是Elasticsearch而不是简单的Lucene&#xff0c;但这是一个有意识的决定。 在这篇文章中&#xff0c;我正在整理一些学习Lucene的资源–希望您对他们有所帮助…

websocket没准备好如何解决_那些很重要,但是不常用的技术,websocket

目录1. 为什么会有websocket2. websocket协议格式3. 协议具体实现一、为什么需要 WebSocket&#xff1f;初次接触 WebSocket 的人&#xff0c;都会问同样的问题&#xff1a;我们已经有了 HTTP 协议&#xff0c;为什么还需要另一个协议&#xff1f;它能带来什么好处&#xff1f;…