How aws fetch public ip in the private vpc

0

Hi My vpc has no internet gateway and their is two private subnet I have created 2 ec2 instances with public ip I want to know if whole vpc is private then how it is assigns public ip to the resources.

profile picture
已提问 10 个月前355 查看次数
3 回答
2
已接受的回答

Even if your VPC has no Internet Gateway (IGW), AWS can still assign public IPs to the instances based on the subnet settings. However, without an IGW, these instances cannot communicate with the internet using the public IP. The public IP will be assigned but will be non-functional for internet traffic because there's no route to the internet.

Why Public IPs Are Assigned: Subnet Settings: If the subnets are configured to auto-assign public IPs, then any instance launched within those subnets will be assigned a public IP by default. Instance Launch Settings: During the launch of an EC2 instance, there is an option to enable or disable the assignment of a public IP address. This can override the subnet setting.

If you are not going to go to the internet from EC2 instances, it's better to create those instances without public IP addresses because Public IP addresses are not free (~3$ per IP per month)

profile picture
专家
已回答 10 个月前
profile picture
专家
已审核 10 个月前
专家
已审核 10 个月前
0

Hello,

The assignment of public IP addresses to EC2 instances in AWS is controlled at the subnet level, not at the VPC level. If you've enabled auto-assign public IPv4 address at the subnet level or allow instances to have public IP addresses if configured during instance launch, the EC2 instances will be assigned with a Public IPv4 address.

profile picture
专家
已回答 10 个月前
0

Note that since you don't need the public IPs, and because AWS charges a fee for each public IPv4 address, and since there is the risk that an IGW and routes would later be added and the instances with the public IPs might be exposed to the public internet by accident, it's best to remove the public IPs.

Removing an automatically assigned public IP wasn't previously possible without terminating and recreating the instance (or detaching and recreating a secondary network interface), but it recently became possible to do simply by modifying the network interface (ENI, elastic network interface) options. The procedure is explained here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-eni.html#managing-network-interface-ip-addresses

Technically, the way all public IPv4 addresses are implemented for EC2 instances is that when traffic arrives to the public IPv4 address, AWS translates the destination IP of the packet to the primary private IPv4 address of the network interface. Return traffic is treated the same way in reverse. That's why also in your current situation, your VPC flow logs would likely show port scans and probes trying to reach your instances with public IPs, despite there being no IGW and even if security groups or network ACLs are dropping the incoming packets. That's because every packet that is received at the public IP first gets its destination IP translated to the private IP of the corresponding ENI and only then evaluated against security group rules, network ACLs, and so on.

The solution is to ensure your subnets are not configured to assign public IPs, as Oleksii Bebych advised earlier. For your existing instances not having public IPv4 addresses but not needing them, I advise you follow the instructions in the documentation article above to remove the public IPs. It will also save you a few dollars per month per public IPv4 address.

专家
已回答 10 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则