Question
You are managing a Terraform project that uses modules to deploy AWS resources across multiple environments (development, staging, production). You want to ensure that configuration settings like VPC CIDR blocks or instance sizes vary across these environments without duplicating code.
Which Terraform feature should you use to make this configuration dynamic and environment-specific?
Correct Answer: C
Explanation:
The correct choice is Option C because:
- Input Variables:
- Terraform modules can define input variables with default values or allow overriding them at runtime through environment variables or CLI arguments. This makes it easy to customize settings like CIDR blocks, instance types, or resource configurations across different environments without duplicating code.
- Dynamic & Reusable Configuration:
- Using input variables allows modules to stay environment-agnostic, while allowing teams to pass in the environment-specific values as needed.
- Scalability & Flexibility:
- This avoids hardcoding values and allows for flexibility across environments by setting variable values at runtime using commands like:
Why Other Options Are Incorrect
Option A:
Hardcoding values leads to duplication, reduces maintainability, and makes it harder to manage changes across environments. This goes against Terraform’s modularity and reusability principles.
Option B:
While Terraform Workspaces can isolate state across environments, they do not allow you to dynamically pass different configurations like CIDR ranges or instance types. They only separate Terraform state, not the configuration itself.
Option D:
Creating completely separate modules and repository branches for each environment leads to unnecessary duplication, increased maintenance overhead, and higher chances of inconsistency across environments.
Correct Answer: C
Explanation:
The correct choice is Option C because:
- Input Variables:
- Terraform modules can define input variables with default values or allow overriding them at runtime through environment variables or CLI arguments. This makes it easy to customize settings like CIDR blocks, instance types, or resource configurations across different environments without duplicating code.
- Dynamic & Reusable Configuration:
- Using input variables allows modules to stay environment-agnostic, while allowing teams to pass in the environment-specific values as needed.
- Scalability & Flexibility:
- This avoids hardcoding values and allows for flexibility across environments by setting variable values at runtime using commands like:
Why Other Options Are Incorrect
Option A:
Hardcoding values leads to duplication, reduces maintainability, and makes it harder to manage changes across environments. This goes against Terraform’s modularity and reusability principles.
Option B:
While Terraform Workspaces can isolate state across environments, they do not allow you to dynamically pass different configurations like CIDR ranges or instance types. They only separate Terraform state, not the configuration itself.
Option D:
Creating completely separate modules and repository branches for each environment leads to unnecessary duplication, increased maintenance overhead, and higher chances of inconsistency across environments.