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

前端 未结 2 933
夕颜
夕颜 2021-02-15 15:17

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 t

相关标签:
2条回答
  • 2021-02-15 15:22

    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"
        }
    }
    
    0 讨论(0)
  • 2021-02-15 15:33

    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.
    0 讨论(0)
提交回复
热议问题