基于DependsOn的AWS云形成条件 [英] AWS Cloud Formation Conditions on DependsOn
问题描述
我正在编写云形成模板,并且在堆栈中创建资源取决于环境.
因此,我检查参数(环境)的值,然后基于该参数创建该资源(条件:ISProduction).
但是,我的问题是,在创建资源(MyProductionResource)的情况下,另一个资源(AnotherResource)变得依赖于该资源,并且需要使用另一个资源(MyProductionResource)的输出属性.
这里的代码:
I am writing a cloud formation template and the creation of a resource in my stack it depends on the environment.
Therefore, I check the value of a parameter (Environment), and based on it I create that resource (Condition: ISProduction).
However, my problem is that in case that resource is created (MyProductionResource) another resource (AnotherResource) becomes dependent on it and needs to use an output attribute from the other (MyProductionResource).
Here the code:
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为true时,我才希望AnotherResource依赖于MyProductionResource.一个想法是在DependsOn项中添加某种条件,或者添加任何会带来相同结果的条件.
如何在AWS云形成上做到这一点?
另外,我不确定当未创建dependsOn列表中列出的资源时会发生什么.云形成模板会产生错误吗?如何使此属性读取安全性!GetAtt MyProductionResource.Outputs.SomeString?
My problem is that I want AnotherResource to be dependent on MyProductionResource only when ISProduction is true. An idea is to add some kind of conditions in the DependsOn item, or anything that would bring to the same result.
How can I do that on AWS Cloud Formation?
Also I am not sure what happen when the resource that is listed in the dependsOn list is not created. Would the cloud formation template generate an error? How can I make this attribute read safety !GetAtt MyProductionResource.Outputs.SomeString ?
推荐答案
您可以使用!If作为参数
you can use !If for the parameter
AParameter: !If [ISProduction, !GetAtt MyProductionResource.Outputs.SomeString, "default value?!?"]
但不幸的是DependsOn不允许Fn :: If.
but unfortunately DependsOn does not allow Fn::If.
因此您可以两次创建资源.
So you could create to resource twice.
AnotherProductionResource:
Type: AWS::CloudFormation::Stack
Condition: ISProduction
DependsOn:
- AResource
- MyProductionResource
Properties:
[...]
AnotherNonProductionResource:
Type: AWS::CloudFormation::Stack
Condition: ISNotProduction
DependsOn:
- AResource
Properties:
[...]
但是拥有太多ifs的想法与您的环境应该尽可能相似的想法背道而驰.所以也许您可以摆脱这整个事情?
But having so many ifs is kind of against the idea that your environments should be as similar as possible. So maybe you can get rid of this whole thing?
这篇关于基于DependsOn的AWS云形成条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!