S3存储桶上复制配置的CFN条件



我知道地形中存在动态块来创建资源上的特定配置,但CloudFormation是否存在这种情况?我所追求的是关闭和打开S3存储桶的复制。目前,我只是在部署时注释掉复制部分。

SpeedDialBucket:
  Type: AWS::S3::Bucket
  Condition: IsPrimaryRegion
  Properties:
    BucketName: !Sub "voip-speed-dial-${StageName}"
    PublicAccessBlockConfiguration:
      BlockPublicAcls: True
      BlockPublicPolicy: True
      IgnorePublicAcls: True
      RestrictPublicBuckets: True
    VersioningConfiguration:
      Status: Enabled
    # THIS HAS TO BE COMMENTED OUT ON FIRST DEPLOY in MULTIREGION
    # ReplicationConfiguration:
    #   Role: !GetAtt SpeedDialBucketReplicationRole.Arn
    #   Rules:
    #     - Status: Enabled
    #       Destination:
    #         Bucket: !Join [ '', [ 'arn:aws:s3:::', !Join  [ '-', [ !Ref SpeedDialBucketName, 'second', !Ref StageName ]]]]
    #         StorageClass: STANDARD

是的,你可以这样做,但你需要有一些条件来启用/禁用这个块,就像在地形中一样。您可以使用ParametersConditions和If来执行此操作。例如:

Parameters:
    CreateReplicationConfiguration:
        Type: String
        Default: false
        AllowedValues: [true, false]        
        
Conditions:
    ShloudCreateReplicationConfiguration:
        !Equals [!Ref CreateReplicationConfiguration, true]
    
Resources:
    SpeedDialBucket:
    Type: AWS::S3::Bucket
    Condition: IsPrimaryRegion
    Properties:
        BucketName: !Sub "voip-speed-dial-${StageName}"
        PublicAccessBlockConfiguration:
        BlockPublicAcls: True
        BlockPublicPolicy: True
        IgnorePublicAcls: True
        RestrictPublicBuckets: True
        VersioningConfiguration:
        Status: Enabled
        ReplicationConfiguration:
            !If
               - ShloudCreateReplicationConfiguration
               - Role: !GetAtt SpeedDialBucketReplicationRole.Arn
                 Rules:
                   - Status: Enabled
                     Destination:
                       Bucket: !Join [ '', [ 'arn:aws:s3:::', !Join  [ '-', [ !Ref SpeedDialBucketName, 'second', !Ref StageName ]]]]
                       StorageClass: STANDARD   
               - !Ref "AWS::NoValue"

最新更新