Amazon cloudformation 模板验证错误:模板错误:Fn::If中未解析的条件依赖项备份

Amazon cloudformation 模板验证错误:模板错误:Fn::If中未解析的条件依赖项备份,amazon-cloudformation,Amazon Cloudformation,我正在编写CF代码来启动ec2实例,这是我的代码的样子: 我面临以下两个问题: 1) 我收到以下错误“模板验证错误:模板错误:Fn::If中未解析的条件依赖项备份” 2) 我想从USERDATA连接参数名和映射。(剩余的userdata工作正常,但此联接不起作用,只是将相同的代码放入userdata中 有人能帮我吗 AWSTemplateFormatVersion: "2010-09-09" Description: "This template should be used to depl

我正在编写CF代码来启动ec2实例,这是我的代码的样子:

我面临以下两个问题:

1) 我收到以下错误“模板验证错误:模板错误:Fn::If中未解析的条件依赖项备份”

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值保留为空,则它不应创建此磁盘。

提供的模板的各种版本都有基本的格式问题。最新版本(附于此答案下面的注释中):

格式化问题包括重复的键、不正确的缩进等。这些问题不能通过简单地检查文件是否是有效的YAML来检测。它可能是有效的YAML,但对于Cloudformation仍然无效。您需要使用
验证模板
命令,如上所示

在修复了所提供模板(包括新版本)中的各种问题后,我无法重现有关的错误

Fn::If中未解析的条件依赖项备份

在我看来,你的Fn::If中的内容还可以

关于如何在
UserData
中插入
Fn::Join

>P>我会考虑重构,使得复杂的逻辑位于云的形成之外。例如,可以将主机名作为单独的参数传递。

  • 如果你真的想这样做,你可以这样做:

  • UserData:
    Fn::Base64:!Sub
    - |
    #!/bin/bash
    NEWHOSTNAME=${NEWHOSTNAME}
    主机名$NEW\u主机名
    myshortname=`hostname-s`
    -新主机名:!加入[“”,[“foo”,“bar”]]
    
    您必须提供已更正格式的实际模板(必须是有效的YAML)因为我无法复制您看到的内容。这里:好的,我同意您在该文件中的模板与上面的模板相同。而且,正如我所说,它是无效的模板。缩进到处都是错误的。您需要给我们一个真实的模板,它会产生
    模板验证错误:模板错误:未解决的条件依赖项备份Fn::如果
    。在修复缩进问题后,模板将生成,
    未解析的资源依赖项[InstanceType]在模板的参考资料块中
    。我有大约100行bash代码,我需要在所有代码行周围加上引号吗?但是依赖性问题呢?我已经上传了具有实例类型的代码,下面是它的链接。我将使用第1点作为解决方案,忘记之前的回复,我已经再次发布了代码w有了所有的细节,希望帮助B罗,我真的很感谢你的帮助。我设法用If条件解决了这个问题,现在我正在尝试处理UserData部分。将上传代码。我也找到了UserData部分,我编辑了正确的代码。现在唯一的挑战是如何添加url之外的另一个变量,我想这就是问题所在方法开发人员会知道这也是一个无效的模板。如果你在链接中与我联系,我将帮助你弄清为什么你粘贴在这里的模板是无效的。
    ▶ 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
    
    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]