【问题标题】:What is difference between string, list and list of string in cloudformation?cloudformation中的字符串,列表和字符串列表有什么区别?
【发布时间】:2021-07-03 08:36:48
【问题描述】:

cloudformation中的字符串、列表和字符串列表有什么区别?

当我使用 if 条件在 dynamodb 中提供 NonKeyAttributes 时。 当我尝试使用 if 条件提供新条件 z 时,我得到属性 NonKeyAttributes 的值必须是字符串列表类型。 还有没有更好的方法来做到这一点,而不是太多的 if 条件

条件: x: ! 或 [ !Equals [ !Ref env, "prod" ], !Equals [ !Ref env, "acpt" ] ] y: ! 或 [ !Equals [ !Ref env, "infrastructure" ], !Equals [ !Ref env, "cont" ] ] z: ! 或 [ !Equals [ !Ref env, "dev" ], !Equals [ !Ref env, "test" ], [ !Ref env, "prod" ], !Equals [ !Ref env, "cont" ] ]

Type: "AWS::DynamoDB::Table"
Properties:
  TableName: Employer
  AttributeDefinitions:
    - AttributeName: "empID"
      AttributeType: "S"
    - AttributeName: "TeamID"
      AttributeType: "S"
    - AttributeName: "Date"
      AttributeType: "N"
  KeySchema:
    - AttributeName: "empID"
      KeyType: "HASH"
  ProvisionedThroughput:
    ReadCapacityUnits: 20
    WriteCapacityUnits: 20
  StreamSpecification: 
    StreamViewType: NEW_AND_OLD_IMAGES
  PointInTimeRecoverySpecification:
    PointInTimeRecoveryEnabled: true
  GlobalSecondaryIndexes:
    - IndexName: "ByTeamID"
      KeySchema:
      - AttributeName: "TeamID"
        KeyType: "HASH"
      - AttributeName: "Date"
        KeyType: "RANGE"
      Projection:
        NonKeyAttributes:
          - "A"
          - "B"
          - "C"
          - "D"
          - !If [x, "x1", !Ref "AWS::NoValue"]
          - !If [y, "y1", !Ref "AWS::NoValue" ]
          - !If 
              - z
              - - "z1"
              - - "z2"
              - - "z3"
              - - "z4"
              - !Ref AWS::NoValue

【问题讨论】:

  • 您是在尝试仅在某些情况下投影属性,还是仅在某些情况下才存在属性?换句话说,您是说仅在某些条件为真时才项目 X,还是仅在某些条件为真时才存在 X,因此您不想包含它?
  • 如果 x 条件为真,那么它将提供 x1 值。当条件满足时,如果它具有单一值,我就可以做到。但是对于 z 条件,我的值不止 z1, z2, z3

标签: amazon-dynamodb amazon-cloudformation


【解决方案1】:

如果您真的只需要在条件为真时投影一个属性,并且有多个属性要包含在特定条件下,那么您必须为每个属性重复 !If

Type: "AWS::DynamoDB::Table"
Properties:
  TableName: Employer
  AttributeDefinitions:
    - AttributeName: "empID"
      AttributeType: "S"
    - AttributeName: "TeamID"
      AttributeType: "S"
    - AttributeName: "Date"
      AttributeType: "N"
  KeySchema:
    - AttributeName: "empID"
      KeyType: "HASH"
  ProvisionedThroughput:
    ReadCapacityUnits: 20
    WriteCapacityUnits: 20
  StreamSpecification: 
    StreamViewType: NEW_AND_OLD_IMAGES
  PointInTimeRecoverySpecification:
    PointInTimeRecoveryEnabled: true
  GlobalSecondaryIndexes:
    - IndexName: "ByTeamID"
      KeySchema:
      - AttributeName: "TeamID"
        KeyType: "HASH"
      - AttributeName: "Date"
        KeyType: "RANGE"
      Projection:
        NonKeyAttributes:
          - "A"
          - "B"
          - "C"
          - "D"
          - !If [x, "x1", !Ref "AWS::NoValue"]
          - !If [y, "y1", !Ref "AWS::NoValue" ]
          - !If [z, "z1", !Ref "AWS::NoValue" ]
          - !If [z, "z2", !Ref "AWS::NoValue" ]
          - !If [z, "z3", !Ref "AWS::NoValue" ]
          - !If [z, "z4", !Ref "AWS::NoValue" ]

【讨论】:

  • 我确实喜欢这个,它奏效了。但是有没有其他方法可以避免多次重复 if 条件?
  • 我想不到。老实说,我从来没有考虑过投影是我可以做的条件。如果数据不同,我只会包括所有变体。如果属性不在数据中,我认为它不会在意。如果你真的想要一个始终在数据中的值有条件地出现在 GSI 中,那么我认为你必须这样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-17
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
相关资源
最近更新 更多