amazon-web-services - CloudFormation - 是否可以为通用且重用的资源定义定义映射?

标签 amazon-web-services aws-cloudformation

我想制作一个云形成模板来创建大量的dynamoDB表。我了解如何将 AttributeDefintions 映射到变量,但是是否可以创建单个资源定义,然后将其与映射的变量一起重新使用?或者我必须静态声明每个资源(表)?

这是我拥有 4 个表的示例,希望通过重新使用资源定义来压缩它,而不是静态列出 block 4 次

Parameters:
  ReadCapacityUnits:
    Type: String
    Default: "2"
  WriteCapacityUnits:
    Type: String
    Default: "2"
Resources:
  DynamoTableTotalCountsHour:
    Type: "AWS::DynamoDB::Table"
    Properties:
      AttributeDefinitions:
        -
          AttributeName: "UserId"
          AttributeType: "S"
        -
          AttributeName: "RangeId"
          AttributeType: "S"
      KeySchema:
        -
          AttributeName: "UserId"
          KeyType: "HASH"
        -
          AttributeName: "RangeId"
          KeyType: "RANGE"
      ProvisionedThroughput:
        ReadCapacityUnits: !Ref ReadCapacityUnits
        WriteCapacityUnits: !Ref WriteCapacityUnits
      TableName: TotalCountsHour
  DynamoTableTotalCountsDay:
    Type: "AWS::DynamoDB::Table"
    Properties:
      AttributeDefinitions:
        -
          AttributeName: "UserId"
          AttributeType: "S"
        -
          AttributeName: "RangeId"
          AttributeType: "S"
      KeySchema:
        -
          AttributeName: "UserId"
          KeyType: "HASH"
        -
          AttributeName: "RangeId"
          KeyType: "RANGE"
      ProvisionedThroughput:
        ReadCapacityUnits: !Ref ReadCapacityUnits
        WriteCapacityUnits: !Ref WriteCapacityUnits
      TableName: TotalCountsDay
  DynamoTableTotalCountsMonth:
    Type: "AWS::DynamoDB::Table"
    Properties:
      AttributeDefinitions:
        -
          AttributeName: "UserId"
          AttributeType: "S"
        -
          AttributeName: "RangeId"
          AttributeType: "S"
      KeySchema:
        -
          AttributeName: "UserId"
          KeyType: "HASH"
        -
          AttributeName: "RangeId"
          KeyType: "RANGE"
      ProvisionedThroughput:
        ReadCapacityUnits: !Ref ReadCapacityUnits
        WriteCapacityUnits: !Ref WriteCapacityUnits
      TableName: TotalCountsMonth
  DynamoTableTotalCountsYear:
    Type: "AWS::DynamoDB::Table"
    Properties:
      AttributeDefinitions:
        -
          AttributeName: "UserId"
          AttributeType: "S"
        -
          AttributeName: "RangeId"
          AttributeType: "S"
      KeySchema:
        -
          AttributeName: "UserId"
          KeyType: "HASH"
        -
          AttributeName: "RangeId"
          KeyType: "RANGE"
      ProvisionedThroughput:
        ReadCapacityUnits: !Ref ReadCapacityUnits
        WriteCapacityUnits: !Ref WriteCapacityUnits
      TableName: TotalCountsYear

最佳答案

CloudFormation 本身没有循环函数。

您可以使用Nested Stacks重用 DynamoDB 定义并最大程度地减少重复代码量。

例如从一个堆栈调用另一个堆栈:

Type: "AWS::CloudFormation::Stack"
    Properties:
       Parameters:
          ReadCapacityUnits: 2
          WriteCapacityUnits: 2
       TemplateURL: Url-of-S3-Bucket-with-DynamoDB-Template-Stack

请注意,使用包含许多表的嵌套堆栈确实意味着,如果您需要对堆栈进行某些类型的更新,您将面临必须同时删除/替换所有 DynamoDB 表的风险。

如果您不希望 DynamoDB 表的构建之间存在依赖关系,请使用带有外部编排引擎的模板堆栈来循环参数并重复调用 AWS CloudFormation API。

关于amazon-web-services - CloudFormation - 是否可以为通用且重用的资源定义定义映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45666592/

相关文章:

java - 为什么我的带有 SQS 的 Spring Boot 应用程序不显示 JMSHealthIndicator?

amazon-web-services - 如何将TIMESTAMP列插入Redshift

templates - 无法将参数从子 CloudFormation 模板发送到另一个子模板

amazon-web-services - AWS CloudFormation VPC CIDR 分配给安全组

aws-cloudformation - AWS 认知 : user pool delete protection?

mongodb - AWS Mongo QuickStart 从未完成

unit-testing - 测试 AWS 现货实例供应

php - 可以在AWS RDS中动态创建数据库吗?

amazon-web-services - 如何从 AWS Lambda 函数返回存储在 AWS S3 存储桶上的 HTML 页面

aws-cloudformation - 如何通过 cloudformation 允许我的 WAF v2 使用某些 IP 集资源?