1
votes

Problem Description

Stack: AWS

Services: Cloud formation

What am I trying to achieve: Trying to form a VPC using Cloud formation

More Details :

1.Trying my hands with Cloud formation.

2.Having a step by step approach in building a VPC using cloud formation[JSON].

  1. Built a vpc alone
  2. Updating the existing stack by adding more components to the JSON template
  3. Components like Subnets, Internet gateway, NAT, Route table ...etc
  4. I want to append one component at a time.So every time when I am doing an update I am adding only one component and I am being mindful of the sequence too.

Problem Faced : With the first template, VPC alone got created successfully.When I tried to update the stack with Internet gateway and attach into the VPC, started getting the error " Template validation error: Invalid template resource property 'VPCID'.

JSON Template is as follows

{
"Parameters": {  
   "CIDRRange": { 
     "Description": "VPCCIDR Range (will be a /16 block)",
     "Type": "String",
     "Default": "10.251.0.0",
     "AllowedValues": ["10.250.0.0","10.251.0.0"]
                } 
              }, 

"Resources": { 
   "VPCBase": { 
     "Type": "AWS::EC2::VPC",
     "Properties": { 
     "CidrBlock": { "Fn::Join" : ["", [{ "Ref" : "CIDRRange" }, "/16"]] },
     "EnableDnsSupport": "True",
     "EnableDnsHostnames": "True",
     "Tags": [{ "Key": "Name", "Value":    { "Fn::Join" : ["", [{ "Ref" : "AWS::StackName" }, "-VPC"]] } }]
                   }
               },

   "IGWBase" : {
     "Type" : "AWS::EC2::InternetGateway",
     "Properties" : {
     "Tags" : [{ "Key": "Name", "Value": { "Fn::Join" : ["", [{ "Ref" : "AWS::StackName" }, "-IGW"]] } }]
                    }
               },

   "VGAIGWBase" : {
     "Type" : "AWS::EC2::VPCGatewayAttachment",
     "Properties" : {
     "InternetGatewayId" : { "Ref" : "IGWBase" },
     "VpcId" : { "Ref" : "VPCBase" }}
                  },

   "Outputs": {
     "VPCID" : { "Value" : { "Ref" : "VPCBase" } },
     "DefaultSG" : { "Value" : { "Fn::GetAtt" : ["VPCBase", "DefaultSecurityGroup"] }}
              }
}
}
1

1 Answers

5
votes

Your formatting is a bit of a mess - I'd recommend going with yaml over json - but the problem is you're not closing the Resources: section.

You can validate a template with the cli with

aws cloudformation validate-template --template-body file://path.json



  "VGAIGWBase" : {
    "Type" : "AWS::EC2::VPCGatewayAttachment",
    "Properties" : {
      "InternetGatewayId" : { "Ref" : "IGWBase" },
      "VpcId" : { "Ref" : "VPCBase" }
    }
  }
},   << ADD THIS

"Outputs": {
  "VPCID" : { "Value" : { "Ref" : "VPCBase" } },
  "DefaultSG" : { "Value" : { "Fn::GetAtt" : ["VPCBase", "DefaultSecurityGroup"] }}
  }
}