Saturday, August 2, 2014

CloudFormation not doing what you expect?

I ran into a problem recently with a CloudFormation stack, where I could not access my EC2 instances started within the VPC that was created.

After much pulling out of hair, and swearing, it turns out that I had a duplicate entry in my JSON. When I was attaching my subnets to the route table, I had both subnets listed with the same JSON name:

        "SubnetRouteTableAssociation" : 
        {
            "Type" : "AWS::EC2::SubnetRouteTableAssociation",
            "Properties" : 
            {
                "SubnetId" : { "Ref" : "Subnet1" },
                "RouteTableId" : { "Ref" : "RouteTable" }
            }
        },

        "SubnetRouteTableAssociation" : 
        {
            "Type" : "AWS::EC2::SubnetRouteTableAssociation",
            "Properties" : 
            {
                "SubnetId" : { "Ref" : "Subnet2" },
                "RouteTableId" : { "Ref" : "RouteTable" }
            }
        },

It took me a long time to find this. It wasn’t until I looked at my route tables that I found the route table had a single entry, and not two. Then I looked at my template, and noticed the duplicate name. I’m using boto to start my stacks, and I received no error or warning. I’m not sure if the main UI would have notified me either. 

I changed the above to:

        "SubnetRouteTableAssociation1" : 
        {
            "Type" : "AWS::EC2::SubnetRouteTableAssociation",
            "Properties" : 
            {
                "SubnetId" : { "Ref" : "Subnet1" },
                "RouteTableId" : { "Ref" : "RouteTable" }
            }
        },

        "SubnetRouteTableAssociation2" : 
        {
            "Type" : "AWS::EC2::SubnetRouteTableAssociation",
            "Properties" : 
            {
                "SubnetId" : { "Ref" : "Subnet2" },
                "RouteTableId" : { "Ref" : "RouteTable" }
            }
        },

And all is well now. 

Lesson learned: Double check the naming on your templates if you start running into odd problems.

No comments:

Post a Comment