模板验证错误:模板错误:Fn :: If中未解决的条件依赖性BackupSize [英] Template validation error: Template error: unresolved condition dependency BackupSize in Fn::If
问题描述
我正在编写CF代码以启动ec2实例,这就是我的代码:
我正面临以下两个问题:
1)我收到此错误模板验证错误:模板错误:Fn :: If中未解决的条件依赖性BackupSize
2)我想要从映射USERDATA中加入参数名称。 (其余的userdata可以正常工作,但是这种联接不起作用,只是将相同的代码放入userdata中。
有人可以帮我吗?
AWSTemplateFormatVersion: 2010-09-09
说明:此模板应仅用于部署测试服务器
映射:
地区:
us-east-1:
AMI: ami-x
VPC: vpc- x
SUBNET:子网-x
USERDATA: .example.com
SHARE: server1:/ share
SecurityGroups : sg-x
SecurityGroups2: sg-y
参数:
ApplSize:
描述:请输入应用程序卷。size
类型: String
BackupSize:
描述:请输入备份卷大小
类型: String
资源:
EC2实例:
类型: AWS :: E C2 :: Instance
属性:
ImageId:!FindInMap [区域,!Ref AWS :: Region,AMI]
InstanceType:!Ref InstanceType
SubnetId:!FindInMap [区域,!Ref AWS :: Region,子网]
SecurityGroupIds:
-!FindInMap [区域,!Ref AWS :: Region,安全组]
-!FindInMap [区域,!Ref AWS :: Region,SecurityGroups2]
BlockDeviceMappings:
-
DeviceName: / dev / sda1
Ebs:
VolumeSize: 20
VolumeType:gp2
-
DeviceName: / dev / sde
Ebs:
VolumeSize:!Ref ApplSize
VolumeType:gp2
-
DeviceName: / dev / sdc
Ebs:
VolumeSize: 5
VolumeType:gp2
-Fn :: If:
-BackupSize
-
DeviceName: / dev / sdg
Ebs:
VolumeSize:!Ref BackupSize
VolumeType:gp2
-!Ref AWS :: NoValue
UserData:
Fn: :Base64:!Sub |
#!/ bin / bash
NEW_HOSTNAME = Fn :: Join:[,[!Ref Name,Fn :: FindInMap:
[Regions,!Ref AWS :: Region ,USERDATA]]]
主机名$ NEW_HOSTNAME
myshortname =`hostname -s
如果我在参数中输入任何值,并且如果我将backupsize值留为空白,则我希望模板创建备份卷。
所提供的模板的各种版本都有基本的格式设置问题。最新版本(在此答案下方的评论中附加):
▶aws cloudformation validate-template --template-body文件: //cloudformation.yml
$中不允许使用'null'值p $ p>
调用ValidateTemplate操作时发生错误(ValidationError):[/ Mappings / Regions]模板
格式问题包括重复的键,不正确的缩进等。仅通过检查文件是否为有效的YAML不能检测到这些问题。它可以是有效的YAML,但对于Cloudformation仍然无效。如上所示,您需要使用
validate-template
命令。
修复了提供的模板(包括新版本),我无法重现有关
未解决的条件依赖性BackupSize在Fn :: If
您在Fn中拥有的内容::如果我觉得还可以的话。
有关如何在
UserData
:
中内插Fn :: Join
的方法
我会考虑进行重构,以使复杂的逻辑位于Cloudformation之外。例如,您可以将主机名作为一个单独的参数传递。
如果您确实想这样做,可以这样做:
UserData:
Fn :: Base64 :!Sub
-|
#!/ bin / bash
NEWHOSTNAME = $ {newhostname}
主机名$ NEW_HOSTNAME
myshortname =`hostname -s`
-newhostname:!加入[ ,[ foo, bar]]
I am writing CF code to launch ec2 instance, this is what my code looks like:
I am facing these 2 issues:
1) I get this error "Template validation error: Template error: unresolved condition dependency BackupSize in Fn::If"
2) I want to join Parameter Name and from Mappings USERDATA. (The remaining userdata works fine, but this join is not working and just puts the same code in the userdata.
Can anyone help me out please?
AWSTemplateFormatVersion: "2010-09-09" Description: "This template should be used to deploy ONLY test servers" Mappings: Regions: us-east-1: "AMI": "ami-x" "VPC": "vpc-x" "SUBNET": "subnet-x" "USERDATA": ".example.com" "SHARE": "server1:/share" "SecurityGroups": "sg-x" "SecurityGroups2": "sg-y" Parameters: ApplSize: Description: "Please enter application vol. size" Type: "String" BackupSize: Description: "Please enter backup vol. size" Type: "String" Resources: EC2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap [Regions, !Ref "AWS::Region", AMI] InstanceType: !Ref InstanceType SubnetId: !FindInMap [Regions, !Ref "AWS::Region", SUBNET] SecurityGroupIds: - !FindInMap [Regions, !Ref "AWS::Region", SecurityGroups] - !FindInMap [Regions, !Ref "AWS::Region", SecurityGroups2] BlockDeviceMappings: - DeviceName : "/dev/sda1" Ebs: VolumeSize: "20" VolumeType: gp2 - DeviceName : "/dev/sde" Ebs: VolumeSize: !Ref ApplSize VolumeType: gp2 - DeviceName : "/dev/sdc" Ebs: VolumeSize: "5" VolumeType: gp2 - Fn::If: - BackupSize - DeviceName : "/dev/sdg" Ebs: VolumeSize: !Ref BackupSize VolumeType: gp2 - !Ref "AWS::NoValue" UserData: Fn::Base64: !Sub | #!/bin/bash NEW_HOSTNAME=Fn::Join: [ " ", [ !Ref Name, Fn::FindInMap: [Regions, !Ref "AWS::Region", USERDATA] ] ] hostname $NEW_HOSTNAME myshortname=`hostname -s`
I expect the template to create Backup volume if I put any value in the parameter, and if I leave backupsize value blank, it should not create this disk.
解决方案The various versions of the template presented all have basic formatting problems. The latest version (attached in a comment below this answer):
▶ aws cloudformation validate-template --template-body file://cloudformation.yml An error occurred (ValidationError) when calling the ValidateTemplate operation: [/Mappings/Regions] 'null' values are not allowed in templates
The formatting problems include duplicate keys, incorrect indentation, etc. These problems can't be detected by simply checking if the file is valid YAML. It can be valid YAML and still be invalid for Cloudformation. You need to use the
validate-template
command as I showed above.After fixing up the various issues in the provided template (including the new version), I was unable to reproduce an error about
unresolved condition dependency BackupSize in Fn::If
What you have in Fn::If looks ok to me.
As for how to interpolate
Fn::Join
in theUserData
:
I would consider refactoring so that complex logic lies outside of Cloudformation. For instance, you could pass the hostname as a separate parameter.
If you really want to do it this way you can do it like this:
UserData: Fn::Base64: !Sub - | #!/bin/bash NEWHOSTNAME=${newhostname} hostname $NEW_HOSTNAME myshortname=`hostname -s` - newhostname: !Join ["", ["foo", "bar"]]
这篇关于模板验证错误:模板错误:Fn :: If中未解决的条件依赖性BackupSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!