在 CloudFormation 中使用条件和参数
Using Conditions and Parameters in CloudFormation
我正在尝试根据可选参数创建条件。该选项是是否 运行 从我的 EC2 部署的 userData 脚本中进行一些额外的安装。
参数和条件如下所示:
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 ]
有人可以提供一些关于如何实现此条件的反馈或为什么会为此实现抛出此错误消息吗?
根据docs,条件应该用在你想要有条件地创建的资源的顶层。
不支持将 Condition
放入实例 UserData
部分。
要在您的情况下使用条件,您需要根据参数有条件地创建单独的资源。
Resources:
Ec2InstanceAPMOnly:
Type: AWS::EC2::Instance
Condition: apmOnly
Properties:
InstanceType: t2.micro
ImageId: ami-9c9443e3 #Amazon Linux AMI in Tokyo
KeyName: tokyocloudformation
IamInstanceProfile: 'S3EC2'
SecurityGroupIds:
- !Ref myNewSecurityGroup
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
installstuff
Ec2InstanceWithInfrastructureAgent:
Type: AWS::EC2::Instance
Condition: addInfrastructureAgent
Properties:
InstanceType: t2.micro
ImageId: ami-9c9443e3 #Amazon Linux AMI in Tokyo
KeyName: tokyocloudformation
IamInstanceProfile: 'S3EC2'
SecurityGroupIds:
- !Ref myNewSecurityGroup
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
installstuff
installsomeotherstuff
您也可以在 userData 中使用条件。我遵循了这个 post https://www.singlestoneconsulting.com/blog/cloudformation-mapping-and-conditionals-making-your-templates-more-universal/ 并且效果很好
我正在尝试根据可选参数创建条件。该选项是是否 运行 从我的 EC2 部署的 userData 脚本中进行一些额外的安装。
参数和条件如下所示:
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 ]
有人可以提供一些关于如何实现此条件的反馈或为什么会为此实现抛出此错误消息吗?
根据docs,条件应该用在你想要有条件地创建的资源的顶层。
不支持将 Condition
放入实例 UserData
部分。
要在您的情况下使用条件,您需要根据参数有条件地创建单独的资源。
Resources:
Ec2InstanceAPMOnly:
Type: AWS::EC2::Instance
Condition: apmOnly
Properties:
InstanceType: t2.micro
ImageId: ami-9c9443e3 #Amazon Linux AMI in Tokyo
KeyName: tokyocloudformation
IamInstanceProfile: 'S3EC2'
SecurityGroupIds:
- !Ref myNewSecurityGroup
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
installstuff
Ec2InstanceWithInfrastructureAgent:
Type: AWS::EC2::Instance
Condition: addInfrastructureAgent
Properties:
InstanceType: t2.micro
ImageId: ami-9c9443e3 #Amazon Linux AMI in Tokyo
KeyName: tokyocloudformation
IamInstanceProfile: 'S3EC2'
SecurityGroupIds:
- !Ref myNewSecurityGroup
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
installstuff
installsomeotherstuff
您也可以在 userData 中使用条件。我遵循了这个 post https://www.singlestoneconsulting.com/blog/cloudformation-mapping-and-conditionals-making-your-templates-more-universal/ 并且效果很好