【问题标题】:AWS CloudFormation: How to output a machine's PublicIP?AWS CloudFormation:如何输出机器的 PublicIP?
【发布时间】:2017-07-01 12:44:40
【问题描述】:

我写了一个 CloudFormation 模板来创建一个 linux docker 主机。

我想在“输出”部分下显示机器的 PublicIP。

这是模板的相关部分:

"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"
    },
  }

我在official AWS documentation 中阅读了有关使用“Fn::GetAtt”的信息并尝试在我的模板中实现它,但是当我尝试创建堆栈时出现以下错误:

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

据我了解,GetAtt 行的第一部分是一个 LogicalName(我可以选择吗?),第二部分是上面链接中显示的真实属性。

所以我的问题是如何在 Outputs 部分下显示服务器的 PublicIP?

【问题讨论】:

  • ServerPublicIp 应该是您的 EC2 实例资源的名称,例如 Server 是您的 ServerAddress 输出。

标签: amazon-web-services amazon-cloudformation


【解决方案1】:

docs 中所述,可以选择导出输出以用于跨堆栈引用。如果这是您的用例:

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"

另见:

  • docs 中的 AWS::EC2::Instance 属性和返回值。

【讨论】:

    【解决方案2】:

    假设您的模板中有一个名为 Server 的 EC2 实例资源:

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

    您输出引用其资源名称的公共 IP 地址:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-11
      • 2019-07-19
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2017-05-28
      • 2019-03-22
      相关资源
      最近更新 更多