如何使用CloudFormation中的一项创建多个资源(名称不同)

时间:2020-09-17 14:23:13

标签: amazon-cloudformation

这是我的CloudFormation模板的一部分:

resources:
  DynamoDBTablePoints:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: points
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'

  DynamoDBTableScore:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: score
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'

  DynamoDBTableName:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: name
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'

它将创建3个DynamoDB资源。这仅是示例,不要只关注名称或所创建的表是否正确。我想知道如何将这种策略应用于任何资源。

如您所见,有3种几乎相同的资源,只是名称不同。我怎么有这样一个条目:

resources:
  DynamoDBTable[Point, Score, Name]:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: [point, score, name]
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: '5'
        WriteCapacityUnits: '5'
'''
So I get the same result as with the original template. 

1 个答案:

答案 0 :(得分:0)

您可以重构模板以使用nested stacks。使用嵌套堆栈,您可以在单独的模板中定义dynamodb模板表,并将其上传到S3。

然后在主模板中,您将使用AWS::CloudFormation::Stack来引用dynamodb模板。

在您的主模板中,您仍然会有三个AWS::CloudFormation::Stack实例,它们会更短,并且在使用CloudFormation开发时会遵循良好做法