【问题标题】:in JSON, Template format error: Unresolved resource dependencies ~~ in the Resources block of the template在 JSON 中,模板格式错误:未解决的资源依赖关系 ~~ 在模板的 Resources 块中
【发布时间】:2022-01-11 13:51:38
【问题描述】:

我尝试使用下面的模板创建 EC2 实例,

    {
  "Description" : "Create an EC2 instance running the Amazon Linux 64 bit AMI.",
  "Parameters" : {
    "KeyPair" : {
      "Description" : "The EC2 Key Pair to allow SSH access to the instance",
      "Type" : "String",
      "Default" : "formationKey"
    }
  },
  "Resources" : {
    "Ec2Instance" : {
      "Type" : "AWS::EC2::Instance",
      "Properties" : {
        "KeyName" : { "Ref" : "formationKeyPair" },
        "ImageId" : "ami-0eb14fe5735c13eb5",
        "SecurityGroups" : [ { "Ref" : "FormationSecurityGroup" } ],
        "InstanceType" : "t2.micro",
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": [ "",
              [ "#!/bin/bash\n",
              "/opt/aws/bin/cfn-init --region ", {  "Ref": "AWS::Region" },
              " -s ", { "Ref": "AWS::StackName" },
              " -r Ec2Instance\n" ]
            ]
          }
        }
      },
      "Metadata" : {
        "AWS::CloudFormation::Init" : {
          "config" : {
            "packages" : {
              "yum" : {
                "httpd" : []
              }
            },
            "services" : {
              "sysvinit" : {
                "httpd" : {
                  "enabled" : "true",
                  "ensureRunning" : "true"
                }
              }
            }
          }
        }
      }
    },
    "InstanceSecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "GroupDescription" : "Allow HTTP and SSH access",
        "SecurityGroupIngress" : [ {
          "IpProtocol" : "tcp",
          "FromPort" : "22",
          "ToPort" : "22",
          "CidrIp" : "0.0.0.0/0"
        }, {
          "IpProtocol" : "tcp",
          "FromPort" : "80",
          "ToPort" : "80",
          "CidrIp" : "0.0.0.0/0"
        } ]
      }
    }
  },
  "Outputs" : {
    "InstanceId" : {
      "Description" : "The InstanceId of the newly created EC2 instance",
      "Value" : {
        "Ref" : "FormationEC2"
      }
    }
  },
  "AWSTemplateFormatVersion" : "2010-09-09"
}

但我得到了这个错误

" 模板格式错误:未解决的资源依赖关系 [formationKeyPair, FormationSecurityGroup] 的 Resources 块中 模板“

好像是没有定义参数导致的问题。 但是,不是我一开始就定义了参数吗??

我看到了一篇与我的问题类似的问题文章,但它是用 YAML 编写的 如何在 JSON 中进行故障排除?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    您的json 存在一些错误。 安全组SSH的引用是错误的。我已经对其进行了编辑,它看​​起来可以正常工作。

    {
      "Description": "Create an EC2 instance running the Amazon Linux 64 bit AMI.",
      "Parameters": {
        "KeyPair": {
          "Description": "The EC2 Key Pair to allow SSH access to the instance",
          "Type": "String",
          "Default": "formationKey"
        }
      },
      "Resources": {
        "Ec2Instance": {
          "Type": "AWS::EC2::Instance",
          "Properties": {
            "KeyName": {
              "Ref": "KeyPair"
            },
            "ImageId": "ami-0eb14fe5735c13eb5",
            "SecurityGroups": [
              {
                "Ref": "InstanceSecurityGroup"
              }
            ],
            "InstanceType": "t2.micro",
            "UserData": {
              "Fn::Base64": {
                "Fn::Join": [
                  "",
                  [
                    "#!/bin/bash\n",
                    "/opt/aws/bin/cfn-init --region ",
                    {
                      "Ref": "AWS::Region"
                    },
                    " -s ",
                    {
                      "Ref": "AWS::StackName"
                    },
                    " -r Ec2Instance\n"
                  ]
                ]
              }
            }
          },
          "Metadata": {
            "AWS::CloudFormation::Init": {
              "config": {
                "packages": {
                  "yum": {
                    "httpd": []
                  }
                },
                "services": {
                  "sysvinit": {
                    "httpd": {
                      "enabled": "true",
                      "ensureRunning": "true"
                    }
                  }
                }
              }
            }
          }
        },
        "InstanceSecurityGroup": {
          "Type": "AWS::EC2::SecurityGroup",
          "Properties": {
            "GroupDescription": "Allow HTTP and SSH access",
            "SecurityGroupIngress": [
              {
                "IpProtocol": "tcp",
                "FromPort": "22",
                "ToPort": "22",
                "CidrIp": "0.0.0.0/0"
              },
              {
                "IpProtocol": "tcp",
                "FromPort": "80",
                "ToPort": "80",
                "CidrIp": "0.0.0.0/0"
              }
            ]
          }
        }
      },
      "Outputs": {
        "InstanceId": {
          "Description": "The InstanceId of the newly created EC2 instance",
          "Value": {
            "Ref": "Ec2Instance"
          }
        }
      }
    }
    

    【讨论】:

    • 谢谢你,我发现了问题所在!
    猜你喜欢
    • 2020-03-30
    • 2018-11-14
    • 1970-01-01
    • 2020-03-25
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    相关资源
    最近更新 更多