Interview Prep/Terraform/Resources Variables and Expressions

Resource names must be unique globally, such as S3 buckets, across many environments and accounts. How do you generate them safely?

BeginnerScenarioResources Variables and Expressions
Answer

Situation read → Hardcoded names collide.

What I'd do →

  1. Build names from project, environment and account ID.
  2. Add a random_id suffix only where uniqueness can't be derived.
data "aws_caller_identity" "current" {}
resource "aws_s3_bucket" "logs" {
  bucket = "${var.project}-${var.environment}-logs-${data.aws_caller_identity.current.account_id}"
}

Why → Derived names are predictable and discoverable, while random suffixes make things hard to find. Random values are stored in state, so they stay stable.

Interview tip → Use name_prefix for resources that must be replaced create-before-destroy.

All Terraform questions Roadmap: Terraform

More on Resources Variables and Expressions