HashiCorp Terraform Associate 003 Exam  Dumps and Practice Test Questions Set 4 Q 61- 80

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

Question 61

Which Terraform construct allows you to dynamically create multiple instances of a resource based on a count or a collection?

A) Variables

B) count and for_each

C) Providers

D) Outputs

ANSWER: B) count and for_each

EXPLANATION

Terraform provides mechanisms for dynamic resource creation, which is essential for scaling infrastructure efficiently without duplicating configuration code.

A) Variables
Variables provide input values to Terraform configurations but do not directly create multiple instances of a resource. While variables can be used in conjunction with dynamic constructs, they themselves only hold values. A is incorrect.

B) count and for_each
This is the correct answer. Count allows resources to be created multiple times based on an integer value, creating identical resources. For_each iterates over maps or sets, enabling the creation of uniquely identified resources with distinct attributes. Both constructs allow dynamic and reusable infrastructure definitions. For example, a list of server names can be passed to for_each to create multiple EC2 instances with different configurations, while count could be used to create three identical instances. These constructs reduce duplication, increase maintainability, and improve scalability. Terraform calculates dependencies and ensures proper ordering of resource creation when using count or for_each, maintaining consistency across deployments. Using these constructs enhances modularity, allows easier automation, and supports large-scale deployments with minimal configuration repetition.

C) Providers
Providers define the APIs and resources Terraform can manage but do not control the number of instances or create resources dynamically. C is incorrect.

D) Outputs
Outputs expose values from resources or modules but do not create resources. They are primarily used for passing information to other modules or configurations. D is incorrect.

Count and for_each are fundamental tools in Terraform for creating dynamic, scalable infrastructure configurations efficiently and predictably, making B the correct choice.

Question 62

Which Terraform command displays a human-readable representation of the current state of managed resources?

A) terraform show

B) terraform plan

C) terraform validate

D) terraform init

ANSWER: A) terraform show

EXPLANATION

Terraform show is an essential command for inspecting the current state of infrastructure. It provides detailed insight into resource attributes and configurations.

A) terraform show
This is the correct answer. Terraform show reads the state file and displays the current status of resources in a human-readable or JSON format. It allows users to inspect the values of attributes, dependencies, and outputs. Show is useful for auditing, debugging, and understanding the real-world infrastructure managed by Terraform. It helps confirm that resources exist, review applied changes, and verify outputs. This command is especially helpful in large-scale environments where multiple resources exist, providing a comprehensive overview of the state without directly accessing cloud provider dashboards.

B) terraform plan
Terraform plan previews changes that will be made based on configuration differences but does not display the existing state in detail. B is incorrect.

C) terraform validate
Terraform validate checks the syntax and internal consistency of configuration files but does not display state or resource attributes. C is incorrect.

D) terraform init
Terraform init initializes the working directory, downloads provider plugins, and configures backends. It does not display resource state. D is incorrect.

Terraform show provides clarity on the current state of managed infrastructure, enabling auditing, monitoring, and verification, making A the correct choice.

Question 63

Which Terraform feature allows you to store infrastructure state remotely and enable collaboration among multiple users?

A) Providers

B) Backends

C) Workspaces

D) Modules

ANSWER: B) Backends

EXPLANATION

Managing Terraform state is critical for collaboration and consistent deployments. Backends handle remote storage, locking, and state management.

A) Providers
Providers define the APIs and resources Terraform can manage. They do not handle state storage or collaboration. A is incorrect.

B) Backends
This is the correct answer. Backends allow Terraform to store state files remotely in services like AWS S3, Azure Blob Storage, Google Cloud Storage, or Terraform Cloud. Remote backends enable multiple team members to collaborate by centralizing the state file, implementing state locking, and providing version history. This prevents conflicts and ensures that state remains consistent across environments. Backends can also encrypt state, manage access permissions, and integrate with CI/CD pipelines. They are critical for production environments, where multiple operators may perform Terraform operations concurrently. Using backends improves reliability, reduces errors, and enables shared infrastructure management. For example, S3 backends with DynamoDB state locking prevent simultaneous apply operations that could corrupt state, ensuring safe collaborative workflows.

C) Workspaces
Workspaces allow isolated state files for different environments but do not inherently provide remote storage or team collaboration features. C is incorrect.

D) Modules
Modules provide reusable infrastructure blocks but do not handle state storage or collaboration. D is incorrect.

Backends are essential for centralizing state, enabling collaboration, preventing conflicts, and supporting scalable Terraform workflows, making B the correct choice.

Question 64

Which Terraform command is used to check the syntax and internal consistency of a configuration without affecting real infrastructure?

A) terraform apply

B) terraform validate

C) terraform plan

D) terraform init

ANSWER: B) terraform validate

EXPLANATION

Validating Terraform configurations ensures correctness and prevents runtime errors. Terraform provides a dedicated command for this purpose.

A) terraform apply
Terraform apply executes changes to infrastructure and updates the state. It does not perform a non-destructive syntax check. A is incorrect.

B) terraform validate
This is the correct answer. Terraform validate checks the syntax and internal consistency of configuration files without interacting with infrastructure or the state. It ensures that resource references, variables, and modules are correctly defined and logically consistent. Validate is commonly used in CI/CD pipelines to catch configuration errors early, ensuring reliability and preventing failed applies. While it does not guarantee successful execution, it ensures that the configuration is structurally sound and internally coherent. Validation improves workflow efficiency, reduces debugging effort, and enhances confidence in Terraform deployments.

C) terraform plan
Terraform plan previews changes to infrastructure but assumes configurations are syntactically correct. Plan is dependent on initialization and provider plugins and does not serve as a dedicated validation tool. C is incorrect.

D) terraform init
Terraform init initializes the working directory, downloads provider plugins, and configures backends. It does not validate syntax or internal consistency. D is incorrect.

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

Question 65

Which Terraform construct allows you to compute intermediate values within a module for reuse, improving readability and reducing redundancy?

A) Variables

B) Locals

C) Outputs

D) Providers

ANSWER: B) Locals

EXPLANATION

Terraform offers several constructs for managing values and computation. Locals are designed for internal computation and reuse, enabling more maintainable and readable configurations.

A) Variables
Variables provide external input to modules but do not compute derived values internally. While variables influence computations, they are not intended for internal logic reuse. A is incorrect.

B) Locals
This is the correct answer. Locals allow defining intermediate or computed values based on input variables, other locals, or resource attributes. They centralize repeated expressions, making the code easier to read, maintain, and debug. For instance, locals can calculate resource names, combine attributes, or compute quantities dynamically. Using locals ensures consistency and reduces errors from repetitive calculations. Locals cannot be accessed outside the module, promoting encapsulation and modular design. They are essential for large-scale infrastructure, where multiple resources depend on the same computed values. Locals improve maintainability, readability, and efficiency by consolidating complex logic into reusable expressions.

C) Outputs
Outputs expose values from modules for external use but do not compute reusable internal values. C is incorrect.

D) Providers
Providers define the APIs and resources Terraform interacts with. They do not perform computation or internal value reuse. D is incorrect.

Locals are key for efficient computation, improving readability, reducing redundancy, and maintaining modular design, making B the correct choice.

Question 66

Which Terraform command is used to format configuration files to a canonical style, making them more readable and consistent?

A) terraform validate

B) terraform fmt

C) terraform plan

D) terraform apply

ANSWER: B) terraform fmt

EXPLANATION

Terraform configurations can be written in HCL (HashiCorp Configuration Language), which is human-readable but prone to inconsistencies in formatting. Maintaining a consistent style is important for readability, maintainability, and collaboration, especially in team environments where multiple contributors edit configurations.

A) terraform validate
Terraform validate checks for syntax correctness and internal consistency of configuration files. It ensures that the configurations are valid and logically coherent but does not modify file formatting or enforce a canonical style. A is incorrect.

B) terraform fmt
This is the correct answer. Terraform fmt automatically formats configuration files according to the standard HCL conventions. It adjusts indentation, spacing, and alignment, making files easier to read and consistent across a project. Fmt is particularly useful in collaborative environments to enforce a common style and reduce merge conflicts. It can be applied recursively to a directory of configurations and integrated into CI/CD pipelines to enforce style checks before merges. Terraform fmt does not change functionality; it only improves readability and ensures adherence to HashiCorp’s style guidelines. Proper formatting enhances code maintainability, reduces errors due to misalignment or unclear structure, and improves understanding for new team members reviewing the configuration.

C) terraform plan
Terraform plan previews infrastructure changes by comparing configuration and state. While plan helps detect potential changes, it does not format files or enforce style guidelines. C is incorrect.

D) terraform apply
Terraform apply executes changes to align infrastructure with configuration but does not adjust file formatting. D is incorrect.

Terraform fmt ensures consistent, readable, and maintainable configurations, which is crucial for collaboration and long-term project health, making B the correct choice.

Question 67

Which Terraform command allows you to remove resources from the state file without destroying the actual infrastructure?

A) terraform state rm

B) terraform destroy

C) terraform apply

D) terraform plan

ANSWER: A) terraform state rm

EXPLANATION

Terraform state management commands are essential for advanced scenarios, such as decoupling resources from Terraform management without affecting real-world infrastructure.

A) terraform state rm
This is the correct answer. Terraform state rm removes a resource from the state file while leaving the actual infrastructure intact. This command is used when a resource needs to be unmanaged by Terraform, for example, if it is being migrated to another tool or manually maintained. After removal, Terraform will no longer track the resource, and subsequent apply operations will not affect it. This command provides flexibility in managing infrastructure lifecycle without inadvertently destroying resources. It is particularly useful in complex deployments where some resources must remain outside Terraform’s control or when reorganizing state for modularization. State rm maintains the integrity of the remaining state, ensuring that other managed resources are unaffected.

B) terraform destroy
Terraform destroy removes resources from both the state file and real infrastructure. It does not allow state removal without deletion. B is incorrect.

C) terraform apply
Terraform apply applies configuration changes and modifies real infrastructure according to the state and configuration. It does not remove resources from state without affecting infrastructure. C is incorrect.

D) terraform plan
Terraform plan previews changes but does not remove resources from state or infrastructure. D is incorrect.

Terraform state rm provides safe decoupling of resources from Terraform control without affecting real infrastructure, making A the correct choice.

Question 68

Which Terraform feature allows you to organize configuration into logical units that can be reused across multiple projects?

A) Providers

B) Modules

C) Workspaces

D) Locals

ANSWER: B) Modules

EXPLANATION

Terraform promotes modularity to create reusable, maintainable, and consistent infrastructure configurations. Modules encapsulate resource definitions, variables, and outputs into logical units.

A) Providers
Providers define which platforms and resources Terraform can manage. They are essential but do not encapsulate reusable blocks of resources. A is incorrect.

B) Modules
This is the correct answer. Modules package resources, variables, and outputs into reusable units that can be invoked multiple times across different projects or environments. Modules improve maintainability, reduce code duplication, and ensure consistent infrastructure patterns. They support parameterization through input variables and can return values through outputs. Modules can be local or remote, enabling sharing across teams or organizations. Nested modules allow complex infrastructure to be composed from smaller building blocks. Using modules also simplifies testing, updates, and refactoring because changes in a module propagate to all configurations using it, while keeping the overall structure modular and organized. Modules encourage standardization, making them a best practice for production-grade Terraform deployments.

C) Workspaces
Workspaces isolate state files for different environments but do not encapsulate reusable resource definitions. They manage environment-specific states rather than code reuse. C is incorrect.

D) Locals
Locals allow computation of intermediate values within a module but do not define reusable resource blocks. D is incorrect.

Modules enable scalable, maintainable, and reusable infrastructure design, making B the correct choice.

Question 69

Which Terraform construct allows you to expose computed values from a module to be used in other modules or configurations?

A) Variables

B) Outputs

C) Locals

D) Providers

ANSWER: B) Outputs

EXPLANATION

Exposing values from a module is crucial for building modular and interconnected infrastructure. Outputs allow modules to return data to calling configurations or modules.

A) Variables
Variables provide input values to modules but do not expose computed values. They only allow external configuration of module behavior. A is incorrect.

B) Outputs
This is the correct answer. Outputs define values that a module can return after execution. These can include resource IDs, IP addresses, or computed values. Outputs enable communication between modules or allow infrastructure data to be consumed by automation scripts, other modules, or external systems. They facilitate modularity by encapsulating internal logic while exposing necessary information. Outputs also support sensitive flags to prevent exposure of confidential information, and can be rendered in JSON for automation. Using outputs effectively allows complex infrastructure configurations to remain modular, maintainable, and composable.

C) Locals
Locals are used for internal computation within a module and cannot be accessed externally. They improve internal consistency but are not exposed outside the module. C is incorrect.

D) Providers
Providers define APIs and resource types Terraform can manage but do not return values or outputs. D is incorrect.

Outputs are essential for inter-module communication and exposing key values for external consumption, making B the correct choice.

Question 70

Which Terraform feature allows you to manage multiple copies of the same configuration for different environments with isolated state files?

A) Modules

B) Workspaces

C) Providers

D) Locals

ANSWER: B) Workspaces

EXPLANATION

Terraform supports managing infrastructure across multiple environments. Workspaces provide a way to maintain isolated state files for the same configuration, enabling separate deployments for dev, staging, production, or other contexts.

A) Modules
Modules encapsulate resources for reuse but do not manage environment-specific state isolation. A is incorrect.

B) Workspaces
This is the correct answer. Workspaces create separate state files for the same configuration, allowing isolated environments without duplicating code. Each workspace maintains its own state, ensuring that changes in one environment do not impact another. Workspaces simplify environment management, allowing users to switch contexts easily using terraform workspace select, while supporting CI/CD automation, testing, and multiple deployments. Workspaces integrate with backends for remote state storage, enabling team collaboration and consistent environment isolation. They provide an efficient mechanism for managing multiple environments using a single configuration codebase.

C) Providers
Providers define infrastructure platforms and resource types but do not manage environment-specific states. C is incorrect.

D) Locals
Locals compute intermediate values within a module but do not handle state isolation or multiple environments. D is incorrect.

Workspaces enable safe, scalable, and isolated environment management, making B the correct choice.

Question 71

Which Terraform construct allows you to define a block of reusable infrastructure code with input variables and outputs that can be called from other configurations or modules?

A) Workspaces

B) Modules

C) Providers

D) Locals

ANSWER: B) Modules

EXPLANATION

Terraform modules are one of the core features that enable the creation of reusable and maintainable infrastructure code. They encapsulate resources, variables, and outputs into logical units that can be reused across multiple configurations or projects.

A) Workspaces
Workspaces allow multiple isolated state files for the same configuration, supporting environment separation like dev, staging, and production. However, they do not encapsulate reusable blocks of resources or provide mechanisms for parameterization and outputs. Workspaces only manage state isolation and do not define reusable infrastructure. Therefore, A is incorrect.

B) Modules
This is the correct answer. Modules allow developers to package resources into reusable units that include input variables for parameterization and outputs to return information to calling configurations. Modules can be local, stored in a directory within a project, or remote, such as from a Git repository or the Terraform Registry. They are critical for promoting code reuse, consistency, and standardization across environments. Modules support nesting, enabling complex infrastructures to be built from simpler building blocks. For example, a module can define a VPC with subnets and routing rules, and then be reused across multiple environments by supplying different input variables. Modules also enhance maintainability, allowing updates to propagate automatically to all configurations using the module, reducing the risk of inconsistencies and manual errors.

C) Providers
Providers define the APIs and resources Terraform can interact with, such as AWS, Azure, or Google Cloud. They do not encapsulate resources or provide reusable infrastructure blocks with input and output parameters. C is incorrect.

D) Locals
Locals allow you to compute intermediate values within a module for internal reuse, but they do not encapsulate resources or provide reusable infrastructure blocks. D is incorrect.

Modules are essential for creating reusable, maintainable, and parameterized infrastructure code, making B the correct choice.

Question 72

Which Terraform command is used to display all resources currently tracked in the state file along with their addresses?

A) terraform show

B) terraform state list

C) terraform plan

D) terraform validate

ANSWER: B) terraform state list

EXPLANATION

Managing and understanding Terraform state is crucial for infrastructure management. The state file keeps track of all resources Terraform manages, and inspecting it is necessary for auditing, debugging, or preparing state manipulations.

A) terraform show
Terraform show provides a human-readable view of the current state, including all attributes of resources. While it displays detailed resource information, it does not provide a simple list of resource addresses suitable for scripting or state operations. A is incorrect.

B) terraform state list
This is the correct answer. Terraform state list displays all resources tracked in the state file with their full addresses, including module paths and resource names. This allows users to identify, inspect, and reference resources for operations like state manipulation, importing, or debugging. It is particularly useful in large environments where hundreds of resources exist, enabling users to locate resources precisely. State list helps with automation by providing structured outputs suitable for loops or scripts. It is also a prerequisite for commands like terraform state mv or terraform state rm, which require exact resource addresses.

C) terraform plan
Terraform plan generates a preview of changes between the configuration and the current state. It does not list all resources in the state independently. C is incorrect.

D) terraform validate
Terraform validate checks the configuration syntax and internal consistency but does not interact with the state file. D is incorrect.

Terraform state list provides a simple, precise, and essential tool for inspecting all resources tracked in state, making B the correct choice.

Question 73

Which Terraform command allows you to safely rename or move a resource within the state file without affecting the actual infrastructure?

A) terraform state mv

B) terraform state rm

C) terraform apply

D) terraform destroy

ANSWER: A) terraform state mv

EXPLANATION

Terraform provides commands to manipulate the state file independently of actual infrastructure, which is crucial for refactoring, renaming, or reorganizing resources and modules.

A) terraform state mv
This is the correct answer. Terraform state mv allows users to rename or move a resource within the state file without impacting the real-world infrastructure. This is particularly useful when reorganizing module structures, changing resource names, or moving resources between modules. By moving a resource in state, Terraform can continue tracking it correctly without recreating or deleting the actual resource. The command ensures dependencies are preserved, and future terraform apply commands recognize the resource in its new location. This provides a safe, predictable way to restructure state during refactoring or modularization.

B) terraform state rm
Terraform state rm removes a resource from the state file without deleting real infrastructure. It is used for detaching resources from Terraform management but does not rename or move them. B is incorrect.

C) terraform apply
Terraform apply executes changes to real infrastructure based on the configuration. It does not independently manipulate state for renaming or moving resources. C is incorrect.

D) terraform destroy
Terraform destroy removes resources from both the state file and the actual infrastructure. It is destructive and not intended for renaming or moving resources. D is incorrect.

Terraform state mv provides a safe and controlled method to rename or move resources within the state, making A the correct choice.

Question 74

Which Terraform command is used to create an execution plan showing the changes Terraform will make without applying them?

A) terraform plan

B) terraform apply

C) terraform validate

D) terraform init

ANSWER: A) terraform plan

EXPLANATION

Terraform plan is a key command for safely managing infrastructure changes by providing a preview of what Terraform intends to execute.

A) terraform plan
This is the correct answer. Terraform plan calculates the differences between the current state and the configuration, producing an execution plan that details resources to be created, updated, or destroyed. It allows users to review proposed changes without affecting real infrastructure, enabling safer deployments. The plan considers dependencies, resource attributes, and provider behaviors. This command is often integrated into CI/CD pipelines to automate change review and approval processes, reducing risk and ensuring predictable infrastructure management. Terraform plan is essential for auditing, debugging, and confirming that intended changes match expectations before applying them.

B) terraform apply
Terraform apply executes the changes based on configuration and state. While apply internally generates a plan, it does not provide a read-only preview. B is incorrect.

C) terraform validate
Terraform validate checks the syntax and consistency of configurations but does not generate a plan or show changes. C is incorrect.

D) terraform init
Terraform init initializes the working directory, downloads provider plugins, and configures backends. It does not generate or preview changes. D is incorrect.

Terraform plan provides a safe, non-destructive mechanism to understand intended infrastructure changes, making A the correct choice.

Question 75

Which Terraform construct allows you to compute values internally within a module for reuse, avoiding repetition and improving readability?

A) Variables

B) Locals

C) Outputs

D) Providers

ANSWER: B) Locals

EXPLANATION

Terraform provides several constructs for managing values and computation, but locals are specifically designed to compute intermediate values for reuse within a module.

A) Variables
Variables are used to provide external input to a module or configuration. They allow parameterization but do not compute derived values internally. A is incorrect.

B) Locals
This is the correct answer. Locals allow you to define intermediate values based on input variables, other locals, or resource attributes. These computed values can be reused multiple times within a module, improving maintainability, readability, and consistency. Locals help reduce code repetition and avoid errors from duplicating expressions. They cannot be accessed outside the module, promoting encapsulation and modular design. For example, a local could compute a standardized resource name that is used across several resources, ensuring consistency and simplifying changes if the naming convention evolves. Locals enhance the clarity and maintainability of Terraform code in large-scale deployments.

C) Outputs
Outputs expose values from a module to other modules or configurations but do not compute internal reusable values. C is incorrect.

D) Providers
Providers define which APIs and resources Terraform can manage but do not compute or store intermediate values. D is incorrect.

Locals are essential for efficient computation, reusability, and clarity within a module, making B the correct choice.

Question 76

Which Terraform command is used to bring existing resources into 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 command designed to enable organizations to bring pre-existing resources into Terraform’s management. This is crucial in scenarios where infrastructure has been provisioned manually or via another tool, and there is a need to transition to Infrastructure as Code.

A) terraform import
This is the correct answer. Terraform import allows you to map an existing resource in your infrastructure to a resource in your Terraform configuration, creating an entry in the state file. The resource does not need to be recreated or modified, and Terraform will recognize it as managed. Importing is particularly useful for organizations migrating legacy infrastructure into Terraform to gain benefits such as state tracking, versioning, and automation. When importing, you specify the resource type, resource name, and provider-specific ID to establish the mapping. It is essential to align the Terraform configuration with the actual state attributes, otherwise subsequent apply operations may attempt to modify the resource to match the configuration. This command bridges the gap between manually managed infrastructure and Terraform’s declarative management model. After importing, it’s common to run terraform plan to verify that the imported resource matches the configuration and ensure no unintended changes are proposed.

B) terraform plan
Terraform plan shows what actions Terraform would take to reconcile configuration with state. While plan can detect discrepancies, it does not bring resources under Terraform management or create state entries. Plan relies on already managed resources and state information. Therefore, B is incorrect.

C) terraform apply
Terraform apply executes changes to real infrastructure based on configuration and state. It does not import existing resources into state. Apply assumes resources are already tracked or defined in the configuration. C is incorrect.

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

Terraform import is vital for safely transitioning existing infrastructure into Terraform’s management, allowing organizations to take advantage of automation and state tracking without recreating resources. This makes A the correct choice.

Question 77

Which Terraform feature allows you to maintain separate state files for different environments, such as dev, staging, and production?

A) Workspaces

B) Modules

C) Providers

D) Locals

ANSWER: A) Workspaces

EXPLANATION

Managing multiple environments with Terraform requires isolation of state files to ensure that changes in one environment do not affect another. Workspaces provide this capability.

A) Workspaces
This is the correct answer. Workspaces create isolated state files for the same configuration, allowing separate deployments for different environments. Each workspace has its own state, which ensures that operations performed in dev, staging, or production do not interfere with each other. Workspaces simplify multi-environment management by avoiding the need to duplicate configurations or manually manage separate state files. They can be easily switched using terraform workspace select, and each workspace can maintain its own variables and outputs. Workspaces integrate with remote backends for collaborative scenarios, ensuring safe and consistent management across teams. By using workspaces, organizations can implement environment-specific testing and deployment workflows, reduce risks associated with accidental cross-environment changes, and streamline CI/CD pipelines.

B) Modules
Modules provide reusable blocks of infrastructure but do not manage separate state files for environments. B is incorrect.

C) Providers
Providers define which APIs and resources Terraform can manage but do not isolate state files or manage environments. C is incorrect.

D) Locals
Locals compute internal values within a module but do not handle state isolation for multiple environments. D is incorrect.

Workspaces enable efficient, safe, and scalable multi-environment management, making A the correct choice.

Question 78

Which Terraform construct allows you to dynamically retrieve information from existing infrastructure or external sources without creating new resources?

A) Variables

B) Data sources

C) Outputs

D) Locals

ANSWER: B) Data sources

EXPLANATION

Terraform provides the ability to reference existing infrastructure or external data without modifying or creating resources. This feature ensures flexibility, adaptability, and dynamic configuration.

A) Variables
Variables provide input to Terraform configurations, allowing parameterization. They do not retrieve external information or query existing infrastructure. A is incorrect.

B) Data sources
This is the correct answer. Data sources allow Terraform to fetch information from existing resources or external services, making configurations dynamic. Examples include retrieving the latest AMI ID from AWS, querying existing VPCs, or accessing metadata from external APIs. Data sources are read-only and do not modify resources. They enable modular, reusable, and adaptive configurations by allowing Terraform to react to existing infrastructure conditions or external inputs. Data sources reduce hardcoding, improve maintainability, and support dynamic deployments in multiple environments. For instance, using a data source for an AWS VPC ID allows multiple modules to consume the same existing VPC without duplicating resource definitions, ensuring consistency and minimizing the risk of errors.

C) Outputs
Outputs expose values from resources or modules to other modules or external systems but do not dynamically fetch external or existing information. C is incorrect.

D) Locals
Locals allow computation of intermediate values within a module but cannot retrieve information from external sources. D is incorrect.

Data sources are essential for creating dynamic, reusable, and adaptive Terraform configurations, making B the correct choice.

Question 79

Which Terraform command is used to safely remove resources from the state file and optionally destroy them in real infrastructure?

A) terraform destroy

B) terraform state rm

C) terraform apply

D) terraform validate

ANSWER: B) terraform state rm

EXPLANATION

Terraform allows advanced manipulation of state files to manage infrastructure effectively without inadvertently affecting resources.

A) terraform destroy
Terraform destroy deletes resources from both the state file and real infrastructure. It is destructive and not suitable for safe state-only removal. A is incorrect.

B) terraform state rm
This is the correct answer. Terraform state rm removes resources from the state file while leaving real infrastructure untouched. It is useful when decoupling Terraform from certain resources, migrating state, or reorganizing modules. After removing a resource from state, Terraform no longer manages it, and subsequent apply operations will not affect it. This allows teams to retain infrastructure while adjusting Terraform’s scope of management safely. State rm preserves dependencies, ensures integrity of remaining resources, and supports workflows where selective detachment of resources is required.

C) terraform apply
Terraform apply executes planned changes and updates real infrastructure but does not independently remove resources from state without affecting them. C is incorrect.

D) terraform validate
Terraform validate checks configuration syntax and internal consistency without interacting with resources or state. D is incorrect.

Terraform state rm provides a safe, controlled method to detach resources from Terraform management without destruction, making B the correct choice.

Question 80

Which Terraform construct allows you to define reusable logic to compute intermediate values based on input variables or resource attributes within a module?

A) Variables

B) Locals

C) Outputs

D) Providers

ANSWER: B) Locals

EXPLANATION

Terraform provides constructs for managing data and computation efficiently. Locals allow for intermediate value computation, centralizing logic, and reducing redundancy.

A) Variables
Variables provide external input to a module or configuration, allowing parameterization but do not compute internal values. A is incorrect.

B) Locals
This is the correct answer. Locals allow you to define intermediate values derived from input variables, other locals, or resource attributes. These values can be reused multiple times within a module, enhancing maintainability, readability, and consistency. Locals reduce duplication of expressions, simplify refactoring, and improve code clarity. For example, a local can compute standardized resource names, combined IP addresses, or calculated attributes used across multiple resources. Using locals promotes modularity, ensures consistency, and reduces the risk of errors in large-scale infrastructure deployments. Locals cannot be accessed outside the module, enforcing encapsulation and protecting internal computations while providing reusable logic.

C) Outputs
Outputs expose values from a module to other modules or external configurations but do not compute internal reusable values. C is incorrect.

D) Providers
Providers define APIs and resource types Terraform can manage but do not compute values. D is incorrect.

Locals are critical for maintaining efficient, reusable, and modular Terraform code, making B the correct choice.

img