Question

I use following function can get PublicDnsName:

        {
         "Fn::GetAtt": [
           "EC2Instance",
           "PublicDnsName"
         ]
       }

However, after I assign static ip to the instance, this above function SOMETIMES return the newly PublicDnsName and sometimes it returns old PublicDnsName (the dns name that before we assign static IP).

Have you seen this issue before? I'm scatching my head to figure out why. Please let me know what seem to be the issue or how I should resolve it. Thanks.

Following is my partial CloudFormation template:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Graphite and StatsD",
  "Parameters": {
    "InstanceType": {
      "Type": "String",
      "Default": "m1.xlarge"
    }
  },
  "Mappings": {
    "RegionMap": {
      "us-east-1": {
        "AMI": "ami-5ffaa636",
        "RecordSetName": "ue1"
      },
      "us-west-1": {
        "AMI": "ami-4ad8ef0f",
        "RecordSetName": "uw1"
      },
      "us-west-2": {
        "AMI": "ami-66049d56",
        "RecordSetName": "uw2"
      },
      "eu-west-1": {
        "AMI": "ami-861bfbf1",
        "RecordSetName": "ew1"
      },
      "ap-southeast-1": {
        "AMI": "ami-42134610",
        "RecordSetName": "as1"
      },
      "ap-southeast-2": {
        "AMI": "ami-c98b17f3",
        "RecordSetName": "as2"
      },
      "ap-northeast-1": {
        "AMI": "ami-0733a806",
        "RecordSetName": "an1"
      },
      "sa-east-1": {
        "AMI": "ami-2bac0a36",
        "RecordSetName": "se1"
      }
    }
  },
  "Resources": {
    "ElasticIP": {
      "Type": "AWS::EC2::EIP"
    },
    "EIPAssociation": {
      "Type": "AWS::EC2::EIPAssociation",
      "Properties": {
        "InstanceId": {
          "Ref": "EC2Instance"
        },
        "EIP": {
          "Ref": "ElasticIP"
        }
      }
    },
    "DNSRecords": {
      "Type": "AWS::Route53::RecordSetGroup",
      "Properties": {
        "HostedZoneName": "mydomain.com.",
        "RecordSets": [
          {
            "Name": {
              "Fn::Join": [
                "",
                [
                  "graphite-",
                  {
                    "Fn::FindInMap": [
                      "RegionMap",
                      {
                        "Ref": "AWS::Region"
                      },
                      "RecordSetName"
                    ]
                  },
                  ".mydomain.com"
                ]
              ]
            },
            "Type": "CNAME",
            "TTL": "300",
            "ResourceRecords": [
              {
                "Fn::GetAtt": [
                  "EC2Instance",
                  "PublicDnsName"
                ]
              }
            ]
          }
        ]
      }
    },
    "EC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "KeyName": "MyKeyName",
        "SecurityGroups": [
          "MySecurityGroup"
        ],
        "ImageId": {
          "Fn::FindInMap": [
            "RegionMap",
            {
              "Ref": "AWS::Region"
            },
            "AMI"
          ]
        },
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "Tags": [
          {
            "Key": "Name",
            "Value": "graphite-statsd"
          }
        ],
        "UserData": {
          "Fn::Base64": {
            "Fn::Join": [
              "",
              [
                ....
                VERY LONG SHELL INSTALL SCRIPT
                ....
              ]
            ]
          }
        }
      }
    }
  }
}
Was it helpful?

Solution

I figured out that I have to use http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html to make sure which resources are supposed to create first.

In this case, I want to make sure my ElasticIP resource, EC2Instance resource, and EIPAssociation resource are created first before DNSRecords resource is called.

  {
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Graphite and StatsD",
    "Parameters": {
      "InstanceType": {
        "Type": "String",
        "Default": "m1.xlarge"
      }
    },
    "Mappings": {
      "RegionMap": {
        "us-east-1": {
          "AMI": "ami-5ffaa636",
          "ShortName": "ue1"
        },
        "us-west-1": {
          "AMI": "ami-4ad8ef0f",
          "ShortName": "uw1"
        },
        "us-west-2": {
          "AMI": "ami-66049d56",
          "ShortName": "uw2"
        },
        "eu-west-1": {
          "AMI": "ami-861bfbf1",
          "ShortName": "ew1"
        },
        "ap-southeast-1": {
          "AMI": "ami-42134610",
          "ShortName": "as1"
        },
        "ap-southeast-2": {
          "AMI": "ami-c98b17f3",
          "ShortName": "as2"
        },
        "ap-northeast-1": {
          "AMI": "ami-0733a806",
          "ShortName": "an1"
        },
        "sa-east-1": {
          "AMI": "ami-2bac0a36",
          "ShortName": "se1"
        }
      }
    },
    "Resources": {
      "ElasticIP": {
        "Type": "AWS::EC2::EIP"
      },
      "EC2Instance": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
          "KeyName": "MyKeyName",
          "SecurityGroups": [
            "MySecurityGroup",
          ],
          "ImageId": {
            "Fn::FindInMap": [
              "RegionMap",
              {
                "Ref": "AWS::Region"
              },
              "AMI"
            ]
          },
          "InstanceType": {
            "Ref": "InstanceType"
          },
          "Tags": [
            {
              "Key": "Name",
              "Value": "graphite-statsd"
            }
          ],
          "UserData": {
            "Fn::Base64": {
              "Fn::Join": [
                "",
                [
                  ....
                  VERY LONG SHELL INSTALL SCRIPT
                  ....
                ]
              ]
            }
          }
        },
        "DependsOn": [
          "ElasticIP"
        ]
      },
      "EIPAssociation": {
        "Type": "AWS::EC2::EIPAssociation",
        "Properties": {
          "InstanceId": {
            "Ref": "EC2Instance"
          },
          "EIP": {
            "Ref": "ElasticIP"
          }
        },
        "DependsOn": [
          "ElasticIP",
          "EC2Instance"
        ]
      },
      "DNSRecords": {
        "Type": "AWS::Route53::RecordSetGroup",
        "Properties": {
          "HostedZoneName": "mydomain.com.",
          "RecordSets": [
            {
              "Name": {
                "Fn::Join": [
                  "",
                  [
                    "graphite-",
                    {
                      "Fn::FindInMap": [
                        "RegionMap",
                        {
                          "Ref": "AWS::Region"
                        },
                        "ShortName"
                      ]
                    },
                    ".mydomain.com"
                  ]
                ]
              },
              "Type": "CNAME",
              "TTL": "300",
              "ResourceRecords": [
                {
                  "Fn::GetAtt": [
                    "EC2Instance",
                    "PublicDnsName"
                  ]
                }
              ]
            }
          ]
        },
        "DependsOn": [
          "EIPAssociation"
        ]
      }
    }
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top