代码之家  ›  专栏  ›  技术社区  ›  Pedro Arantes

防止AWS CloudFormation使用IAM删除DynamoDB

  •  0
  • Pedro Arantes  · 技术社区  · 7 年前

    我正在尝试创建一个AWS角色,该角色可以防止删除表。例如,我创建的表如下所示:

    UsersDynamoDBTable:
        Type: AWS::DynamoDB::Table
        Description: Users DynamoDB Table
        Properties:
          AttributeDefinitions:
            - AttributeName: hashKey
              AttributeType: S
            - AttributeName: rangeKey
              AttributeType: S
          KeySchema:
            - AttributeName: hashKey
              KeyType: HASH
            - AttributeName: rangeKey
              KeyType: RANGE
          BillingMode: PAY_PER_REQUEST
          GlobalSecondaryIndexes:
            - IndexName: index-rangeKey
              KeySchema:
                - AttributeName: rangeKey
                  KeyType: HASH
                - AttributeName: hashKey
                  KeyType: RANGE
              Projection:
                ProjectionType: ALL
    

    现在假设开发人员意外地删除了这些行并更新了堆栈。这样,表及其所有数据都将被删除。因此,我想创建一个角色,防止CloudFormation删除DynamoDB表。我的第一次尝试是创建下面的角色,但没有成功。

    PreventCloudFormationDeleteTableIAMRole:
        Type: AWS::IAM::Role
        Properties:
          AssumeRolePolicyDocument:
            Version: 2012-10-17
            Statement:
              - Effect: Allow
                Principal:
                  Service:
                    - cloudformation.amazonaws.com
                Action:
                  - sts:AssumeRole
          Policies:
            - PolicyName: PreventTableDeletePolicy
              PolicyDocument:
                Version: 2012-10-17
                Statement:
                  - Effect: Deny
                    Action:
                      - dynamodb:DeleteTable
                    Resource:
                      - !Join
                        - '/'
                        - - !Join [':', ['arn:aws:dynamodb', !Sub '${AWS::Region}', '*', 'table']]
                          - !Join ['', [!Sub '${StackName}', '*']]
    

    我是否缺少一些角色配置?

    2 回复  |  直到 5 年前
        1
  •  7
  •   cementblocks    7 年前

    DeletionPolicy 属于 RETAIN 防止在从模板中删除堆栈或表时删除表。还有新的 UpdateReplacePolicy 将防止CloudFormation在由于主键更改而需要删除表时删除该表。

    推荐文章