我正在编写CF代码以启动ec2实例,这就是我的代码:
我面临以下两个问题:
1)我收到此错误“模板验证错误:模板错误:Fn :: If中未解决的条件依赖性BackupSize”
2)我想从映射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`
如果我在参数中输入任何值,并且如果我将backupsize值留为空白,则我希望模板创建备份卷。
答案 0 :(得分:0)
所呈现的模板的各种版本都有基本的格式设置问题。最新版本(在此答案下方的评论中附加):
▶ 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
格式问题包括重复的键,不正确的缩进等。仅通过检查文件是否为有效的YAML不能检测到这些问题。它可以是有效的YAML,但对于Cloudformation仍然无效。您需要使用上面显示的validate-template
命令。
解决了所提供模板(包括新版本)中的各种问题之后,我无法重现有关的错误
Fn :: If中未解决的条件依赖性BackupSize
您在Fn ::中拥有的东西对我来说还可以。
关于如何在Fn::Join
中插值UserData
:
我会考虑进行重构,以便将复杂的逻辑置于Cloudformation之外。例如,您可以将主机名作为单独的参数传递。
如果您真的想用这种方式做,可以这样:
UserData:
Fn::Base64: !Sub
- |
#!/bin/bash
NEWHOSTNAME=${newhostname}
hostname $NEW_HOSTNAME
myshortname=`hostname -s`
- newhostname: !Join ["", ["foo", "bar"]]
答案 1 :(得分:0)
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"
VaultSize:
Description: "Please enter secret vol. size"
Type: "String"
InstanceType:
Description: "Please select the instance type"
Type: "String"
Name:
Description: "Please mention server name"
Type: "String"
CustomerName:
Description: "Please mention customer name"
Type: "String"
Url:
Description: "Please mention url without the domain name"
Type: "String"
Conditions:
BackupVol: !Equals [!Ref BackupSize, ""]
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:
- BackupVol
- !Ref "AWS::NoValue"
- DeviceName : "/dev/sdg"
Ebs:
VolumeSize: !Ref BackupSize
VolumeType: gp2
UserData:
Fn::Base64: !Sub
- |+
#!/bin/bash -xe
NEW_HOSTNAME=${test}
- test:
Fn::FindInMap: [Regions, !Ref "AWS::Region", Name]
答案 2 :(得分:0)
简单 ans..if 的第一个条件应该是一个 CONDITIONS..创建一个条件块并在 IF 中使用它...示例:
条件: 我的条件:等等
IF [我的条件:blah1, blah2]