Workspace naming conventions in Terraform - Mini Project: Build & Apply
Start learning this pattern below
Jump into concepts and practice - no test required
environments with the exact list of strings: ["dev", "test", "prod"].Use variable block with default set to the list.
prefix with the exact string value "teamA-".Use a variable block with default set to "teamA-".
workspace_names that uses a for expression to combine var.prefix and each item in var.environments into a list of full workspace names.Use a for expression inside locals to concatenate strings.
workspaces that outputs the value of local.workspace_names.Use an output block with value set to local.workspace_names.
Practice
What is the main purpose of using workspace names in Terraform?
Solution
Step 1: Understand workspace role
Workspaces in Terraform separate different environments or states, like dev and prod.Step 2: Match purpose to options
Only To organize different environments like development and production describes organizing environments, which is the workspace's main use.Final Answer:
To organize different environments like development and production -> Option BQuick Check:
Workspace names = environment organization [OK]
- Confusing workspace with code storage
- Thinking workspace manages credentials
- Assuming workspace creates resources directly
Which of the following workspace names follows best naming conventions in Terraform?
terraform workspace new ?Solution
Step 1: Review naming rules
Best practice is lowercase letters, hyphens allowed, no underscores or uppercase.Step 2: Check each option
staging-1 uses lowercase and hyphen, fitting the rules. Others use uppercase or underscores.Final Answer:
staging-1 -> Option AQuick Check:
Lowercase + hyphen = staging-1 [OK]
- Using uppercase letters
- Using underscores instead of hyphens
- Including spaces or special characters
What will happen if you run the command terraform workspace select prod-env but the workspace prod-env does not exist?
Solution
Step 1: Understand workspace select behavior
The select command switches to an existing workspace; it does not create one.Step 2: Check command outcome
If the workspace does not exist, Terraform returns an error message.Final Answer:
Terraform shows an error saying workspace not found -> Option AQuick Check:
Select non-existent workspace = error [OK]
- Assuming select creates workspace
- Thinking Terraform switches to default silently
- Believing workspace gets deleted
Identify the error in this workspace creation command:
terraform workspace new Dev_EnvSolution
Step 1: Check naming rules for characters
Workspace names should use lowercase letters and hyphens; underscores are not recommended.Step 2: Analyze the command
The name 'Dev_Env' contains an underscore, which breaks the naming convention.Final Answer:
Workspace names cannot contain underscores -> Option DQuick Check:
Underscores disallowed in workspace names [OK]
- Thinking uppercase letters are forbidden (they are discouraged but allowed)
- Believing names must start with numbers
- Assuming length restrictions apply
You want to create workspaces for three environments: development, testing, and production. Which set of workspace names follows best practices for naming conventions?
Solution
Step 1: Review naming best practices
Use lowercase letters, hyphens allowed, no underscores or uppercase letters.Step 2: Evaluate each option
dev-01, test-01, prod-01 uses lowercase and hyphens with numbers, fitting best practices. dev, test_env, production uses underscore, B is long but valid, D uses uppercase.Step 3: Choose best consistent and clear naming
dev-01, test-01, prod-01 is concise, consistent, and follows all rules.Final Answer:
dev-01, test-01, prod-01 -> Option CQuick Check:
Lowercase + hyphens + numbers = best practice [OK]
- Using uppercase letters
- Using underscores
- Choosing inconsistent naming styles
