AWS CloudFormation:如何输出机器的 PublicIP?
AWS CloudFormation: How to output a machine's PublicIP?
我写了一个创建 linux docker 主机的 CloudFormation 模板。
我想在“输出”部分下显示机器的 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(我可以选择哪个?),第二部分是上面显示的真实属性 link。
所以我的问题是如何在输出部分下显示服务器的 PublicIP?
假设您的模板中有一个名为 Server
:
的 EC2 实例资源
"Server" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
}
}
您输出引用其资源名称的 public IP 地址:
"Outputs" : {
"PublicIp" : {
"Value" : { "Fn::GetAtt" : [ "Server", "PublicIp" ]},
"Description" : "Server's PublicIp Address"
}
}
如 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"
另请参阅:
- AWS::EC2::Instance 属性和 docs 中的 return 值。
我写了一个创建 linux docker 主机的 CloudFormation 模板。
我想在“输出”部分下显示机器的 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(我可以选择哪个?),第二部分是上面显示的真实属性 link。
所以我的问题是如何在输出部分下显示服务器的 PublicIP?
假设您的模板中有一个名为 Server
:
"Server" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
}
}
您输出引用其资源名称的 public IP 地址:
"Outputs" : {
"PublicIp" : {
"Value" : { "Fn::GetAtt" : [ "Server", "PublicIp" ]},
"Description" : "Server's PublicIp Address"
}
}
如 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"
另请参阅:
- AWS::EC2::Instance 属性和 docs 中的 return 值。