liveBook · Manning

Random_string

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

random_string is a Terraform resource type from the hashicorp/random provider that generates a random string value.

Use random_string when a Terraform resource needs a compact, non-sensitive random suffix to satisfy naming constraints. It is especially useful for resources that are difficult to recreate or require globally unique names. section 10.1.2

Add a random secret suffix

resource "random_string" "suffix" { length = 6 special = false upper = false } resource "aws_secretsmanager_secret" "main" { name = "${var.name}-${random_string.suffix.result}" } This keeps the configured prefix readable while adding six lowercase, non-special characters. The result is referenced through random_string.suffix.result. section 10.1.2

Prefer random_string here over random_id because it fits more randomness into fewer characters, and over random_password because it does not mark names as sensitive. Encapsulate resource-specific naming rules inside modules so callers do not manage them manually. section 10.1.2

Table 10.1 Examples of hierarchical naming schemes (view table figure)

System

Description

Examples

Domain Name System

Domains are split into subdomains.

example.com

auth.example.com

db.auth.example.com

Terraform

Resource names in modules inherit the module path.

module.service.aws_ecs_cluster.main  

module.lb.module.logs.aws_s3_bucket.main  

Compare hierarchical Terraform names with DNS-style naming to see how module paths and descriptive prefixes organize resources. section 10.1.2

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Demonstrates configuration and result interpolation, but does not detail provider implementation or generation internals.
Examples
Uses a six-character lowercase suffix on an AWS Secrets Manager secret and contrasts alternative random resources.
What it assumes
Assumes basic Terraform resources, expressions, modules, and provider resource naming.
What you can do after
Build resource names with a readable prefix and a referenced random_string.result suffix.
How it connects
Connects random_string with Terraform modules, aws_secretsmanager_secret, random_id, random_password, and hierarchical naming.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.