Home > Back-end >  Conditional dynamo global table in CloudFormation
Conditional dynamo global table in CloudFormation

Time:07-27

I am trying to figure out how to have Global Dynamo table in case a condition a true, otherwise have a regular Dynamo table. However, the linter is complaining about the validity of this: Expected a block but got... at the Properties. Here's my code:

Resources:
    ClientNameTable:
      !If
        - SingleEnv
        - Type: AWS::DynamoDB::GlobalTable
        - Type: AWS::DynamoDB:Table

      Properties:
        TableName: ClientNameTest
        AttributeDefinitions:
          - AttributeName: uuuuu
            AttributeType: S
          - AttributeName: xxxxx
            AttributeType: S
        BillingMode: PAY_PER_REQUEST
        KeySchema:
          - AttributeName: uuuuu
            KeyType: HASH
          - AttributeName: xxxxx
            KeyType: RANGE
        StreamSpecification:
          StreamViewType: NEW_IMAGE
        TimeToLiveSpecification:
            AttributeName: time_to_live
            Enabled: true
        Replicas:
          !If
            - SingleEnv
              - Region: us-east-1
              - Region: us-west-2
            - !Ref "AWS::NoValue"

CodePudding user response:

You can't put If like that. Instead you have to have two separate resources.

Resources:
    ClientNameTable:
      Condition: SingleEnv  
      Properties:
        TableName: ClientNameTest
        AttributeDefinitions:
          - AttributeName: uuuuu
            AttributeType: S
          - AttributeName: xxxxx
            AttributeType: S
        BillingMode: PAY_PER_REQUEST
        KeySchema:
          - AttributeName: uuuuu
            KeyType: HASH
          - AttributeName: xxxxx
            KeyType: RANGE
        StreamSpecification:
          StreamViewType: NEW_IMAGE
        TimeToLiveSpecification:
            AttributeName: time_to_live
            Enabled: true
        Replicas:
          !If
            - SingleEnv
              - Region: us-east-1
              - Region: us-west-2
            - !Ref "AWS::NoValue"
            
            
    ClientNameTable:
      Condition: NotSingleEnv  
      Properties:
        TableName: ClientNameTest
        AttributeDefinitions:
          - AttributeName: uuuuu
            AttributeType: S
          - AttributeName: xxxxx
            AttributeType: S
        BillingMode: PAY_PER_REQUEST
        KeySchema:
          - AttributeName: uuuuu
            KeyType: HASH
          - AttributeName: xxxxx
            KeyType: RANGE
        StreamSpecification:
          StreamViewType: NEW_IMAGE
        TimeToLiveSpecification:
            AttributeName: time_to_live
            Enabled: true
        Replicas:
          !If
            - SingleEnv
              - Region: us-east-1
              - Region: us-west-2
            - !Ref "AWS::NoValue"            

Where NotSingleEnv you have to create in Conditions.

  • Related