Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 789
Itai Ganot Avatar asked Feb 13 '17 13:02

Itai Ganot


People also ask

How do I export a value from CloudFormation template?

To export a stack's output value, use the Export field in the Output section of the stack's template. To import those values, use the Fn::ImportValue function in the template for the other stacks. For a walkthrough and sample templates, see Walkthrough: Refer to resource outputs in another AWS CloudFormation stack.

What are outputs in AWS CloudFormation?

The optional Outputs section declares output values that you can import into other stacks (to create cross-stack references), return in response (to describe stack calls), or view on the AWS CloudFormation console. For example, you can output the S3 bucket name for a stack to make the bucket easier to find.

How do you refer to existing resources in CloudFormation?

To import existing resources into a CloudFormation stack, you need to provide: A template that describes the entire stack, including both the resources to import and (for existing stacks) the resources that are already part of the stack. Each resource to import must have a DeletionPolicy attribute in the template.


2 Answers

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"
    }
}
like image 135
Matt Houser Avatar answered Oct 19 '22 23:10

Matt Houser


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.
like image 37
Alex Harvey Avatar answered Oct 20 '22 00:10

Alex Harvey