Dav*_*ron 3 amazon-dynamodb serverless-framework dynamodb-queries aws-serverless
我有一个用DynamoDB表定义的API /服务。我有几个索引(定义为全局二级索引)来支持几个查询。我设计了具有GSI定义的表,以及看起来像正确的查询的表。但是,在进行查询时出现此异常:
{ AccessDeniedException: User: arn:aws:sts::OBSCURED:assumed-role/chatroom-application-dev-us-east-1-lambdaRole/chatroom-application-dev-getRoomMessages is not authorized to perform: dynamodb:Query on resource: arn:aws:dynamodb:us-east-1:OBSCURED:table/messages-table-dev/index/roomIndex
at Request.extractError (/var/task/node_modules/aws-sdk/lib/protocol/json.js:48:27)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/var/task/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/var/task/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/task/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/task/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/var/task/node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:115:18)
message: 'User: arn:aws:sts::OBSCURED:assumed-role/chatroom-application-dev-us-east-1-lambdaRole/chatroom-application-dev-getRoomMessages is not authorized to perform: dynamodb:Query on resource: arn:aws:dynamodb:us-east-1:OBSCURED:table/messages-table-dev/index/roomIndex',
code: 'AccessDeniedException',
time: 2018-06-02T22:05:46.110Z,
requestId: 'OBSCURED',
statusCode: 400,
retryable: false,
retryDelay: 30.704899664776054 }
Run Code Online (Sandbox Code Playgroud)
在异常的最上方,它说我的getRoomMessages方法is not authorized to perform: dynamodb:Query on resource:的ARN并显示全局二级索引的ARN。
显然,我需要定义策略以授予访问全局二级索引的权限。但这还不清楚。我已经看到了有关DynamoDB的其他StackOverflow问题,他们抱怨文档零散,很难找到任何东西。我必须同意。“碎片化”一词太温和了。
我正在使用无服务器框架。本provider部分显示此策略/角色定义:
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: us-east-1
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
- { "Fn::GetAtt": ["MessagesDynamoDBTable", "Arn" ] }
- { "Fn::GetAtt": ["#roomIndex", "Arn" ] }
- { "Fn::GetAtt": ["#userIndex", "Arn" ] }
environment:
MESSAGES_TABLE: ${self:custom.tableName}
Run Code Online (Sandbox Code Playgroud)
在本Resource节中,我相信我应该列出声明其权限的资源。第一个引用整个表。我刚刚添加了最后两个,并引用了索引。
编辑:当我运行serverless deploy以下消息时打印:
The CloudFormation template is invalid: Template error: instance of Fn::GetAtt references undefined resource #roomIndex
Run Code Online (Sandbox Code Playgroud)
我为此尝试了几种变体,只是得到了相同的错误。归结为- 我如何serverless.yml使用Cloudfront语法获取索引的ARN。由于存在异常,因此ARN确实存在。
DynamoDB表定义:
resources:
Resources:
MessagesDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: messageId
AttributeType: S
- AttributeName: room
AttributeType: S
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: messageId
KeyType: HASH
GlobalSecondaryIndexes:
- IndexName: roomIndex
KeySchema:
- AttributeName: room
KeyType: HASH
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
- IndexName: userIndex
KeySchema:
- AttributeName: userId
KeyType: HASH
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.tableName}
Run Code Online (Sandbox Code Playgroud)
使用的查询对应于上述异常:
{
"TableName": "messages-table-dev",
"IndexName": "roomIndex",
"KeyConditionExpression": "#roomIndex = :room",
"ExpressionAttributeNames": {
"#roomIndex": "room"
},
"ExpressionAttributeValues": {
":room": {
"S": "everyone"
}
}
}
Run Code Online (Sandbox Code Playgroud)
生成查询的Lambda函数代码段:
app.get('/messages/room/:room', (req, res) => {
const params = {
TableName: MESSAGES_TABLE,
IndexName: "roomIndex",
KeyConditionExpression: '#roomIndex = :room',
ExpressionAttributeNames: { '#roomIndex': 'room' },
ExpressionAttributeValues: {
":room": { S: `${req.params.room}` }
},
};
console.log(`QUERY ROOM ${JSON.stringify(params)}`);
dynamoDb.query(params, (error, result) => {
if (error) {
console.log(error);
res.status(400).json({ error: 'Could not get messages' });
} else {
res.json(result.Items);
}
});
});
Run Code Online (Sandbox Code Playgroud)
我发现比jake.lang发布的答案更好。编辑:我没有看到他的第二条评论,他在其中提出了以下建议。
正如他指出的那样,他的说法是错误的,因为ARN可以出于正当理由进行更改。但是,由于全局二级索引的ARN会将“ / INDEXNAME”附加到表的ARN上,因此出现了解决方案。这意味着该策略声明可以是:
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource:
- { "Fn::GetAtt": ["MessagesDynamoDBTable", "Arn" ] }
- { "Fn::Join": [ "/", [
{ "Fn::GetAtt": ["MessagesDynamoDBTable", "Arn" ] }, "index", "roomIndex"
]]}
- { "Fn::Join": [ "/", [
{ "Fn::GetAtt": ["MessagesDynamoDBTable", "Arn" ] }, "index", "userIndex"
]]}
Run Code Online (Sandbox Code Playgroud)
“ Fn :: Join”位来自CloudFormation,并且是“ join”操作。它需要一个字符串数组,并使用第一个参数将它们连接起来。因此,计算此策略声明中所需的ARN是一种相当复杂且过于复杂的方法。
有关文档,请参阅:https : //docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-join.html
而不是Fn::Join您可以使用!Sub "${MessagesDynamoDBTable.Arn}",它更简单。此外,如果您想访问所有索引(这通常是我的情况),那么这/index/*就是您所需要的。
例子:
...
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- dynamodb:Query
Resource:
- !Sub "${MessagesDynamoDBTable.Arn}"
- !Sub "${MessagesDynamoDBTable.Arn}/index/*"
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2128 次 |
| 最近记录: |