微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用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. 

解决方法

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。