2016-07-06 154 views
2

我想創建一個ec2實例,並且我希望能夠創建一個包含ec2實例公有DNS名稱的文件,雖然在下面的代碼中我得到造成線循環依賴錯誤:AWS Cloud Formation - 如何獲取公共ip地址的ec2實例

"server_name = \"",{ "Fn::GetAtt" : [ "ECServer", "PublicDnsName" ]},"\"\n","\n" 

是否有可能得到,當我試圖創建EC2實例中的部分公共DNS名稱?

"ECServer": { 
    "Type": "AWS::EC2::Instance", 
    "Metadata" : { 
    "AWS::CloudFormation::Init" : { 
     "configSets": { 
     "Install": ["ECServerConfig"] 
     }, 
     "ECConfig": { 
     "files": { 
      "/tmp/test.txt" : { 
      "content": { "Fn::Join" : ["", [ 
       "server_name = \"",{ "Fn::GetAtt" : [ "ECServer", "PublicDnsName" ]},"\"\n","\n" 
      ]]}, 
      "mode" : "000644", 
      "owner": "root", 
      "group": "root" 
      } 
     } 
     } 
    } 
    }, 

回答

0
 You can try using this, in CFT one cannot use the refer to the properties of itself using Fn::GetAtt function 
"ECServer": { 
      "Type": "AWS::EC2::Instance", 
      "Metadata" : { 
      "AWS::CloudFormation::Init" : { 
       "configSets": { 
       "Install": ["ECServerConfig"] 
       }, 
       "ECConfig": { 
       "files": { 
        "/tmp/test.txt" : { 
        "mode" : "000644", 
        "owner": "root", 
        "group": "root" 
        } 
       }, 
       "commands" :{ 
       "test" : { 
        "command" : "curl -s http://169.254.169.254/latest/meta-data/public-hostname > /tmp/test.txt", 
     "ignoreErrors" : "false" 
     } 
       } 
      } 
      } 
     } 
     } 
0

您可以通過使用簡單的curl命令原樣
1. SSH到EC2實例獲得運行中的EC2實例的公網IP。
2.執行以下命令 -
curl http://169.254.169.254/latest/meta-data/public-ipv4