CDK构造使用APIGateway(REST)与IoCore进行代理通信。
aws-solutions-constructs.aws-apigateway-iot的Python项目详细描述
aws apigateway物联网模块
---All classes are under active development and subject to non-backward compatible changes or removal in any future version. These are not subject to the Semantic Versioning model. This means that while you may use them, you may need to update your source code when upgrading to a newer version of this package.
Reference Documentation: | https://docs.aws.amazon.com/solutions/latest/constructs/ |
---|
概述
这个AWS解决方案构造实现了一个连接到AWS IoT模式的amazonapi网关restapi。在
这个构造在API网关和AWS IoT之间创建了一个可伸缩的HTTPS代理。当希望允许不支持MQTT或MQTT/Websocket协议的遗留设备与AWS IoT平台交互时,这非常有用。在
此实现允许在给定的MQTT主题上发布只写消息,还支持HTTPS设备的影子更新,以允许设备注册表中的内容。它不涉及用于代理消息的Lambda函数,而是依赖于AWS IoT集成的直接API网关,该网关支持JSON消息和二进制消息。在
以下是Typescript中的最小可部署模式定义:
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826fromaws_solutions_constructs.aws_apigateway_iotimportApiGatewayToIotApiGatewayToIot(self,"ApiGatewayToIotPattern",iot_endpoint="a1234567890123-ats")
初始值设定项
^{pr2}$参数
构装道具
Name | Type | Description |
---|---|---|
iotEndpoint | ^{ | The AWS IoT endpoint subdomain to integrate the API Gateway with (e.g a1234567890123-ats). |
apiGatewayCreateApiKey? | ^{ | If set to ^{ |
apiGatewayExecutionRole? | ^{ | IAM Role used by the API Gateway to access AWS IoT. If not specified, a default role is created with wildcard ('*') access to all topics and things. |
apiGatewayProps? | ^{ | Optional user-provided props to override the default props for the API Gateway. |
图案属性
Name | Type | Description |
---|---|---|
apiGateway | ^{ | Returns an instance of the API Gateway REST API created by the pattern. |
apiGatewayRole | ^{ | Returns an instance of the iam.Role created by the construct for API Gateway. |
apiGatewayCloudWatchRole | ^{ | Returns an instance of the iam.Role created by the construct for API Gateway for CloudWatch access. |
apiGatewayLogGroup | ^{ | Returns an instance of the LogGroup created by the construct for API Gateway access logging to CloudWatch. |
默认设置
没有任何重写的构件的现成实现将设置以下默认值:
亚马逊API网关
- 部署边缘优化的API终结点
- 使用
POST
方法创建API资源,以将消息发布到IoT主题 - 使用
POST
方法创建API资源,以将消息发布到ThingShadow&NamedShadows - 为API网关启用CloudWatch日志记录
- 为API网关配置IAM角色,以访问所有主题和内容
- 将所有API方法的默认授权类型设置为IAM
- 启用X射线跟踪
- 创建一个使用计划并关联到
prod
阶段
下面是对部署构造后API网关公开的不同资源和方法的描述。有关如何使用curl
轻松测试这些端点的更多信息,请参见Examples部分。在
Method | Resource | Query parameter(s) | Return code(s) | Description |
---|---|---|---|---|
POST | ^{ | qos | ^{ | By calling this endpoint, you need to pass the topics on which you would like to publish (e.g ^{ |
POST | ^{ | None | ^{ | This route allows to update the shadow document of a thing, given its ^{ |
POST | ^{ | None | ^{ | This route allows to update the named shadow document of a thing, given its ^{ |
建筑
示例
以下示例仅适用于API_KEY
身份验证类型,因为IAM授权也需要指定SIGv4令牌,因此在部署堆栈时,请确保构造属性的apiGatewayCreateApiKey属性设置为true,否则以下示例将无法工作。在
发布消息
您可以使用curl
使用HTTPS API发布关于不同MQTT主题的消息。下面的示例将在device/foo
主题上发布一条消息。在
curl -XPOST https://<stage-id>.execute-api.<region>.amazonaws.com/prod/message/device/foo -H "x-api-key: <api-key>" -H "Content-Type: application/json" -d '{"Hello": "World"}'
Replace the
stage-id
,region
andapi-key
parameters with your deployment values.
您可以在URL中链接主题名称,API最多接受7个子主题,您可以在这些子主题上发布。例如,下面的示例发布主题为device/foo/bar/abc/xyz
的消息。在
curl -XPOST https://<stage-id>.execute-api.<region>.amazonaws.com/prod/message/device/foo/bar/abc/xyz -H "x-api-key: <api-key>" -H "Content-Type: application/json" -d '{"Hello": "World"}'
更新设备阴影
要更新与给定对象关联的卷影文档,可以使用对象名称发出卷影状态请求。请参阅下面的示例,了解如何更新thing shadow。在
curl -XPOST https://<stage-id>.execute-api.<region>.amazonaws.com/prod/shadow/device1 -H "x-api-key: <api-key>" -H "Content-Type: application/json" -d '{"state": {"desired": { "Hello": "World" }}}'
更新命名阴影
要更新与给定对象的命名shadow相关联的shadow文档,可以使用thing name和shadow name发出一个shadow state请求。有关如何更新命名阴影,请参见以下示例。在
curl -XPOST https://<stage-id>.execute-api.<region>.amazonaws.com/prod/shadow/device1/shadow1 -H "x-api-key: <api-key>" -H "Content-Type: application/json" -d '{"state": {"desired": { "Hello": "World" }}}'
发送二进制有效载荷
可以将二进制有效负载发送到代理API,向下发送到AWS IoT服务。在下面的示例中,我们使用application/octet-stream
内容类型将与此模块关联的README.md
文件的内容(作为二进制数据处理)发送到device/foo
主题。在
curl -XPOST https://<stage-id>.execute-api.<region>.amazonaws.com/prod/message/device/foo/bar/baz/qux -H "x-api-key: <api-key>" -H "Content-Type: application/octet-stream" --data-binary @README.md
Execute this command while in the directory of this project. You can then test sending other type of binary files from your file-system.
©版权所有2020亚马逊网站,Inc.或其附属公司。版权所有。在
- 项目
标签: