Description
If you omit associate_public_ip_address, subnet and launch settings determine whether the instance receives a public IPv4 address.
Potential impact
A public address can make a listening service reachable from the internet when routes and security group rules allow it.
Remediation
For an internal instance, select a private subnet and set associate_public_ip_address = false. Route public service traffic through a load balancer. Provide NAT for outbound internet access or a VPC endpoint for a supported service.
Examples
Both excerpts omit subnet_id. Supply the intended private subnet ID and configure its routes and the instance’s security group rules.
Before
hcl
resource "aws_instance" "example" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
After
hcl
resource "aws_instance" "example" {
ami = data.aws_ami.ubuntu.id
associate_public_ip_address = false
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}