【问题标题】:How to reference secondary indexes in serverless.yml?如何在 serverless.yml 中引用二级索引?
【发布时间】:2023-03-17 09:01:01
【问题描述】:

我非常不清楚引用或变量如何与 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 是在另一个文件中创建的表格,如下所示:

Resources:
  ReportsTable:
    Type: AWS::DynamoDB::Table
    Properties:
    ...
    LocalSecondaryIndexes:
        - IndexName: typeId-accessToken-index
          KeySchema:
          - AttributeName: typeId
            KeyType: HASH
            ...etc

我知道 Fn::GetAtt 数组中的第二个值引用了一个属性名,但我不明白 Arn 的来源。它看起来像一个变量,但它没有在任何地方定义。

最终我需要添加另一个引用我创建的本地二级索引的效果、动作、资源块,但我不知道从哪里开始。

编辑:看起来 Arn 来自 dynamoDB 表返回值 (https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html)

Edit2:好的,所以我现在有了来自permissions reference docsarn:aws:dynamodb:region:account-id:table/table-name/index/* 格式,现在正在测试。

【问题讨论】:

  • arn:aws:dynamodb:region:account-id:table/table-name/index/* 是正确的参考。现在我需要弄清楚如何像上面所做的那样动态引用表名。以及如何在没有硬编码的情况下引用我的帐户 ID 和区域。

标签: amazon-cloudformation serverless-framework aws-serverless


【解决方案1】:

您可以使用 Cloudformation 内部函数 Sub 创建索引 arn

!Sub '${ReportsTable.Arn}/index/*'

【讨论】:

    【解决方案2】:

    参考这些文档后: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

    我发现引用索引表所需的格式是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/*'
    

    表名在您的自定义块中定义。

    【讨论】:

    • 仅供参考,除非您对 Join 组件进行操作或导入它们,否则您通常可以使用 Sub 而不是 Join,这样更容易阅读:!Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${self:custom.tableName}/index/*
    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 2019-01-14
    • 2020-05-09
    • 2019-03-10
    • 2013-07-25
    • 2013-05-15
    相关资源
    最近更新 更多