【发布时间】:2021-07-12 04:45:19
【问题描述】:
我正在编写 CF 代码来启动 ec2 实例,这就是我的代码的样子:
我面临这两个问题:
1) 我收到此错误“模板验证错误:模板错误:Fn::If 中未解决的条件依赖 BackupSize”
2) 我想加入 Parameter Name 和 Mappings USERDATA。 (剩余的 userdata 工作正常,但这个连接不起作用,只是将相同的代码放在 userdata 中。
谁能帮帮我?
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`
如果我在参数中输入任何值,我希望模板创建备份卷,如果我将备份大小值留空,它不应该创建这个磁盘。
【问题讨论】:
-
您必须提供格式正确的实际模板(必须是有效的 YAML),因为我无法重现您所看到的内容。
-
好的,我同意您在该文件中的模板与上述模板相同。而且,正如我所说,这是一个无效的模板。缩进到处都是错误的。你需要给我们真正的产生
Template validation error: Template error: unresolved condition dependency BackupSize in Fn::If。 -
修复缩进问题后,模板生成
Unresolved resource dependencies [InstanceType] in the Resources block of the template。