Col*_*phy 4 amazon-ec2 amazon-web-services amazon-cloudformation
在我在aws ec2上使用的vpc中,默认情况下不会获得公共IP地址。我正在尝试在参考此文档和这一点文档后手动添加一个。
目前我的cloudformation模板包括
"netinterface" : {
"Type" : "AWS::EC2::NetworkInterface",
"Properties" : {
"SubnetId" : {"Ref": "Subnet"}
}
},
"billingattributionapi" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"NetworkInterfaces" : [
{
"AssociatePublicIpAddress" : "true",
"DeviceIndex" : "eth0",
"NetworkInterfaceId" : {"Ref" : "netinterface"},
"DeleteOnTermination" : "true"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
省略了很多,但这就是与添加 ip 相关的所有内容。
我的问题是,文档说只有 DeviceIndex 为 eth0 的网络接口才能拥有公共 IP 地址,但使用 eth0 会导致错误:
Encountered non numeric value for property DeviceIndex
Run Code Online (Sandbox Code Playgroud)
但是如果我将设备 ID 设置为 0 我会得到
The associatePublicIPAddress parameter cannot be specified for a network interface with an ID
Run Code Online (Sandbox Code Playgroud)
但是,如果我删除 NetworkInterfaceId 并根据我得到的文档的要求添加子网 ID
Network interfaces and an instance-level subnet ID may not be specified on the same request
Run Code Online (Sandbox Code Playgroud)
此时我不知道我应该做什么。根据文档,我原来的方法似乎是正确的。以前有人这样做过并且可以指出我做错了什么吗?
这对我有用:
在“AWS::EC2::Instance”资源中将私有 IP 设置为主 IP 地址:
"NetworkInterfaces" : [
{
"DeleteOnTermination" : true,
"Description" : "Main interface",
"DeviceIndex" : "0",
"PrivateIpAddresses" : [
{
"PrivateIpAddress" : {
"Ref" : "InternalIPAddress"
},
"Primary" : true
}
],
"GroupSet" : [
{
"Ref" : "SecurityGroupId"
}
],
"SubnetId" : {
"Ref" : "VPCSubnet"
}
}
],
Run Code Online (Sandbox Code Playgroud)
请注意,上面对“InternalIPAddress”的引用是一个参数,用于传递机器应具有的内部IP。我认为没有必要,因为没有它,实例将通过 dhcp 获取 IP。
然后稍后在模板中添加“AWS::EC2::EIP”类型的资源:
"EIPExternalIP" : {
"Type" : "AWS::EC2::EIP",
"Properties" : {
"InstanceId" : {
"Ref" : "Instance"
},
"Domain" : "vpc"
}
},
Run Code Online (Sandbox Code Playgroud)
您可以使用 {"Ref" : "EIPExternalIP"} 获取外部 IP
归档时间: |
|
查看次数: |
8184 次 |
最近记录: |