2016-12-12 51 views
0

我正在寫一個雲形成模板,並在我的堆棧中創建資源取決於環境。
因此,我檢查參數(環境)的值,並基於它創建該資源(條件:ISProduction)。
但是,我的問題是,如果創建資源(MyProductionResource),另一個資源(AnotherResource)變得依賴於它並需要使用另一個資源(MyProductionResource)的輸出屬性。
下面的代碼:AWS雲形成條件取決於

Conditions: 
    ISProduction: 
    "Fn::Equals": 
     - !Ref Environment 
     - production 
... 

MyProductionResource: 
    Type: AWS::CloudFormation::Stack 
    Condition: ISProduction 
    Properties: 
    [.. properties..] 

AnotherResource: 
    Type: AWS::CloudFormation::Stack 
    DependsOn: 
     - AResource 
     - MyProductionResource 
    Properties: 
     TemplateURL: whatever 
     Parameters: 
     AParameter: !GetAtt MyProductionResource.Outputs.SomeString 

我的問題是,我想AnotherResource依賴於MyProductionResource只有當ISProduction是真實的。一個想法是在DependsOn項目中添加某種條件,或者任何會帶來相同結果的條件。
我如何在AWS Cloud Formation上做到這一點?
此外,我不確定在dependsOn列表中列出的資源未創建時發生了什麼。雲形成模板會產生錯誤嗎?我怎樣才能使這個屬性讀取安全!GetAtt MyProductionResource.Outputs.SomeString?

回答

1

可以使用!如果參數

AParameter: !If [ISProduction, !GetAtt MyProductionResource.Outputs.SomeString, "default value?!?"] 

可惜DependsOn不允許FN ::如果。

所以你可以創建資源兩次。

AnotherProductionResource: 
    Type: AWS::CloudFormation::Stack 
    Condition: ISProduction 
    DependsOn: 
    - AResource 
    - MyProductionResource 
    Properties: 
    [...] 
AnotherNonProductionResource: 
    Type: AWS::CloudFormation::Stack 
    Condition: ISNotProduction 
    DependsOn: 
    - AResource 
    Properties: 
    [...] 

但是有這麼多的ifs有點違揹你的環境應該儘可能相似的想法。那麼也許你可以擺脫這一切?