【发布时间】:2019-01-05 02:06:31
【问题描述】:
我正在尝试基于可选参数创建条件。选项是是否从我的 userData 脚本运行一些额外的安装以进行 EC2 部署。
参数和条件如下所示:
Parameters:
EnvType:
Description: Option to install New Relic Infrastructure.
Default: apm
Type: String
AllowedValues:
- apm
- +infra
然后是我的带有条件启动脚本的 EC2 资源
Resources:
Ec2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: ami-9c9443e3 #Amazon Linux AMI in Tokyo
KeyName: tokyocloudformation
IamInstanceProfile: 'S3EC2'
SecurityGroupIds:
- !Ref myNewSecurityGroup
UserData:
Condition: apmOnly
Fn::Base64:
|
#!/bin/bash
installstuff
Condition: addInfrastructureAgent
Fn::Base64:
|
#!/bin/bash
installstuff
installsomeotherstuff
我收到的错误消息是: 模板验证错误:模板格式错误:未解决的依赖项 [EnvTyp]。无法在模板的条件块中引用资源
我明白错误所说的我相信,但它似乎不符合 AWS 给出的示例。
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/conditions-section-structure.html 。 此 AWS 示例清楚地在条件块中使用了 !Ref。
EnvType:
Description: Environment type.
Default: test
Type: String
AllowedValues:
- prod
- test
ConstraintDescription: must specify prod or test.
Conditions:
CreateProdResources: !Equals [ !Ref EnvType, prod ]
有人可以就如何实现此条件或为什么为此实现抛出此错误消息提供一些反馈吗?
【问题讨论】:
-
您应该调整脚本而不是资源。每种情况下的资源都是相同的。只有脚本会改变。
-
谢谢!我还发现我需要以下(cfn-hup)在 userData 中有条件地实现,以便脚本在重新启动或从 cfn 更新时再次运行。留在这里留给后人。 docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
标签: amazon-web-services amazon-cloudformation