gotchabashterraformModerate
Resource naming collisions when multiple Terraform resources share a logical name
Viewed 0 times
resource blocklogical nameresource addressnaming conventionstate addressduplicate resource
Error Messages
Problem
Terraform resource blocks require a unique combination of resource type and logical name within a module. Reusing a logical name across different invocations of the same resource type (e.g., two
aws_s3_bucket blocks both named main) causes a parse error. Developers also frequently confuse the Terraform logical name with the cloud resource name attribute.Solution
Use distinct, descriptive logical names for resource blocks. Set the actual cloud resource name via the
name or bucket argument, not the block label. Follow a consistent naming convention like <service>_<purpose>.resource "aws_s3_bucket" "app_assets" {
bucket = "my-app-assets-${var.environment}"
}
resource "aws_s3_bucket" "app_logs" {
bucket = "my-app-logs-${var.environment}"
}Why
The resource block label is a Terraform-internal identifier used to build the state address (
aws_s3_bucket.app_assets). It has no relationship to the cloud resource name unless you set them to the same string intentionally.Gotchas
- Changing a resource's logical name forces destroy/recreate unless you use
terraform state mvfirst - Module logical names follow the same rules — two
moduleblocks cannot share a name - Resource addresses in state are case-sensitive
Context
Defining multiple resources of the same type within a single module
Revisions (0)
No revisions yet.