in JSON, Template format error: Unresolved resource dependencies ~~ in the Resources block of the template

i tried to create an EC2 instance with the template below,

    {
  "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"
}

but i get this error

" Template format error: Unresolved resource dependencies [formationKeyPair, FormationSecurityGroup] in the Resources block of the template "

it seems to be a problem caused by not defining a parameter. but, isn't it that i defined the parameter in the first place??

i saw a question article similar to my problem, but it was written in YAML how can i troubleshoot in JSON?


There are some errors with your json. The reference of the security group and SSH is wrong. I've edited it and it looks like it is working.

{
  "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"
      }
    }
  }
}