如何引用serverless.yml中的二级索引?
How to reference secondary indexes in serverless.yml?
我不太清楚引用或变量如何与 CloudFormation 一起使用。
目前我的 serverless.yml 中的 iAmRole 看起来像:
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:DescribeTable
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
# Restrict our IAM role permissions to
# the specific table for the stage
Resource:
- "Fn::GetAtt": [ ReportsTable, Arn ]
ReportsTable 是在另一个文件中创建的 table,如下所示:
Resources:
ReportsTable:
Type: AWS::DynamoDB::Table
Properties:
...
LocalSecondaryIndexes:
- IndexName: typeId-accessToken-index
KeySchema:
- AttributeName: typeId
KeyType: HASH
...etc
我知道 Fn::GetAtt 数组中的第二个值引用了一个属性名称,但我不明白 Arn 的来源。它看起来像一个变量,但它没有在任何地方定义。
最终我需要添加另一个引用我创建的本地二级索引的 Effect、Action、Resource 块,但我不知道从哪里开始。
编辑:看起来 Arn 来自 dynamoDB tables return 值 (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html)
Edit2:好的,我现在有来自 permissions reference docs 的格式 arn:aws:dynamodb:region:account-id:table/table-name/index/*
,现在正在测试。
参考这些文档后:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html)
还有这些:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/api-permissions-reference.html
我发现引用索引 table 所需的格式是 arn:aws:dynamodb:region:account-id:table/table-name/index/*
。
此外,为了不对所有值进行硬编码(在我的例子中是因为我有多个暂存环境),您可以像这样进行连接:
Fn::Join:
- ''
-
- 'arn:aws:dynamodb:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- ':table/'
- ${self:custom.tableName}/
- 'index/*'
其中 table 名称在您的自定义块中定义。
您可以使用 Cloudformation 内部函数 Sub 创建索引 arn
!Sub '${ReportsTable.Arn}/index/*'
我不太清楚引用或变量如何与 CloudFormation 一起使用。
目前我的 serverless.yml 中的 iAmRole 看起来像:
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:DescribeTable
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
# Restrict our IAM role permissions to
# the specific table for the stage
Resource:
- "Fn::GetAtt": [ ReportsTable, Arn ]
ReportsTable 是在另一个文件中创建的 table,如下所示:
Resources:
ReportsTable:
Type: AWS::DynamoDB::Table
Properties:
...
LocalSecondaryIndexes:
- IndexName: typeId-accessToken-index
KeySchema:
- AttributeName: typeId
KeyType: HASH
...etc
我知道 Fn::GetAtt 数组中的第二个值引用了一个属性名称,但我不明白 Arn 的来源。它看起来像一个变量,但它没有在任何地方定义。
最终我需要添加另一个引用我创建的本地二级索引的 Effect、Action、Resource 块,但我不知道从哪里开始。
编辑:看起来 Arn 来自 dynamoDB tables return 值 (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html)
Edit2:好的,我现在有来自 permissions reference docs 的格式 arn:aws:dynamodb:region:account-id:table/table-name/index/*
,现在正在测试。
参考这些文档后:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html)
还有这些:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/api-permissions-reference.html
我发现引用索引 table 所需的格式是 arn:aws:dynamodb:region:account-id:table/table-name/index/*
。
此外,为了不对所有值进行硬编码(在我的例子中是因为我有多个暂存环境),您可以像这样进行连接:
Fn::Join:
- ''
-
- 'arn:aws:dynamodb:'
- Ref: AWS::Region
- ':'
- Ref: AWS::AccountId
- ':table/'
- ${self:custom.tableName}/
- 'index/*'
其中 table 名称在您的自定义块中定义。
您可以使用 Cloudformation 内部函数 Sub 创建索引 arn
!Sub '${ReportsTable.Arn}/index/*'