![](/img/trans.png)
[英]AWS: Cloud Formation: Is it possible to use multiple “DependsOn”?
[英]AWS Cloud Formation Conditions on DependsOn
我正在編寫一個雲形成模板,並且在我的堆棧中創建一個資源取決於環境。
因此,我檢查參數(環境)的值,並基於它創建該資源(條件:ISProduction)。
但是,我的問題是,在創建資源 (MyProductionResource) 的情況下,另一個資源 (AnotherResource) 依賴於它並且需要使用另一個 (MyProductionResource) 的輸出屬性。
這里的代碼:
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
我的問題是,只有當 ISProduction 為真時,我才希望 AnotherResource 依賴於 MyProductionResource。 一個想法是在 DependsOn 項中添加某種條件,或者任何可以帶來相同結果的條件。
我如何在 AWS Cloud Formation 上做到這一點?
此外,我不確定當未創建dependsOn 列表中列出的資源時會發生什么。 雲形成模板會產生錯誤嗎? 我怎樣才能使這個屬性讀取安全!GetAtt MyProductionResource.Outputs.SomeString ?
您可以使用 !If 作為參數
AParameter: !If [ISProduction, !GetAtt MyProductionResource.Outputs.SomeString, "default value?!?"]
但不幸的是 DependsOn 不允許 Fn::If。
所以你可以創建資源兩次。
AnotherProductionResource:
Type: AWS::CloudFormation::Stack
Condition: ISProduction
DependsOn:
- AResource
- MyProductionResource
Properties:
[...]
AnotherNonProductionResource:
Type: AWS::CloudFormation::Stack
Condition: ISNotProduction
DependsOn:
- AResource
Properties:
[...]
但是擁有如此多的 if 與您的環境應該盡可能相似的想法背道而馳。 所以也許你可以擺脫這整個事情?
這是“DependsOn 不允許 Fn::If”的替代方法。
Conditions:
CreateConfigRecorder: !Equals [ !Ref ConfigRecorderExists, 'false' ]
Resource:
#my 1st AWS Resource
ConfigRecorder:
Condition: CreateConfigRecorder
Type: AWS::Config::ConfigurationRecorder
*more codes below*
#added, since DependsOn: !If is not possible, trigger by WaitCondition if CreateConfigRecorder is true
#Hacks: https://garbe.io/blog/2017/07/17/cloudformation-hacks/
ConfigRecorderWaitHandle:
Condition: CreateConfigRecorder
DependsOn: ConfigRecorder
Type: "AWS::CloudFormation::WaitConditionHandle"
#added, since DependsOn: !If is not possible, trigger by WaitCondition if CreateConfigRecorder is false
WaitHandle:
Type: "AWS::CloudFormation::WaitConditionHandle"
#added, since DependsOn: !If is not possible
WaitCondition:
Type: "AWS::CloudFormation::WaitCondition"
Properties:
Handle: !If [CreateConfigRecorder, !Ref ConfigRecorderWaitHandle, !Ref WaitHandle]
Timeout: "1"
Count: 0
#my 2nd AWS Resource that requires DependsOn Attribute
AWSConfigRule:
Type: AWS::Config::ConfigRule
DependsOn: WaitCondition #added, since DependsOn: !If is not possible
*more codes below*
在運行 CFN 之前,如果我的第一個資源不存在,基本上我的第二個資源只有 DependsOn 屬性。 我從: https : //garbe.io/blog/2017/07/17/cloudformation-hacks/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.