I have created a VPC and an Elastic Network Interface, but my ENI doesn't get assigned a public IP adress. I checked the subnet that the ENI is in, and it has its Auto-assign public IPv4 address
option is set to yes
. I even re-created the ENI and it still doesn't get assigned public IP.
I used AWS CDK to create these resources. Here are the code snippets that I used when creating these resources
const vpc = new Vpc(this, "common:ec2.Vpc", {
vpcName: "common",
natGateways: 0,
});
const msiEni = new CfnNetworkInterface(
this,
`msi-${COMMON_INSTANCE_NAME}:ec2.CfnNetworkInterface`,
{
subnetId: instance.instance.subnetId!,
groupSet: [sg.securityGroupId],
}
);
The auto-assign public IP address attribute on VPC subnets only applies to the primary ENI attached to each EC2 instance (interface eth0) on the subnet, and the IP is only attached while the instance is actually running. It doesn't apply to any other ENIs on the subnet.
The only way to associate multiple public IP addresses with a single EC2 instance is by using Elastic IP addresses (EIP), which can be attached to individual ENIs or can be bound directly to one specific private IP of an ENI where the ENI has secondary private IPs.
An hourly charge applies for each EIP attached to an EC2 instance, whether the instance is running or stopped, except for the first EIP, which is not billed as long as the instance is running. EIPs also incur charges when allocated but not attached to any ENI, to discourage inefficient use.