【问题标题】:Prevent AWS CloudFormation from deleting DynamoDB using IAM防止 AWS CloudFormation 使用 IAM 删除 DynamoDB
【发布时间】:2019-06-25 00:13:02
【问题描述】:

我正在尝试创建一个阻止 CloudFormation 删除表的 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}', '*']]

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

谢谢。

【问题讨论】:

  • 你是如何使用这个角色的?你是在创建栈的时候传入的吗?
  • 是的。我在创建表格的同一模板上传递了它
  • 当您创建堆栈时,您可以为 cloudformation 在创建/更新/删除资源时指定一个角色 arn。该角色当然不能由堆栈创建。

标签: amazon-web-services amazon-cloudformation amazon-iam


【解决方案1】:

您可以使用RETAINDeletionPolicy 来防止在删除堆栈或从模板中删除表格时删除表格。此外,新的UpdateReplacePolicy 将阻止 CloudFormation 在由于主键更改而需要删除表时。

【讨论】:

    【解决方案2】:

    考虑到角色已正确附加到调用用户/主体,该连接中的策略 Arn 是否可能与表 Arn 不匹配?

    还可以考虑保留资源而不是拒绝操作: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-deletionpolicy.html

    【讨论】:

      猜你喜欢
      • 2021-10-15
      • 2018-04-13
      • 2021-02-10
      • 1970-01-01
      • 2018-10-07
      • 2018-08-19
      • 2014-05-29
      • 2018-10-25
      • 2019-02-21
      相关资源
      最近更新 更多