2017-08-14 34 views
1

我想製作一個雲形成模板來創建大量的dynamoDB表。我瞭解如何將AttributeDefintions映射到變量,但是可以創建單個資源定義,然後將其與映射變量重新使用?或者我必須靜態聲明每個資源(表)?CloudFormation - 是否可以爲通用和重用資源定義定義映射?

這是什麼,我有4代表的例子,希望通過重新使用資源定義,而不是靜態地列出塊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 
+0

我推薦使用[對流層](https://github.com/cloudtools/troposphere),它是python中的CF DSL,您將獲得真正編程語言的所有優點(hello loops,good bye yaml和json )。 – Raf

回答

2

凝結這沒有循環功能與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。

相關問題