3

我使用以下函数可以获得 PublicDnsName:

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

但是,在我为实例分配静态 IP 后,上述函数有时会返回新的 PublicDnsName,有时会返回的 PublicDnsName(我们分配静态 IP 之前的 dns 名称)。

你以前见过这个问题吗?我正在努力寻找原因。请让我知道似乎是什么问题或我应该如何解决它。谢谢。

以下是我的部分 CloudFormation 模板:

{
  "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
                ....
              ]
            ]
          }
        }
      }
    }
  }
}
4

1 回答 1

5

我发现我必须使用http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html来确保应该首先创建哪些资源。

在这种情况下,我想确保在调用 DNSRecords 资源之前先创建我的 ElasticIP 资源、EC2Instance 资源和 EIPAssociation 资源。

  {
    "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"
        ]
      }
    }
  }
于 2013-11-06T07:17:59.870 回答