Rajnish Rana
Rajnish Rana

Reputation: 13

CloudFormation - Template contains errors.: Invalid template parameter property 'Properties'

I am uploading following template to create an EC2 instance in CloudFormation. And when I "Validate Template" from console getting following error- Template contains errors.: Invalid template parameter property 'Properties'

Template Code: Template is attached. Open template with notepad or notepad++

{

"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "This is an AWS Cloud Formation template to create an EC2 instance in a Custom VPC.",

"Parameters" : {

"KeyName" : {
"Type" : "String",
"Default" : "ec2-us-east",
"Description" : "SSH Key to access the EC2 instance"
},

"MyVpc" : {
"Type" : "AWS::EC2::VPC",
"Properties" : {
"CidrBlock" : "10.0.0.0/16",
"EnableDnsHostnames" : "true"
}
},

"PublicSubnet" : {
"Type" : "AWS::EC2::Subnet",
"Properties" : {
"VpcId" : {"Ref" : "MyVpc"},
"CidrBlock" : "10.0.0.0/24",
"AvailabilityZone" : "us-east-1a"
}
},

"InstanceType" : {
"Type" : "String",
"Default" : "t2.micro",
"Description" : "Select EC2 instance type"
}

},


"Resources" : {

"SecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupName" : "My Security Group",
"GroupDescription" : "My CFSecurity Group",
"VpcId" : {"Ref" : "MyVpc"},
"SecurityGroupIngress" : [{
"CidrIp" : "0.0.0.0/0",
"FromPort" : "22",
"IpProtocol" : "tcp",
"ToPort" : "22"
}]
}
},

"Server" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"ImageId" : "ami-1853ac65",
"InstanceType" : {"Ref" : "InstanceType"},
"KeyName" : {"Ref" : "KeyName"},
"SecurityGroupIds" : {"Ref" : "SecurityGroup"},
"SubnetId" : {"Ref" : "PublicSubnet"}
}
}

},

"Outputs" : {
"PublicName" : {
"Value" : {"Fn::GetAtt" : ["Server", "PublicDnsName"]},
"Description" : "Public Name (connect via ssh)"
}
}

}

Can you please help me to find out What I am doing wrong?

Upvotes: 1

Views: 2296

Answers (1)

Zohaib
Zohaib

Reputation: 7116

You are creating VPC and public subnet under key Parameters. You need to define vpc and subnet under key resources. This should work:

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "This is an AWS Cloud Formation template to create an EC2 instance in a Custom VPC.",
    "Parameters": {
        "KeyName": {
            "Type": "String",
            "Default": "ec2-us-east",
            "Description": "SSH Key to access the EC2 instance"
        },
        "InstanceType": {
            "Type": "String",
            "Default": "t2.micro",
            "Description": "Select EC2 instance type"
        }
    },

"Resources": {
    "SecurityGroup": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties": {
            "GroupName": "My Security Group",
            "GroupDescription": "My CFSecurity Group",
            "VpcId": {
                "Ref": "MyVpc"
            },
            "SecurityGroupIngress": [{
                "CidrIp": "0.0.0.0/0",
                "FromPort": "22",
                "IpProtocol": "tcp",
                "ToPort": "22"
            }]
        }
    },
    "Server": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "ImageId": "ami-1853ac65",
            "InstanceType": {
                "Ref": "InstanceType"
            },
            "KeyName": {
                "Ref": "KeyName"
            },
            "SecurityGroupIds": {
                "Ref": "SecurityGroup"
            },
            "SubnetId": {
                "Ref": "PublicSubnet"
            }
        }
    },
    "MyVpc": {
        "Type": "AWS::EC2::VPC",
        "Properties": {
            "CidrBlock": "10.0.0.0/16",
            "EnableDnsHostnames": "true"
        }
    },
    "PublicSubnet": {
        "Type": "AWS::EC2::Subnet",
        "Properties": {
            "VpcId": {
                "Ref": "MyVpc"
            },
            "CidrBlock": "10.0.0.0/24",
            "AvailabilityZone": "us-east-1a"
        }
    }
},
"Outputs": {
    "PublicName": {
        "Value": {
            "Fn::GetAtt": ["Server",
            "PublicDnsName"]
        },
        "Description": "Public Name (connect via ssh)"
    }
}
}

Upvotes: 1

Related Questions