2016-07-06 151 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