Description
publicly_accessible controls whether an RDS instance uses a public address. Internet clients can reach it only if the subnet routes and security groups allow the connection.
Potential impact
Unintended clients may be able to attempt logins. Reads or changes to data still depend on credentials and database permissions.
Remediation
- For an internal instance, set
publicly_accessible = false. If you use a module, verify that it passes the value to the instance. Allow only required clients through subnet routes, network ACLs and security groups. - Before applying, test DNS resolution, connectivity and authentication on the private path from applications and administrators. Use VPN or Direct Connect where an external private network needs access.
- This setting takes effect immediately regardless of
apply_immediately. It causes no DB downtime itself, but clients on the public path may disconnect. Checkterraform planfor other changes.
Examples
Supply a supported db_instance_class, a DB subnet group in the same VPC and the security-group ID variables. Protect the RDS-managed password and Terraform state.
Before
hcl
resource "aws_db_instance" "public_instance" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
instance_class = var.db_instance_class
db_name = "mydb"
username = "foo"
manage_master_user_password = true
db_subnet_group_name = var.db_subnet_group_name
vpc_security_group_ids = var.db_security_group_ids
publicly_accessible = true
}
After
hcl
resource "aws_db_instance" "public_instance" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
instance_class = var.db_instance_class
db_name = "mydb"
username = "foo"
manage_master_user_password = true
db_subnet_group_name = var.db_subnet_group_name
vpc_security_group_ids = var.db_security_group_ids
publicly_accessible = false
}
Explanation:
- Before: The instance requests public addressing. Network rules still determine which clients can reach it.
- After: The same resource requests private addressing. Test the intended client connections after deployment.
References
- CWE-668
- AWS DB Instance publicly_accessible reference
- Reviewed Terraform AWS provider RDS implementation
- RDS module v3.0.0 inputs
- Terraform type conversion
- RDS connections and addressing in a VPC
- Changing RDS public-access settings
- DB instance argument changes in AWS provider 5.0
- RDS secret management
- Managing sensitive data in Terraform