AWS CloudFormation: How to output a machine's PublicIP?

落爺英雄遲暮 提交于 2020-02-01 04:15:26

问题


I wrote a CloudFormation template which creates a linux docker host.

I want to display the PublicIP of the machine under the "Outputs" section.

This is the relevant portion of the template:

"Outputs" : {
    "ServerAddress" : {
      "Value" : { "Fn::GetAtt" : [ "Server", "PublicDnsName" ] },
      "Description" : "Server Domain Name"
    },
    "SecurityGroup" : {
      "Value" : { "Fn::GetAtt" : [ "ServerSecurityGroup", "GroupId" ] },
      "Description" : "Server Security Group Id"
    },
    "PublicIp" : {
      "Value" : { "Fn::GetAtt" : [ "ServerPublicIp", "PublicIp" ]},
      "Description" : "Server's PublicIp Address"
    },
  }

I've read in the official AWS documentation about using "Fn::GetAtt" and tried to implement it in my template, but when I try to create the stack I get the following error:

Error
Template validation error: Template error: instance of Fn::GetAtt references undefined resource ServerPublicIp

As far as I understand, the first part in the GetAtt line is a LogicalName (which I can choose?) and the second one is the real attribute as appears on the above link.

So my question is how to display the PublicIP of the server under the Outputs section?


回答1:


Assuming your have an EC2 instance resource in your template named Server:

"Server" : {
    "Type" : "AWS::EC2::Instance",
    "Properties" : {
    }
}

You output the public IP address referencing it's resource name:

"Outputs" : {
    "PublicIp" : {
      "Value" : { "Fn::GetAtt" : [ "Server", "PublicIp" ]},
      "Description" : "Server's PublicIp Address"
    }
}



回答2:


As mentioned in the docs, the outputs can optionally be exported for cross-stack references. In case that is your use-case:

JSON:

"Outputs" : {
  "PublicIp" : {
    "Value" : { "Fn::GetAtt" : ["Server", "PublicIp"]},
    "Description" : "Server Public IP"
    "Export" : {
      "Name" : {"Fn::Sub": "${AWS::StackName}-PublicIP"}
    }
  }
}

YAML:

Outputs:
  PublicIp:
    Description: Server Public IP
    Value: !GetAtt Server.PublicIp
    Export:
      Name: !Sub "${AWS::StackName}-PublicIp"

See also:

  • AWS::EC2::Instance properties and return values in the docs.


来源:https://stackoverflow.com/questions/42205622/aws-cloudformation-how-to-output-a-machines-publicip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!