如何解决Cloudformation DynamoDB-API 网关代理
我尝试使用 dynamodb
实现 apigateway
代理。但是调用的时候,api返回错误,
2021 年 3 月 19 日星期五 20:30:27 UTC:由于配置,执行失败 错误:无法转换请求
2021 年 3 月 19 日星期五 20:30:27 UTC:方法完成,状态:500
对我来说,问题似乎不在于 requestTemplates
转换 (?),但还有什么问题吗?
Cloudformation
模板。
AWstemplateFormatVersion: "2010-09-09"
Transform: "AWS::Serverless-2016-10-31"
Description: Feedback Store; A hypothetical example to test direct proxy to Dynamodb table from api gateway.
Metadata:
cfn-lint:
config:
ignore_checks:
# Not useful at all.
- I1022
Parameters:
Environment:
Type: String
Description: Current environment
Default: "dev"
Resources:
EventsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: Feedback_events
AttributeDeFinitions:
- AttributeName: UserID
AttributeType: S
- AttributeName: TimeUTC
AttributeType: S
KeySchema:
- AttributeName: UserID
KeyType: HASH
- AttributeName: TimeUTC
KeyType: RANGE
BillingMode: PAY_PER_REQUEST
Tags:
- Key: "Feedbacks"
Value: "true"
ApiGatewayDynamoRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: "apigateway.amazonaws.com"
Action:
- "sts:AssumeRole"
Policies:
- PolicyName: "ApiGatewayDynamoRolePolicy"
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "dynamodb:DescribeTable"
- "dynamodb:BatchWriteItem"
- "dynamodb:PutItem"
- "dynamodb:Query"
- "dynamodb:UpdateItem"
Resource:
- !GetAtt EventsTable.Arn
- Effect: "Allow"
Action:
- "dynamodb:ListTables"
Resource: "*"
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
DeFinitionBody:
swagger: 2.0
info:
title: !Sub "${AWS::StackName}-backend-api"
version: 2021-03-21T22:37:24Z
basePath: /prod
schemes:
- https
paths:
/Feedback:
post:
summary: Creates a new Feedback
description: |
Creates a new Feedback object in the datastore
consumes:
- application/json
produces:
- application/json
parameters:
- name: NewFeedback
in: body
description: New Feedback details.
schema:
$ref: "#/deFinitions/Feedbacks"
tags:
- Feedback Store
x-amazon-apigateway-integration:
type: aws
uri: !Sub arn:aws:apigateway:${AWS::Region}:dynamodb:action/BatchWriteItem
credentials: !GetAtt ApiGatewayDynamoRole.Arn
httpMethod: POST
requestTemplates:
application/json: |
#set($inputRoot = $input.path('$'))
{
"RequestItems": {
"${self:resources.Resources.EventsTable.Properties.TableName}": [
#foreach($event in $inputRoot.event)
{
"PutRequest": {
"Item": {
"UserID" : { "S": "$event.userid" },"TimeUTC" : { "S": "$event.time" },"Lat": { "S": "$event.lat" },"Lng": { "S": "$event.lng" },"UUID" : { "S": "$event.uuid"},"Sensor" : { "S": "$event.sensor_name" },"Reading" : { "S": "$event.reading_value" },"Active" : { "S": "$event.is_active" },}
}
}#if($foreach.hasNext),#end
#end
]
},"ReturnValues": "UPDATED_NEW","ReturnConsumedCapacity": "NONE","ReturnItemCollectionMetrics": "NONE"
}
responses:
"default":
statusCode: "200"
SelectionPattern: "2\\d{2}"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'OPTIONS,POST'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: "'*'"
"BAD.*":
statusCode: "400"
SelectionPattern: "4\\d{2}"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'OPTIONS,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: "'*'"
"INT.*":
statusCode: "500"
SelectionPattern: "5\\d{2}"
responseParameters:
method.response.header.Access-Control-Allow-Methods: "'OPTIONS,X-Amz-Security-Token'"
method.response.header.Access-Control-Allow-Origin: "'*'"
responses:
"200":
description: The unique identifier of the new Feedback
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: string
Access-Control-Allow-Headers:
type: string
schema:
$ref: "#/deFinitions/NewFeedbackResponse"
"400":
description: Bad request
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: string
Access-Control-Allow-Headers:
type: string
schema:
$ref: "#/deFinitions/Error"
"500":
description: Internal error
headers:
Access-Control-Allow-Origin:
type: "string"
Access-Control-Allow-Methods:
type: string
Access-Control-Allow-Headers:
type: string
schema:
$ref: "#/deFinitions/Error"
deFinitions:
Empty:
type: object
title: Empty Schema
Feedbacks:
type: array
items:
$ref: FeedbackItem
FeedbackItem:
properties:
userid:
type: string
description: UserID of the author
time:
type: string
description: Feedback time
lat:
type: string
description: Latitude
lng:
type: string
description: Longitude
uuid:
type: string
description: Globaly unique id for the Feedback
sensor_name:
type: string
description: Device the Feedback coming from
reading_value:
type: string
description: Current reading
is_active:
type: string
description: Device status
NewFeedbackResponse:
properties:
FeedbackId:
type: string
description: The generated unique identifier for the new Feedback
Error:
properties:
code:
type: integer
format: int32
message:
type: string
fields:
type: string
StageName: prod
Variables:
StageVariableName: "TestAPI"
有效载荷:
{
"event": [
{
"userid": "21d6523137f6","time": "2020-06-16T15:22:33Z","lng": "-122.03053391","lat": "37.33180957","uuid": "96a6f48c-fe67-4cad-be24-21d6523137f6","sensor_name": "CYT523","reading_value": "72.9","is_active": "true"
},{
"userid": "4354069ba6e5","uuid": "512f2543-a458-424c-a141-4354069ba6e5","sensor_name": "JQR928","reading_value": "41.3","is_active": "true"
}
]
}
解决方法
我注意到需要更正的三件事。
- Dynamo 表名未正确替换。我们需要将 !Sub 与
tableName: !Ref EventsTable
一起使用
-
"Active" : { "S": "$event.is_active" }
后面有一个不应该出现的逗号。 - 这三个附加参数在batch-write-item api中的根级别并不都是有效的,我暂时删除了它们,我们可以根据需要添加。
"ReturnValues": "UPDATED_NEW","ReturnConsumedCapacity": "NONE",and "ReturnItemCollectionMetrics": "NONE"
更新的请求模板:
requestTemplates:
application/json: !Sub
- |
#set($inputRoot = $input.path('$'))
{
"RequestItems": {
"${tableName}": [
#foreach($event in $inputRoot.event)
{
"PutRequest": {
"Item": {
"UserID" : { "S": "$event.userid" },"TimeUTC" : { "S": "$event.time" },"Lat": { "S": "$event.lat" },"Lng": { "S": "$event.lng" },"UUID" : { "S": "$event.uuid"},"Sensor" : { "S": "$event.sensor_name" },"Reading" : { "S": "$event.reading_value" },"Active" : { "S": "$event.is_active" }
}
}
}#if($foreach.hasNext),#end
#end
]
},"ReturnValues": "UPDATED_NEW","ReturnItemCollectionMetrics": "NONE"
}
- {
tableName: !Ref EventsTable
}
,
我认为这一行是问题所在:
"${self:resources.Resources.EventsTable.Properties.TableName}"
我不确定这是否适用于 API Gateway VTL。我的建议是让它与静态代码一起工作,然后重新添加引用。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。