当前位置: 首页 > news >正文

aws apigateway 基础概念和入门示例

参考资料

  • https://docs.aws.amazon.com/zh_cn/apigateway/latest/developerguide/getting-started.html

apigateway基础理解

apigateway的核心概念

  • apigateway,基础服务用来管理接口的创建,部署和管理
  • restapi,http资源和方法的集合,可以根据应用程序的逻辑形成资源树。能够与http,lambda和其他aws服务集成,多阶段部署
  • httpapi,由路由和方法组成,可以与http和lambda集成,多阶段部署
  • websocketapi,由路由和路由键组成,能够与http,lambda,和其他aws服务集成,多阶段部署
  • deployment,api状态的实时快照,与api阶段关联
  • endpoint,apigateway的特定主机名,分为边缘优化,私有和区域端点
  • apikey,用来标识开发人员的唯一标识,可以由apigateway生成
  • stage,api生命周期的指针
  • callbackurl,使用websocket时,客户新连接会在apigateway中存储回调url,用来主动推动信息
  • edge-optimized endpoint,默认的终端节点,实际是由cloudfront转发的最近区域端点、
  • 集成请求和响应,见下文
  • Mapping template,apugateway前后端数据映射的模板
  • 方法请求和响应,见下文
  • Mock integration,apigateway mock响应,不需要后端
  • Private integration,通过私有端点访问vpc内部资源,不需要暴露公网
  • Proxy integration。可以设置HTTP 代理集成或 Lambda 代理集成,代理集成下不会对请求进行额外处理,直接转发

apigateway3个用例

区别和比较,https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-vs-rest.html

  • REST APIs,In API Gateway REST APIs, the frontend is encapsulated by method requests and method responses. The API interfaces with the backend by means of integration requests and integration responses. You can configure the integration response to map required response parameters from integration to method
  • HTTP APIs,lower latency and lower cost. use HTTP APIs to send requests to AWS Lambda functions or to any publicly routable HTTP endpoint
  • WebSocket APIs,the client and the server can both send messages to each other at any time. Can integrated with lambda kinesis and HTTP endpoint.

apigateway的请求逻辑

apigateway的restapi用来集成后端http服务,lambda函数和其他aws服务,将这些服务通过资源和方法暴露出去

例如,/incomes使资源,而其上的GET/POST/PUT等操作即方法

总体的请求逻辑可以分为以下两个阶段

  • 方法请求和方法响应,应用程序和apigateway之间的通信

  • 集成请求和集成响应,apigateway和后端之间的通信

由于以上两个阶段的请求格式和处理逻辑不同,因此需要apigateway将方法请求转换为集成请求,将集成响应转换为方法响应(通过定义schema和model实现)

在这里插入图片描述

apigateway集成的方式

在这里插入图片描述

入门示例

创建lambda函数,使用nodejs16运行时,保留默认代码即可

export const handler = async (event) => {const response = {statusCode: 200,body: JSON.stringify('Hello World!'),};return response;
};

创建httpapi,相比restapi功能少,费用低

  • 创建httpapi
  • 选择lambda集成
  • 测试api调用

这里需要注意,lambda集成的负载格式分为v1和v2

https://docs.amazonaws.cn/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html

在这里插入图片描述

保留贪婪匹配,直接下一步自动部署

在这里插入图片描述

在lambda函数界面可以看到自动添加基于资源的策略

在这里插入图片描述

{"Version": "2012-10-17","Id": "default","Statement": [{"Sid": "a2bb716b-d866-538b-b02d-db66f38db633","Effect": "Allow","Principal": {"Service": "apigateway.amazonaws.com"},"Action": "lambda:InvokeFunction","Resource": "arn:aws-cn:lambda:cn-north-1:xxxxxxxxxxx:function:my-function","Condition": {"ArnLike": {"AWS:SourceArn": "arn:aws-cn:execute-api:cn-north-1:xxxxxxxxxxx:8qpcm7sc5d/*/*/my-function"}}}]
}

同样在apigateway控制台能看到相应的添加权限操作
在这里插入图片描述

尝试访问生成的终端节点
在这里插入图片描述

$ curl https://8qpcm7sc5d.execute-api.cn-north-1.amazonaws.com.cn/my-function
{"message":"Forbidden"}

逻辑上我们已经对外提供了接口,但是访问报错,这可能是由于两个因素导致的

  1. 中国区没有备案,不允许匿名访问
  2. 没有权限访问

因此,这里我们使用iam授权

https://docs.amazonaws.cn/apigateway/latest/developerguide/http-api-access-control-iam.html

注意,httpapi不支持基于资源的策略,因此请求方需要sigv4签名,并且具有execute-api权限

aws apigatewayv2 update-route \--api-id 8qpcm7sc5d \--route-id 9f5s6fh \--authorization-type AWS_IAM

这里的routeid有点隐蔽

在这里插入图片描述

我们在之前的文章中讨论过鉴权的问题,当时是使用postman工具,这里用awscurl替代下,用来快速测试apigateway很是方便

https://github.com/okigan/awscurl

使用默认凭证访问apigateway

$ awscurl --service execute-api -X POST -d @request.json https://<prefix>.execute-api.us-east-1.amazonaws.com/<resource>
$ awscurl --service execute-api -X GET https://8qpcm7sc5d.execute-api.cn-north-1.amazonaws.com.cn/my-function --region cn-north-1
"Hello from Lambda!"
http://www.lryc.cn/news/29127.html

相关文章:

  • 2023年“中银杯”安徽省职业院校技能大赛网络安全A模块全过程解析
  • 【Python入门第二十四天】Python 迭代器
  • Qt扫盲-CMake 使用概述
  • minGW-w64配置途径
  • 程序、进程、线程的基本概念、信号量的PV操作、前趋图的PV操作
  • 设计测试用例
  • CSS 选择器以及CSS常用属性
  • 测试概念及模型
  • 王道计算机组成原理课代表 - 考研计算机 第六章 总线 究极精华总结笔记
  • 【C++升级之路】第八篇:string类
  • mysql性能优化_原理_课程大纲
  • 项目管理报告工具的功能
  • centos8上安装hbase
  • linux 进程及调度基础知识
  • Python计算分类问题的评价指标(准确率、精确度、召回率和F1值,Kappa指标)
  • 51单片机LCD1602的使用
  • [深入理解SSD系列综述 1.5] SSD固态硬盘参数图文解析_选购固态硬盘就像买衣服?
  • zio1升级到zio2踩坑和总结
  • 【算法题】1834. 单线程 CPU
  • Vue学习[2023]
  • 【Redis】Redis分片集群
  • 【Android笔记81】Android之RxJava的介绍及其使用
  • Pr 定格拍照动画
  • 放弃node-sass,启用sass
  • 力扣旋转字符串
  • Java 代码组织机制
  • 【剧前爆米花--爪哇岛寻宝】MySQL中索引和事务
  • C++ 线程库
  • python字典和集合——笔记
  • TEX:显示文本