HashiCorp Terraform Associate 003 Exam  Dumps and Practice Test Questions Set 3 Q 41- 60

Visit here for our full HashiCorp Terraform Associate 003 exam dumps and practice test questions.

Question 41

Which of the following best describes a Terraform data source?

A) A mechanism to pass input values into modules

B) A way to fetch information from existing infrastructure without creating new resources

C) A feature to apply changes to infrastructure

D) A command to validate configuration syntax

ANSWER: B) A way to fetch information from existing infrastructure without creating new resources

EXPLANATION

Terraform provides data sources to allow configurations to retrieve information from existing infrastructure or external systems. This is essential when you want to reference resources that are not managed by the current Terraform configuration.

A) A mechanism to pass input values into modules
Input values into modules are handled by variables, not data sources. Variables allow you to parameterize module behavior by providing values from outside the module. While data sources may feed values into modules indirectly, their primary role is to fetch existing infrastructure data. A is incorrect.

B) A way to fetch information from existing infrastructure without creating new resources
This is the correct answer. Data sources allow Terraform to query external systems or existing resources to retrieve their attributes without creating new resources. For example, you can use an AWS AMI data source to fetch the latest Amazon Linux AMI ID dynamically, or query existing VPCs or subnets. The advantage of using data sources is that it allows configurations to be dynamic and adaptive to changes in existing infrastructure. Data sources enable reusable modules that can adapt to different environments without hardcoding values. By fetching existing values, data sources prevent duplication, maintain consistency, and simplify infrastructure management.

C) A feature to apply changes to infrastructure
Terraform apply is responsible for creating, updating, or deleting resources based on configuration and state. Data sources do not perform any actions that modify infrastructure; they only read information. C is incorrect.

D) A command to validate configuration syntax
Validation of configuration syntax is performed by terraform validate. Data sources are not involved in validation. D is incorrect.

Using data sources allows Terraform configurations to remain flexible, dynamic, and capable of interacting with existing infrastructure while avoiding unnecessary duplication. This makes B the correct choice.

Question 42

Which Terraform construct allows the computation of values within a module that can be reused elsewhere in the same module?

A) Variables

B) Locals

C) Outputs

D) Providers

ANSWER: B) Locals

EXPLANATION

Terraform provides mechanisms to compute, store, and propagate values. Locals are specifically designed for internal computation and reuse within a module, reducing redundancy and improving readability.

A) Variables
Variables allow external input to influence module behavior. They are designed for parameterization and external configuration, not for computing internal values. A is incorrect.

B) Locals
This is the correct answer. Locals allow you to define intermediate or computed values within a module. They are not intended for input or output but for internal reuse and simplification. For example, a local can compute a combined resource name or calculate a derived attribute based on other variables. Locals improve readability, reduce repetitive expressions, and centralize computations. They allow complex configurations to remain modular, maintainable, and easy to understand. By using locals, you avoid repeated calculations and ensure consistent values across multiple resources in the module.

C) Outputs
Outputs expose values from a module to the parent configuration or other modules. They are not intended for internal computation and reuse within a module. C is incorrect.

D) Providers
Providers define the set of resources Terraform can manage and how it interacts with infrastructure APIs. Providers do not compute values or store intermediate results. D is incorrect.

Locals provide a safe, efficient mechanism to compute values for reuse within a module, making B the correct choice.

Question 43

Which of the following commands is used to display the dependency graph of Terraform resources?

A) terraform show

B) terraform graph

C) terraform plan

D) terraform fmt

ANSWER: B) terraform graph

EXPLANATION

Understanding dependencies among resources is crucial for Terraform to execute operations in the correct order. Terraform provides a dedicated command for visualizing these relationships.

A) terraform show
Terraform show displays the current state of resources in human-readable or JSON format. While it presents resource attributes, it does not provide a visual representation of resource dependencies. A is incorrect.

B) terraform graph
This is the correct answer. Terraform graph outputs the dependency graph of resources in DOT format. This graph can be rendered using visualization tools like Graphviz to display how resources depend on each other. Understanding the dependency graph is useful for troubleshooting, optimizing resource creation order, and documenting infrastructure. The graph shows which resources must be created before others, which is particularly important for complex deployments where resource interdependencies exist. This helps in understanding infrastructure relationships, detecting potential cycles, and ensuring predictable Terraform operations.

C) terraform plan
Terraform plan previews changes to the infrastructure but does not provide a detailed dependency graph. While plan considers dependencies internally, it does not produce a visual or structured graph for analysis. C is incorrect.

D) terraform fmt
Terraform fmt is used to format configuration files for readability. It does not analyze dependencies or produce graphs. D is incorrect.

Terraform graph is an essential tool for visualizing and analyzing resource dependencies, making B the correct choice.

Question 44

Which of the following describes the purpose of Terraform terraform taint?

A) To mark a resource for recreation during the next apply

B) To validate the syntax of configuration files

C) To remove a resource from the state file without destroying it

D) To format Terraform configuration files

ANSWER: A) To mark a resource for recreation during the next apply

EXPLANATION

Terraform taint is a state management feature that allows deliberate marking of resources as needing replacement. This is useful in troubleshooting or forcing a resource to be recreated.

A) To mark a resource for recreation during the next apply
This is the correct answer. Terraform taint marks a resource as tainted, signaling Terraform that the resource is invalid or requires replacement. During the next apply operation, Terraform will destroy the tainted resource and recreate it according to the configuration. This is helpful when a resource is in an inconsistent or failed state or when updates require recreation due to provider limitations. Taint does not immediately destroy the resource; it simply flags it for replacement in the next execution.

B) To validate the syntax of configuration files
Validation is performed by terraform validate. Taint is not a syntax validation tool. B is incorrect.

C) To remove a resource from the state file without destroying it
Terraform state rm is used for removing resources from the state without affecting real infrastructure. Taint marks resources for recreation instead, so C is incorrect.

D) To format Terraform configuration files
Terraform fmt formats configuration files for readability. Taint does not modify files or formatting. D is incorrect.

Terraform taint provides a controlled way to ensure that a resource is replaced, making A the correct choice.

Question 45

Which of the following best describes the Terraform terraform destroy command?

A) It displays the current state of resources

B) It removes resources defined in the configuration from the real infrastructure and updates the state file

C) It validates configuration syntax

D) It initializes the working directory

ANSWER: B) It removes resources defined in the configuration from the real infrastructure and updates the state file

EXPLANATION

Terraform destroy is a critical command for safely removing infrastructure. Understanding its function and consequences is essential.

A) It displays the current state of resources
Displaying state is performed by terraform show. Destroy does not provide a read-only view of resources. A is incorrect.

B) It removes resources defined in the configuration from the real infrastructure and updates the state file
This is the correct answer. Terraform destroy reads the configuration and the current state, then removes all managed resources from the real-world infrastructure. After execution, it updates the state file to reflect that the resources no longer exist. Destroy is often used in testing environments, for cleaning up temporary infrastructure, or for decommissioning resources. It is irreversible unless backups exist. Users must exercise caution when executing destroy in production environments, as it will remove all tracked resources. Destroy respects dependencies, ensuring that resources are removed in the correct order to avoid errors or conflicts.

C) It validates configuration syntax
Syntax validation is performed by terraform validate. Destroy modifies infrastructure rather than checking configuration correctness. C is incorrect.

D) It initializes the working directory
Initialization is handled by terraform init. Destroy does not perform initialization. D is incorrect.

Terraform destroy provides a reliable mechanism for safely removing infrastructure, making B the correct choice.

Question 46

Which Terraform command allows you to copy the current state of a resource to a new name or module within the state file?

A) terraform import

B) terraform state mv

C) terraform state rm

D) terraform apply

ANSWER: B) terraform state mv

EXPLANATION

Terraform provides advanced state management commands to manipulate the state file independently of real-world infrastructure. Understanding these commands is critical for safely reorganizing resources or modules.

A) terraform import
Terraform import adds an existing resource to the Terraform state, mapping it to a resource block in your configuration. While import introduces resources into state, it does not rename or move them within the state. Import creates a new entry rather than relocating an existing one. A is incorrect.

B) terraform state mv
This is the correct answer. Terraform state mv allows a resource to be renamed or moved within the state file. This is particularly useful when refactoring modules, reorganizing resource names, or moving resources between modules without recreating them. By moving resources in the state, Terraform can correctly track the existing infrastructure under the new configuration. This ensures that apply operations do not attempt to recreate or destroy the resource. It is a safe and controlled method for state restructuring. For example, if you change a resource block’s module path, state mv aligns the state to the new module hierarchy while preserving the actual infrastructure. This maintains consistency and prevents downtime.

C) terraform state rm
Terraform state rm removes a resource from the state file without affecting the real infrastructure. It does not rename or move resources; it only detaches Terraform from tracking the resource. C is incorrect.

D) terraform apply
Terraform apply executes changes to infrastructure to align with configuration. It does not manipulate the state independently, nor does it move or rename resources within the state. D is incorrect.

Terraform state mv is a crucial tool for managing state during refactoring, module restructuring, or renaming resources, making B the correct choice.

Question 47

Which of the following Terraform commands is used to generate a plan that shows what actions Terraform will take without actually applying any changes?

A) terraform apply

B) terraform plan

C) terraform init

D) terraform show

ANSWER: B) terraform plan

EXPLANATION

Terraform plan is one of the most important commands for safely managing infrastructure, allowing users to review proposed changes before execution.

A) terraform apply
Terraform apply executes the planned changes, creating, updating, or deleting resources. While apply internally creates a plan before execution, it does not serve as a read-only preview command. A is incorrect.

B) terraform plan
This is the correct answer. Terraform plan evaluates the differences between the current state and the configuration to generate an execution plan. It provides a detailed report of what resources will be created, updated, or destroyed without applying changes to real infrastructure. This preview allows teams to review proposed actions, catch unintended changes, and approve or adjust plans before execution. Plan also considers dependencies, ensuring the correct order of operations. It is often integrated into CI/CD pipelines to automate validation of infrastructure changes while minimizing risk.

C) terraform init
Terraform init initializes the working directory, downloads providers, and configures the backend. It does not generate execution plans or preview resource changes. C is incorrect.

D) terraform show
Terraform show displays the current state of resources managed by Terraform. While informative, it does not provide a preview of planned changes based on the configuration. D is incorrect.

Terraform plan provides a safe and effective way to understand intended infrastructure changes, making B the correct choice.

Question 48

What is the main purpose of Terraform terraform state list?

A) To display all resources in the Terraform state file

B) To apply changes to resources

C) To validate configuration syntax

D) To format configuration files

ANSWER: A) To display all resources in the Terraform state file

EXPLANATION

Terraform state commands allow detailed inspection and management of the state file. The terraform state list command specifically focuses on enumerating resources tracked in the state.

A) To display all resources in the Terraform state file
This is the correct answer. Terraform state list outputs a list of all resources currently tracked in the state file. Each resource is displayed with its full address, including module paths and resource names. This is essential for auditing, debugging, and understanding which resources Terraform manages. By listing resources, operators can identify dependencies, check module references, and confirm the presence or absence of specific resources. It also helps in preparing for state manipulation operations such as state mv or state rm.

B) To apply changes to resources
Terraform apply executes changes in real infrastructure based on the configuration and state. State list does not perform any modifications; it only reads and displays information. B is incorrect.

C) To validate configuration syntax
Validation is performed by terraform validate. State list is unrelated to syntax or configuration correctness. C is incorrect.

D) To format configuration files
Formatting is done by terraform fmt. State list does not affect file formatting. D is incorrect.

Terraform state list is a valuable tool for inspecting tracked resources, understanding module hierarchies, and managing complex state, making A the correct choice.

Question 49

Which Terraform command is used to retrieve the current values of outputs defined in a configuration?

A) terraform output

B) terraform plan

C) terraform validate

D) terraform show

ANSWER: A) terraform output

EXPLANATION

Terraform outputs are used to expose resource or module information for use elsewhere, either in another module, automation scripts, or directly for inspection. Accessing these values requires a specific command.

A) terraform output
This is the correct answer. Terraform output reads the values of all outputs defined in the configuration or module and displays them to the user. It can display outputs in human-readable or JSON format, making it suitable for scripts and automation. Outputs provide important information like resource IDs, IP addresses, or dynamically computed attributes that are required for further configuration or integration with external systems. Terraform output allows users to consume these values after apply or to confirm the expected output values.

B) terraform plan
Terraform plan previews changes that would occur based on the configuration. It does not retrieve defined output values. B is incorrect.

C) terraform validate
Terraform validate checks the configuration for syntax errors and internal consistency. It does not retrieve outputs. C is incorrect.

D) terraform show
Terraform show displays the entire state in human-readable or JSON format, including resource attributes and outputs. While it can show outputs indirectly, terraform output provides a concise, focused view specifically for retrieving outputs. D is less precise and therefore incorrect.

Terraform output provides a structured mechanism for accessing values exposed by the configuration, making A the correct choice.

Question 50

Which Terraform feature allows you to define a reusable and parameterized block of resources that can be invoked multiple times across different configurations?

A) Workspaces

B) Modules

C) Locals

D) Providers

ANSWER: B) Modules

EXPLANATION

Terraform modules are a central feature for code reuse, parameterization, and standardized infrastructure deployment. They encapsulate resources, variables, and outputs in a reusable structure.

A) Workspaces
Workspaces isolate state files to allow multiple environments to share the same configuration. They do not encapsulate reusable resources with variables and outputs. A is incorrect.

B) Modules
This is the correct answer. Modules allow developers to package resources into a reusable block with input variables and outputs. They can be stored locally or remotely, making them sharable across teams and projects. Modules enable standardization of best practices, reduce redundancy, and allow complex infrastructure to be composed from simpler components. They also support parameterization, allowing the same module to adapt to different environments or resource configurations. Modules are invoked using the module block and can be nested for hierarchical organization.

C) Locals
Locals allow the computation of values within a module for reuse. They do not encapsulate resources or provide a reusable infrastructure block. C is incorrect.

D) Providers
Providers define the APIs and resources Terraform can manage. They do not package reusable blocks of resources. D is incorrect.

Modules provide the foundation for reusable, maintainable, and parameterized Terraform infrastructure, making B the correct choice.

Question 51

Which Terraform command is used to bring existing infrastructure under Terraform management by creating state entries for resources that were created manually?

A) terraform import

B) terraform plan

C) terraform apply

D) terraform validate

ANSWER: A) terraform import

EXPLANATION

Terraform import is a powerful command designed to integrate existing infrastructure that was not originally created using Terraform into the Terraform state. This capability is crucial for organizations adopting Terraform incrementally or managing hybrid environments where some resources exist outside of Terraform control.

A) terraform import
This is the correct answer. Terraform import allows an existing resource, such as an AWS EC2 instance or an Azure storage account, to be mapped into the Terraform state file. This enables Terraform to recognize the resource and manage it going forward. Import does not create new resources; instead, it creates state entries that correspond to resources already present in the infrastructure. After importing, the configuration must be updated to reflect the actual resource attributes. Import is particularly useful when transitioning from manually provisioned resources to Infrastructure as Code, as it allows teams to maintain continuity without recreating resources. The import process involves specifying the resource type, name, and provider-specific ID. Importing does not automatically generate configuration files; users must manually ensure that the configuration matches the imported resource to avoid future discrepancies.

B) terraform plan
Terraform plan previews the actions Terraform will take based on the differences between configuration and state. It does not bring existing resources under management; it only calculates changes to resources already tracked in the state. Therefore, B is incorrect.

C) terraform apply
Terraform apply executes changes to infrastructure, creating, updating, or deleting resources to align with configuration. While apply modifies resources, it does not import existing ones into state. Apply assumes that resources are already defined in state or configuration. C is incorrect.

D) terraform validate
Terraform validate checks the syntax and internal consistency of configuration files. It does not interact with existing infrastructure or state management. D is incorrect.

Terraform import is essential for integrating existing infrastructure into Terraform management. By using import, organizations can transition from manual provisioning to Terraform-managed infrastructure without service disruption, enabling consistent, predictable, and versioned infrastructure management. This makes A the correct choice.

Question 52

Which Terraform construct allows you to define environment-specific variables and manage different configurations for dev, staging, and production environments?

A) Modules

B) Workspaces

C) Providers

D) Locals

ANSWER: B) Workspaces

EXPLANATION

Terraform provides several mechanisms to manage environments, but workspaces are the native solution for maintaining isolated state files and managing environment-specific configurations.

A) Modules
Modules are used to package and reuse infrastructure code but do not inherently provide environment isolation. They can be parameterized with input variables to adapt to different environments, but managing separate state files per environment requires additional mechanisms. Therefore, A is incorrect.

B) Workspaces
This is the correct answer. Workspaces allow you to maintain multiple state files for the same configuration, enabling separate deployments for environments such as dev, staging, and production. Each workspace has its own state, ensuring that changes in one environment do not affect another. Switching workspaces is simple and allows teams to deploy the same configuration across multiple environments with minimal duplication. Workspaces are particularly useful in CI/CD pipelines, where automated deployments require isolated states for testing and production. They also help in reducing risks associated with accidental changes in production. For example, a single Terraform configuration can define a network and compute resources, and different workspaces can deploy the same configuration with environment-specific parameters, such as smaller instance sizes for dev and larger ones for production. Workspaces integrate seamlessly with backends to store remote states, enabling collaboration among multiple users.

C) Providers
Providers define which platforms and resources Terraform can manage, but they do not manage environments or state separation. Providers can be configured differently per environment but do not inherently provide environment isolation. C is incorrect.

D) Locals
Locals allow the computation of intermediate values within a module but do not provide state isolation or environment-specific management. D is incorrect.

Workspaces offer a structured and scalable approach to managing multiple environments, enabling teams to maintain consistent, isolated, and reproducible deployments. This makes B the correct choice.

Question 53

Which Terraform feature allows you to fetch and use dynamic values from existing resources or external APIs during the execution of a configuration?

A) Variables

B) Data sources

C) Locals

D) Outputs

ANSWER: B) Data sources

EXPLANATION

Terraform provides mechanisms for incorporating dynamic and existing values into configurations. Data sources are specifically designed to retrieve information from existing infrastructure or external APIs without creating new resources.

A) Variables
Variables are used to provide input values to configurations, allowing parameterization and flexibility. While variables can influence the resources, they do not dynamically fetch existing resource attributes or external data. A is incorrect.

B) Data sources
This is the correct answer. Data sources allow Terraform to query existing infrastructure or external APIs and use the retrieved values in configurations. For example, a data source can retrieve the latest AMI ID from AWS, query an existing VPC, or fetch metadata from a cloud service. Data sources make configurations dynamic, reusable, and adaptable to changing infrastructure environments. By leveraging data sources, users avoid hardcoding values, which improves maintainability and reduces errors. Data sources are read-only; they do not create or modify resources, ensuring safe integration with existing infrastructure. They are particularly useful in modules, enabling reusable and flexible configurations that can adapt to different environments or resources dynamically.

C) Locals
Locals allow computation and reuse of values within a module, but they cannot fetch data from external sources or existing resources. They depend on input variables or static values. C is incorrect.

D) Outputs
Outputs expose values from resources or modules for use elsewhere, but they do not fetch dynamic information from external sources. D is incorrect.

Data sources enable Terraform to remain dynamic, adaptive, and reusable by integrating existing infrastructure or external APIs into configurations. This makes B the correct choice.

Question 54

Which Terraform command is used to remove resources from both the state file and real-world infrastructure?

A) terraform apply

B) terraform destroy

C) terraform state rm

D) terraform validate

ANSWER: B) terraform destroy

EXPLANATION

Terraform provides multiple commands for managing resources, but destroying infrastructure safely requires a dedicated command. Understanding the difference between state manipulation and real resource destruction is crucial.

A) terraform apply
Terraform apply creates, updates, or deletes resources to align infrastructure with the configuration. While it may remove resources if they are removed from the configuration, it is not specifically designed as a dedicated destruction command. A is incorrect.

B) terraform destroy
This is the correct answer. Terraform destroy reads the configuration and state, then removes all tracked resources from both the state file and the real-world infrastructure. Destroy ensures dependencies are respected during removal to prevent errors or orphaned resources. It is commonly used for cleaning up temporary environments, decommissioning infrastructure, or testing. Destroy is irreversible unless backup states exist, so it should be used with caution, especially in production environments. During destroy, Terraform first calculates the resource dependencies to determine the correct order of deletion, ensuring safe teardown of infrastructure. This command is essential for managing lifecycle events when infrastructure is no longer needed or needs to be rebuilt from scratch.

C) terraform state rm
Terraform state rm removes resources from the state file without affecting actual infrastructure. It is safe for decoupling resources but does not delete real-world resources. C is incorrect.

D) terraform validate
Terraform validate checks configuration syntax and internal consistency. It does not interact with infrastructure or state. D is incorrect.

Terraform destroy is the only command designed to safely remove all managed infrastructure and update the state accordingly, making B the correct choice.

Question 55

Which Terraform feature allows you to create reusable logic that computes values dynamically based on other input values or computed values within a module?

A) Variables

B) Locals

C) Outputs

D) Providers

ANSWER: B) Locals

EXPLANATION

Terraform provides several constructs for managing data and values. Locals are specifically designed for internal computation and dynamic value derivation, providing a mechanism for reusable logic within a module.

A) Variables
Variables provide external input values to a module or configuration, enabling parameterization. While variables influence computation, they do not themselves compute derived values within the module. A is incorrect.

B) Locals
This is the correct answer. Locals allow the definition of intermediate or computed values based on input variables, other locals, or resource attributes. They centralize repeated expressions and calculations, reducing redundancy and ensuring consistency. For example, locals can compute a derived resource name, concatenate attributes, or calculate sizes and quantities dynamically. Locals improve readability, maintainability, and reusability of code by allowing complex logic to be encapsulated in a single expression that can be referenced multiple times across the module. Using locals also reduces the likelihood of errors when the same computation is needed in multiple resources. Locals cannot be accessed outside the module, ensuring encapsulation and modular design.

C) Outputs
Outputs expose values from a module for consumption elsewhere but do not compute intermediate values for internal reuse. C is incorrect.

D) Providers
Providers define which platforms and resources Terraform can manage. They do not perform computations or create reusable logic within modules. D is incorrect.

Locals provide a safe and efficient way to compute dynamic values, centralize logic, and reuse computed results within a module, making B the correct choice.

Question 56

Which Terraform command is used to upgrade the provider plugins to the latest acceptable version defined in the configuration?

A) terraform init -upgrade

B) terraform plan

C) terraform apply

D) terraform validate

ANSWER: A) terraform init -upgrade

EXPLANATION

Terraform providers are essential components that enable Terraform to interact with cloud services or APIs. Providers have versions, and managing these versions is critical to ensure compatibility, stability, and access to new features. Upgrading provider plugins is necessary when introducing new functionality, fixing bugs, or aligning with updated infrastructure standards.

A) terraform init -upgrade
This is the correct answer. Terraform init -upgrade performs initialization while also upgrading provider plugins to the newest versions allowed by version constraints specified in the configuration. It downloads the latest provider binaries, ensures compatibility with the required Terraform version, and updates the local plugin cache. Upgrading providers ensures that Terraform can use the latest features, improvements, and bug fixes while maintaining the constraints defined by the user. This command is particularly useful in long-lived projects where providers evolve over time. Without upgrading, configurations may rely on outdated provider features or miss enhancements and bug fixes. Using terraform init -upgrade also ensures the backend configuration is respected and that modules depending on provider updates function correctly. This command integrates both initialization and plugin upgrade in a single step, which is safer and more reliable than manually replacing binaries.

B) terraform plan
Terraform plan generates an execution plan showing what changes Terraform would make. It does not interact with provider plugins or upgrade them. Plan operates using the currently installed provider versions and the existing state. Therefore, B is incorrect.

C) terraform apply
Terraform apply executes changes based on the configuration and state. While apply relies on provider plugins to interact with infrastructure, it does not upgrade them. Apply assumes the providers are already installed and compatible with the configuration. C is incorrect.

D) terraform validate
Terraform validate checks the configuration files for syntax correctness and internal consistency. Validation does not interact with provider versions or upgrade them. D is incorrect.

Terraform init -upgrade is the definitive command for updating provider plugins while initializing the working directory, ensuring that Terraform projects remain compatible, secure, and feature-rich. This makes A the correct choice.

Question 57

Which Terraform feature allows you to enforce specific constraints on the values of variables to ensure correctness and prevent invalid configurations?

A) Locals

B) Variable validation

C) Outputs

D) Providers

ANSWER: B) Variable validation

EXPLANATION

Terraform variables allow parameterization and dynamic configuration. Ensuring that input values meet specific criteria is essential to prevent errors, enforce standards, and maintain infrastructure integrity.

A) Locals
Locals allow internal computation of values within a module but do not enforce constraints on external input. While locals can manipulate values programmatically, they cannot validate input correctness. A is incorrect.

B) Variable validation
This is the correct answer. Variable validation allows users to define rules or conditions that input values must satisfy. For example, a variable representing a server size might have a validation rule restricting it to a predefined list of acceptable sizes. If an input does not meet the criteria, Terraform will fail early, preventing incorrect deployments. Validation ensures predictable infrastructure behavior, reduces misconfigurations, and supports adherence to organizational policies. It is especially useful in shared modules, where multiple teams provide input values. Validation supports custom error messages, improving clarity and debugging. Implementing variable validation enhances infrastructure reliability, enforces standards, and prevents runtime failures caused by invalid inputs.

C) Outputs
Outputs expose values from a module for use in other modules or configurations but do not enforce constraints on input values. C is incorrect.

D) Providers
Providers define APIs and resources Terraform can interact with but do not perform validation of variable inputs. D is incorrect.

Variable validation ensures that infrastructure is deployed consistently, safely, and in accordance with best practices, making B the correct choice.

Question 58

Which Terraform construct allows you to reuse a set of resource definitions with variable inputs across different configurations?

A) Workspaces

B) Modules

C) Locals

D) Providers

ANSWER: B) Modules

EXPLANATION

Terraform modules are central to reusable and maintainable infrastructure code. They provide the ability to encapsulate multiple resources, variables, and outputs, allowing consistent deployment patterns across different environments or projects.

A) Workspaces
Workspaces allow multiple state files for the same configuration, supporting environment isolation. They do not encapsulate reusable resources or provide variable inputs for reusability. A is incorrect.

B) Modules
This is the correct answer. Modules package resources into reusable blocks, parameterized with variables and outputs. By using modules, developers can deploy standardized infrastructure patterns across multiple configurations while maintaining flexibility through variable inputs. Modules can be local or remote, supporting collaboration across teams. For example, a networking module can define VPCs, subnets, and routing rules that can be reused in dev, staging, and production environments with environment-specific variables. Modules improve maintainability, reduce redundancy, and support best practices by centralizing complex infrastructure logic. Nested modules allow complex architectures to be composed of simpler, reusable components, enhancing scalability and consistency.

C) Locals
Locals allow computation and reuse of values within a module but do not encapsulate resources for reuse across configurations. C is incorrect.

D) Providers
Providers define the APIs and resources Terraform can interact with. They are foundational but do not provide reusable resource blocks. D is incorrect.

Modules are fundamental to Terraform’s design philosophy of reusable, modular, and parameterized infrastructure, making B the correct choice.

Question 59

Which Terraform command allows you to review the exact changes that will occur to your infrastructure before applying them?

A) terraform plan

B) terraform apply

C) terraform destroy

D) terraform validate

ANSWER: A) terraform plan

EXPLANATION

Reviewing changes before applying them is a critical step in Terraform workflows, enabling teams to prevent errors and verify desired outcomes.

A) terraform plan
This is the correct answer. Terraform plan generates an execution plan, detailing which resources will be created, updated, or destroyed. It calculates differences between the current state and configuration, considering dependencies, attributes, and provider behavior. The plan provides a safe preview without affecting real infrastructure, allowing teams to review changes and approve or adjust them before execution. Integration with CI/CD pipelines often relies on terraform plan to automate checks and enforce review processes. Plan ensures predictable infrastructure management and reduces risk of accidental destruction or misconfiguration.

B) terraform apply
Terraform apply executes the planned changes, creating, updating, or destroying resources. While it relies on a plan, it does not provide a preview before execution; it performs the actions immediately. B is incorrect.

C) terraform destroy
Terraform destroy removes resources from infrastructure and state. It does not preview changes; it executes deletions. C is incorrect.

D) terraform validate
Terraform validate checks configuration syntax and consistency. It does not preview changes or provide execution plans. D is incorrect.

Terraform plan is essential for controlled infrastructure management, allowing users to safely review and approve changes, making A the correct choice.

Question 60

Which Terraform command is used to verify that a configuration is syntactically valid and internally consistent without interacting with real infrastructure?

A) terraform init

B) terraform validate

C) terraform plan

D) terraform apply

ANSWER: B) terraform validate

EXPLANATION

Terraform validate is a key command for ensuring correctness and reliability of configurations before executing operations that affect infrastructure.

A) terraform init
Terraform init initializes the working directory, downloads provider plugins, and configures the backend. It does not check for syntax or internal consistency. A is incorrect.

B) terraform validate
This is the correct answer. Terraform validate checks that configuration files are syntactically correct and internally consistent, without interacting with real infrastructure or changing state. It ensures that references to variables, resources, and modules are valid and that constraints are met. Validation is a critical step in CI/CD pipelines to prevent deployment errors and ensure configurations meet standards. Terraform validate is useful for detecting errors early in the development workflow, saving time and preventing infrastructure issues. While it does not guarantee successful execution of apply, it confirms that the configuration is structurally sound and logically coherent.

C) terraform plan
Terraform plan previews the changes that will be applied but requires initialized working directories and provider plugins. While useful for planning, it does not specifically validate syntax independently. C is incorrect.

D) terraform apply
Terraform apply executes changes to infrastructure. It does not serve as a validation-only command. D is incorrect.

Terraform validate provides a safe, non-destructive mechanism to ensure configuration correctness and integrity, making B the correct choice.

img