HashiCorp Terraform Associate 003 Exam Dumps and Practice Test Questions Set 5 Q 81- 100
Visit here for our full HashiCorp Terraform Associate 003 exam dumps and practice test questions.
Question 81
Which Terraform command is used to initialize a working directory, download provider plugins, and configure the backend?
A) terraform plan
B) terraform apply
C) terraform init
D) terraform validate
ANSWER: C) terraform init
EXPLANATION
Terraform init is a fundamental command in Terraform workflows and serves as the initial step when starting a new project or working directory. Its purpose is to set up the environment so that Terraform can interact with infrastructure providers and maintain state properly.
A) terraform plan
Terraform plan generates a preview of changes by comparing the current state of resources with the desired configuration. It does not initialize the working directory, download providers, or configure backends. Plan assumes that Terraform init has already been executed and that the working directory is properly initialized. Without initialization, plan cannot function correctly because provider plugins may be missing, modules may not be downloaded, and backend configuration may not be recognized. Plan is dependent on init for setup, so it is not the correct answer.
B) terraform apply
Terraform apply executes changes to real infrastructure to align it with the desired configuration. While apply interacts with providers and requires an initialized working directory, it does not perform the initialization process itself. Apply assumes that Terraform init has already been run and that all provider plugins are available. It is primarily used to apply the execution plan generated by terraform plan and is dependent on initialization to function properly. B is incorrect.
C) terraform init
This is the correct answer. Terraform init performs several critical tasks: it initializes the working directory by creating required directories, downloads the necessary provider plugins specified in the configuration, and configures the backend where the state file will be stored. Backend configuration is essential for remote state management, collaboration, and locking. During initialization, Terraform also downloads modules referenced in the configuration, ensuring that all required dependencies are available before planning or applying changes. Terraform init can be enhanced with flags such as -backend-config to override backend settings or -upgrade to upgrade provider plugins to the latest acceptable version. By establishing a fully prepared working environment, terraform init ensures that subsequent terraform plan or terraform apply commands can execute successfully. This command is mandatory for any new Terraform project and is considered the foundation of the Terraform workflow.
D) terraform validate
Terraform validate checks configuration files for syntax correctness and internal consistency. It does not initialize the working directory, download providers, or configure the backend. Validate is used after init to confirm that configurations are valid before applying changes. It ensures that variables, resources, and modules are correctly defined but does not perform any setup tasks required for Terraform to operate. D is incorrect.
Terraform init is the foundational command that prepares the environment for Terraform operations, making it indispensable for initializing directories, managing providers, downloading modules, and configuring backends. This makes C the correct choice.
Question 82
Which Terraform feature allows you to retrieve data from existing infrastructure or external sources without creating new resources?
A) Variables
B) Data sources
C) Locals
D) Outputs
ANSWER: B) Data sources
EXPLANATION
Data sources are a core feature of Terraform that enable dynamic retrieval of information from existing resources or external services. This feature ensures that infrastructure configurations can adapt to current environments without duplicating or creating unnecessary resources.
A) Variables
Variables provide input to Terraform configurations, allowing parameterization. They are user-defined and supplied either via command-line, environment, or files. Variables do not fetch information from external infrastructure or compute values dynamically from existing resources. While variables can influence how data sources are used, they themselves are not designed to retrieve external information. Therefore, A is incorrect.
B) Data sources
This is the correct answer. Data sources allow Terraform configurations to query existing resources or external data sources for information. Examples include fetching the latest AMI ID from AWS, querying an existing VPC, or retrieving metadata from external APIs. Data sources are read-only and do not modify or create resources, providing a safe way to integrate dynamic infrastructure elements. They are widely used in modular designs where modules need to reference existing infrastructure without hardcoding identifiers. Data sources improve maintainability, reduce errors, and allow Terraform to react to changes in existing environments. By using data sources, multiple modules can consume shared infrastructure information consistently, enabling adaptive and scalable infrastructure design. For example, retrieving a list of subnets via a data source allows multiple instances to be deployed dynamically across those subnets without duplicating configuration. Data sources also integrate well with outputs and locals, allowing for complex computations and dynamic infrastructure assembly.
C) Locals
Locals compute intermediate values within a module based on variables, resource attributes, or other locals. While useful for internal computation, locals cannot query existing infrastructure or external systems. C is incorrect.
D) Outputs
Outputs expose values from resources or modules to other configurations or external systems. They do not retrieve external information. Outputs are primarily used for communication and reference between modules rather than dynamic data retrieval. D is incorrect.
Data sources provide a mechanism for dynamic and adaptive configuration by querying existing infrastructure, ensuring flexibility, reducing duplication, and supporting modular architecture, making B the correct choice.
Question 83
Which Terraform command is used to review the exact changes Terraform will make to infrastructure before applying them?
A) terraform apply
B) terraform plan
C) terraform validate
D) terraform init
ANSWER: B) terraform plan
EXPLANATION
Terraform plan is a critical command for safely managing infrastructure changes. It previews changes that Terraform will make to achieve the desired state without actually applying them, offering transparency and control over modifications.
A) terraform apply
Terraform apply executes changes to real infrastructure. While apply internally generates a plan, it does not provide a separate preview before execution. Users seeking a read-only review of planned changes would use terraform plan instead. A is incorrect.
B) terraform plan
This is the correct answer. Terraform plan generates an execution plan detailing resources to be created, updated, or destroyed based on the differences between the current state and configuration. The plan accounts for dependencies, provider behavior, and attribute changes. It is a non-destructive command, allowing teams to review proposed modifications, detect potential errors, and ensure that changes align with expectations. Terraform plan is integral to CI/CD pipelines, enabling automated reviews, approvals, and audits before applying changes. It provides detailed insights, including resource addresses, proposed actions, and specific attribute modifications. This functionality is essential for collaboration and safe infrastructure management, preventing accidental misconfigurations or destructive operations.
C) terraform validate
Terraform validate checks configuration syntax and internal consistency but does not show planned changes or preview resource modifications. C is incorrect.
D) terraform init
Terraform init initializes a working directory, downloads provider plugins, and configures the backend. It does not generate an execution plan or show potential changes. D is incorrect.
Terraform plan ensures controlled, predictable, and auditable infrastructure management, providing a comprehensive preview before any changes occur, making B the correct choice.
Question 84
Which Terraform construct allows you to expose values from a module to other modules or configurations?
A) Variables
B) Outputs
C) Locals
D) Providers
ANSWER: B) Outputs
EXPLANATION
Outputs are a fundamental construct in Terraform that allows modules to communicate with other modules or configurations by exposing key computed values.
A) Variables
Variables provide external input to modules or configurations, allowing parameterization. They do not expose computed or derived values from a module for external use. A is incorrect.
B) Outputs
This is the correct answer. Outputs define values that a module can return to calling configurations, other modules, or external systems. Examples include resource IDs, IP addresses, or computed attributes. Outputs are essential for modular architecture, enabling decoupled modules to interact by sharing necessary information. They can also be marked as sensitive to prevent the exposure of confidential data and rendered in JSON format for automation purposes. Outputs allow developers to build complex infrastructure systems composed of multiple modules while maintaining modularity, clarity, and reusability. By using outputs, modules can provide dynamic information without requiring external hardcoding or duplication, supporting best practices in Infrastructure as Code.
C) Locals
Locals compute intermediate values for internal reuse within a module. They are not accessible outside the module and therefore cannot expose values to other configurations. C is incorrect.
D) Providers
Providers define APIs and resources that Terraform can manage but do not expose module-specific values. D is incorrect.
Outputs facilitate inter-module communication, maintain modularity, and enable dynamic configurations, making B the correct choice.
Question 85
Which Terraform feature allows you to parameterize configurations and pass values into modules or resources?
A) Locals
B) Variables
C) Outputs
D) Providers
ANSWER: B) Variables
EXPLANATION
Variables are a cornerstone of Terraform configurations, enabling parameterization and flexibility. They allow users to supply dynamic inputs to modules or resources, supporting reusable and environment-specific configurations.
A) Locals
Locals compute intermediate values internally within a module. They cannot accept external input or parameterize module behavior. A is incorrect.
B) Variables
This is the correct answer. Variables allow Terraform configurations to accept external input from files, environment variables, or command-line arguments. They enable dynamic configuration, making modules reusable across environments. Variables can have default values, type constraints, and validation rules to enforce correctness. They provide flexibility for multi-environment deployments, allowing the same module to be applied in dev, staging, or production by simply changing variable values. Variables support complex types such as lists, maps, and objects, enabling sophisticated configurations. Proper use of variables reduces code duplication, improves maintainability, and enhances modularity. They are central to creating reusable, parameterized Terraform modules and supporting Infrastructure as Code best practices.
C) Outputs
Outputs expose values from modules but do not provide a mechanism to pass external input into modules. C is incorrect.
D) Providers
Providers define APIs and resource types Terraform can manage but do not parameterize modules or accept dynamic input. D is incorrect.
Variables are essential for creating flexible, reusable, and environment-specific Terraform configurations, making B the correct choice.
Question 86
Which Terraform feature allows you to organize and group infrastructure resources into reusable, composable units?
A) Providers
B) Modules
C) Workspaces
D) Locals
ANSWER: B) Modules
EXPLANATION
Modules are one of the foundational concepts in Terraform that enable organizations to structure infrastructure in a modular, reusable, and maintainable way. By grouping related resources into logical units, modules provide the ability to create scalable and consistent infrastructure patterns across multiple projects and environments.
A) Providers
Providers are responsible for defining the API and types of resources Terraform can interact with, such as AWS, Azure, or Google Cloud. Providers are essential for enabling Terraform to communicate with cloud platforms and manage resources, but they do not provide a mechanism for grouping resources into reusable units. Providers set the context for resource creation and management but cannot encapsulate infrastructure logic or make configurations reusable. A is incorrect.
B) Modules
This is the correct answer. Modules allow you to define a block of infrastructure resources along with input variables and outputs that can be invoked from other configurations. Modules can be local, defined within the same project directory, or remote, sourced from a Git repository, Terraform Registry, or other remote locations. Modules encapsulate configuration logic, making infrastructure easier to maintain, test, and extend. They promote best practices such as DRY (Don’t Repeat Yourself) by centralizing configuration patterns that can be reused across environments like dev, staging, and production. Nested modules allow developers to create complex infrastructures by composing smaller, focused modules into larger, cohesive solutions. For example, a module might define a VPC along with subnets, route tables, and security groups. By calling this module multiple times with different input variables, Terraform can provision multiple isolated VPCs without duplicating the underlying configuration. Modules also facilitate collaboration, as teams can develop standardized modules that enforce organization-wide best practices. Proper use of modules leads to maintainable, scalable, and standardized Terraform deployments.
C) Workspaces
Workspaces enable the management of multiple isolated state files for the same configuration, allowing environment separation like dev, staging, and production. While workspaces are critical for managing environments, they do not provide the capability to group resources into reusable infrastructure units. Workspaces focus on state isolation rather than modularity or composability of code. C is incorrect.
D) Locals
Locals are used to compute intermediate values within a module and can be reused internally to avoid code repetition. However, locals cannot group resources or encapsulate infrastructure logic. They are intended for internal calculations and do not support modularity or resource reusability across configurations. D is incorrect.
Modules are essential for creating reusable, composable, and maintainable infrastructure structures, allowing for standardized deployments and scalable architecture, making B the correct choice.
Question 87
Which Terraform command is used to apply the desired state to the real infrastructure based on the configuration?
A) terraform plan
B) terraform apply
C) terraform validate
D) terraform init
ANSWER: B) terraform apply
EXPLANATION
Terraform apply is the primary command used to make real changes to infrastructure according to the configuration and state. It is responsible for provisioning, modifying, or destroying resources to align the actual infrastructure with the desired state.
A) terraform plan
Terraform plan calculates the differences between the current state and the desired configuration and produces an execution plan. It does not apply changes to the real infrastructure. Plan is used to preview changes and ensure they meet expectations before applying them. Therefore, A is incorrect.
B) terraform apply
This is the correct answer. Terraform apply reads the configuration files, compares them with the current state of the infrastructure, and executes the necessary actions to create, update, or destroy resources. It applies the execution plan generated either automatically or manually from terraform plan. Apply handles dependencies between resources to ensure correct ordering and minimizes the risk of conflicts or errors. It also updates the state file to reflect the current infrastructure accurately after execution. Terraform apply can be run interactively, requiring user confirmation, or non-interactively using the -auto-approve flag for automated workflows. This command is central to Infrastructure as Code practices, enabling reproducible, consistent, and automated deployments across multiple environments. Apply also integrates with backends to ensure state consistency, locking, and collaborative access in team environments. It allows organizations to manage infrastructure as code safely, ensuring that the actual infrastructure mirrors the desired declarative configuration.
C) terraform validate
Terraform validate checks the configuration syntax and internal consistency but does not modify infrastructure or apply any changes. Validate ensures correctness but does not execute actions. C is incorrect.
D) terraform init
Terraform init initializes the working directory, downloads required provider plugins, and sets up the backend configuration. While init prepares the environment for terraform apply, it does not execute changes itself. D is incorrect.
Terraform apply is essential for executing changes, provisioning infrastructure, and synchronizing the real-world environment with configuration, making B the correct choice.
Question 88
Which Terraform feature allows you to store and manage infrastructure state remotely, enabling team collaboration and locking?
A) Modules
B) Providers
C) Backends
D) Locals
ANSWER: C) Backends
EXPLANATION
Terraform backends are critical for managing state files and ensuring collaborative workflows in multi-user environments. By storing state remotely, backends prevent conflicts, provide state locking, and enable secure access.
A) Modules
Modules are reusable blocks of infrastructure code. While modules improve maintainability and standardization, they do not manage state or facilitate remote storage. A is incorrect.
B) Providers
Providers define the resources and APIs Terraform interacts with but do not store or manage state. Providers facilitate resource management but do not enable collaboration or locking. B is incorrect.
C) Backends
This is the correct answer. Backends determine where Terraform stores its state file, which is essential for collaboration and team environments. Remote backends, such as AWS S3, Azure Blob Storage, Google Cloud Storage, or Terraform Cloud, allow multiple users to work on the same infrastructure safely. Backends provide state locking to prevent simultaneous modifications that could corrupt the state, maintain versioning for auditing, and support encryption for security. By centralizing the state, teams can coordinate changes, perform plan and apply operations reliably, and avoid accidental conflicts. Backends also enable advanced workflows like CI/CD integration, remote execution, and automated environment provisioning. Without backends, managing infrastructure collaboratively becomes error-prone and risky. Using backends is a best practice for production environments, ensuring consistency, traceability, and safe collaboration across teams.
D) Locals
Locals are used for internal computation of intermediate values within a module. They do not manage state or facilitate remote storage or collaboration. D is incorrect.
Backends are fundamental for secure, collaborative, and reliable Terraform workflows, enabling teams to manage infrastructure state safely, making C the correct choice.
Question 89
Which Terraform construct allows you to compute intermediate values within a module to improve readability, maintainability, and reduce repetition?
A) Variables
B) Locals
C) Outputs
D) Providers
ANSWER: B) Locals
EXPLANATION
Terraform locals are an essential construct for simplifying configurations by allowing internal computations that can be reused across multiple resources within a module.
A) Variables
Variables are used to pass external input into modules or resources, providing parameterization. They do not compute intermediate values internally and therefore cannot reduce repetition or improve internal readability. A is incorrect.
B) Locals
This is the correct answer. Locals allow developers to define computed values based on input variables, resource attributes, or other locals. These values can be referenced multiple times within a module, reducing code duplication, improving readability, and ensuring consistency. For example, a local can compute a standardized naming convention for resources, a combination of subnets, or a derived configuration parameter. Using locals ensures that changes need only be updated in one place, rather than across multiple resources, enhancing maintainability. Locals also encapsulate logic within the module, supporting modularity and avoiding side effects outside the module. They are invaluable for large-scale infrastructure, where complex configurations require intermediate calculations to manage dependencies, naming conventions, and other attributes consistently. Locals also improve collaboration by providing a clear place for computed values, making Terraform code easier to understand for new team members.
C) Outputs
Outputs expose values from modules to external configurations or other modules but do not compute intermediate values internally. C is incorrect.
D) Providers
Providers define APIs and resource types but cannot compute values. They are necessary for resource management but do not provide intermediate computation functionality. D is incorrect.
Locals improve maintainability, readability, and reusability by allowing internal computations within a module, making B the correct choice.
Question 90
Which Terraform command allows you to remove a resource from the state file without destroying the actual infrastructure?
A) terraform destroy
B) terraform state rm
C) terraform apply
D) terraform plan
ANSWER: B) terraform state rm
EXPLANATION
Terraform state management commands provide advanced control over resources without impacting the actual infrastructure. State removal is a key feature for decoupling Terraform from specific resources safely.
A) terraform destroy
Terraform destroy deletes resources from both the state file and the actual infrastructure. It is destructive and cannot be used to remove a resource from state while preserving it. A is incorrect.
B) terraform state rm
This is the correct answer. Terraform state rm allows users to remove a resource from the Terraform state file while leaving the underlying resource intact. This is useful in scenarios such as migrating resource management to another tool, reorganizing modules, or detaching resources from Terraform’s scope. After removing a resource from the state, Terraform no longer tracks it, and future apply operations will not affect it. This enables safe state manipulation and prevents accidental deletion. State rm ensures that dependencies remain intact for other resources in the state file and supports advanced workflows like modularization, state file consolidation, and environment-specific resource management.
C) terraform apply
Terraform apply executes changes to align infrastructure with configuration. It does not independently remove resources from state without affecting them. C is incorrect.
D) terraform plan
Terraform plan previews changes between configuration and state but does not remove resources from the state file. 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 91
Which Terraform command is used to check the syntax and internal consistency of configuration files without creating or modifying infrastructure?
A) terraform plan
B) terraform validate
C) terraform apply
D) terraform init
ANSWER: B) terraform validate
EXPLANATION
Terraform validate is an essential command for ensuring that configuration files are syntactically correct and logically consistent before applying changes to real infrastructure. This validation process reduces the risk of errors and misconfigurations.
A) terraform plan
Terraform plan is used to preview changes between the current state and the desired configuration. While it can highlight discrepancies and show proposed actions, it does not check the syntax or logical correctness of the configuration files independently. Plan assumes that the configuration is valid and executable. Therefore, A is incorrect.
B) terraform validate
This is the correct answer. Terraform validate performs a comprehensive check of configuration files to ensure they are syntactically correct, all required arguments are provided, and resources are logically consistent. It verifies that variables, resources, and modules are defined properly, ensuring that the configuration can be processed successfully by Terraform. Validate is particularly useful in continuous integration pipelines, where automated checks of configuration files are required before deployment. It helps detect issues such as missing provider configurations, invalid references, or improper module usage early in the workflow, preventing costly errors during apply. By running validate, developers can ensure that configurations follow best practices, maintain consistency, and reduce the risk of runtime failures. Unlike plan or apply, validate does not interact with state files or remote infrastructure. It is a safe, non-destructive way to confirm that Terraform configurations are ready for execution, making it a critical step in quality assurance and automated testing workflows.
C) terraform apply
Terraform apply executes the planned changes to the actual infrastructure. While apply implicitly performs validation as part of the execution process, its primary purpose is to create, update, or destroy resources. It is not intended solely for syntax or logical checking. C is incorrect.
D) terraform init
Terraform init initializes a working directory by downloading provider plugins, setting up backends, and preparing modules. It does not verify configuration syntax or internal consistency. D is incorrect.
Terraform validate ensures safe, predictable, and error-free infrastructure deployment by checking configurations for correctness, making B the correct choice.
Question 92
Which Terraform command is used to list all resources currently tracked in the state file?
A) terraform show
B) terraform state list
C) terraform plan
D) terraform apply
ANSWER: B) terraform state list
EXPLANATION
Understanding the current state of resources in Terraform is crucial for auditing, debugging, and performing state manipulations. Terraform state list provides a simple, structured way to inspect all tracked resources.
A) terraform show
Terraform show provides a detailed human-readable view of the current state, including resource attributes and values. While it offers comprehensive information, it does not provide a concise list of resource addresses suitable for scripting or quick reference. A is incorrect.
B) terraform state list
This is the correct answer. Terraform state list outputs all resources tracked in the state file with their full addresses, including module paths and resource names. This command is valuable for identifying and referencing resources for state operations such as state mv or state rm. In large environments, state list simplifies resource identification and supports automation by providing a structured resource list. It ensures accuracy in managing resources, especially when multiple modules or nested structures exist. Using state list, teams can efficiently audit state, verify resource presence, and plan modifications without inspecting every attribute manually. This command is essential for advanced state management and operational control over Terraform-managed infrastructure.
C) terraform plan
Terraform plan generates a preview of changes between the configuration and the current state but does not provide a simple list of resources in state. C is incorrect.
D) terraform apply
Terraform apply executes changes to real infrastructure. It does not provide a listing of all tracked resources in state. D is incorrect.
Terraform state list enables efficient state management, auditing, and resource identification, making B the correct choice.
Question 93
Which Terraform construct allows you to define input values for modules or resources to make them reusable and configurable?
A) Locals
B) Variables
C) Outputs
D) Providers
ANSWER: B) Variables
EXPLANATION
Variables are a fundamental construct in Terraform that allow configurations and modules to accept dynamic inputs, enabling flexibility, reusability, and environment-specific deployments.
A) Locals
Locals are used to compute intermediate values internally within a module. They do not accept external inputs or allow modules to be parameterized. A is incorrect.
B) Variables
This is the correct answer. Variables allow users to provide input values from command-line arguments, environment variables, or variable files. They enable modules and resources to be reusable across environments by altering behavior or configuration based on variable values. Variables support default values, type constraints, and validation rules, ensuring proper usage and reducing errors. For example, a module defining an EC2 instance could use variables to set instance type, region, and AMI ID. By changing the variables, the same module can be deployed in dev, staging, or production without modifying the underlying configuration. Variables also support complex types like lists, maps, and objects, enabling sophisticated and flexible infrastructure definitions. They are essential for Infrastructure as Code best practices, allowing teams to build maintainable, scalable, and modular infrastructure. Variables improve maintainability, reduce duplication, and enable environment-specific customization, ensuring consistency across deployments.
C) Outputs
Outputs expose values from modules to other modules or external systems but do not allow input configuration. They are used for sharing data rather than parameterizing modules. C is incorrect.
D) Providers
Providers define the APIs and resources that Terraform can manage but do not provide parameterization capabilities for modules or resources. D is incorrect.
Variables provide dynamic input, flexibility, and reusability for Terraform configurations, making B the correct choice.
Question 94
Which Terraform feature 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 state manipulation commands are critical for advanced workflows such as refactoring modules, renaming resources, or moving resources between modules without recreating them.
A) terraform state mv
This is the correct answer. Terraform state mv allows users to rename or relocate a resource within the state file safely. This operation updates the resource’s address in the state without modifying the actual infrastructure. It is particularly useful during refactoring, modularization, or restructuring of configurations. Terraform state mv ensures that dependencies and references are preserved, preventing accidental destruction or duplication of resources. For example, if a resource is moved into a module, state mv can update the state file so Terraform recognizes the new module path. This command supports safe, predictable, and controlled refactoring of Terraform-managed infrastructure while maintaining consistency and avoiding disruptions.
B) terraform state rm
Terraform state rm removes a resource from the state file entirely, effectively detaching it from Terraform management. It does not rename or move resources. B is incorrect.
C) terraform apply
Terraform apply executes changes to real infrastructure and updates the state file accordingly. It does not provide a mechanism to rename or move resources safely within state without affecting infrastructure. C is incorrect.
D) terraform destroy
Terraform destroy deletes resources from both the state file and actual infrastructure. It is destructive and not suitable for renaming or moving resources. D is incorrect.
Terraform state mv provides a controlled and non-destructive way to reorganize resources within the state, making A the correct choice.
Question 95
Which Terraform command is used to safely remove resources from the state file while leaving the actual infrastructure intact?
A) terraform destroy
B) terraform state rm
C) terraform apply
D) terraform plan
ANSWER: B) terraform state rm
EXPLANATION
Terraform provides advanced state management capabilities that allow resources to be detached from Terraform tracking without destroying the underlying infrastructure. This is essential for scenarios like migration, reorganization, or selective resource management.
A) terraform destroy
Terraform destroy removes resources from both the state and the actual infrastructure. It is destructive and cannot be used to detach resources safely. A is incorrect.
B) terraform state rm
This is the correct answer. Terraform state rm removes a resource from the state file, meaning Terraform no longer manages it. However, the resource in the real infrastructure remains unaffected. This command is valuable when migrating management responsibilities, reorganizing modules, or selectively removing resources from Terraform’s scope. After execution, subsequent apply operations will not affect the removed resource. State rm maintains the integrity of other resources, respects dependencies, and ensures safe decoupling. It is a non-destructive and controlled method to manipulate the state file, supporting advanced workflows in collaborative environments. Using terraform state rm reduces the risk of accidental deletion and allows administrators to maintain existing infrastructure while transitioning to new management strategies.
C) terraform apply
Terraform apply aligns the infrastructure with the configuration but cannot independently remove resources from state without affecting the infrastructure. C is incorrect.
D) terraform plan
Terraform plan previews changes between configuration and state but does not remove resources from state. D is incorrect.
Terraform state rm ensures safe detachment of resources from Terraform management without destruction, making B the correct choice.
Question 96
Which Terraform construct allows you to encapsulate a group of resources and reuse them across multiple configurations or environments?
A) Providers
B) Modules
C) Workspaces
D) Locals
ANSWER: B) Modules
EXPLANATION
Modules are a foundational feature in Terraform that allow users to encapsulate multiple resources and reuse them across different configurations or environments. This feature promotes modularity, reduces duplication, and ensures standardized infrastructure management.
A) Providers
Providers define the APIs and resource types Terraform can manage, such as AWS, Azure, or Google Cloud. They are essential for connecting Terraform to infrastructure platforms but do not group resources into reusable units or encapsulate infrastructure logic. Providers only specify the resource type and API endpoints, without offering modularity or reusability. A is incorrect.
B) Modules
This is the correct answer. Modules allow a set of resources, along with input variables and outputs, to be packaged into a reusable block. Modules can be local, residing in the same directory, or remote, sourced from Terraform Registry, Git repositories, or other locations. By using modules, teams can standardize deployment patterns, enforce best practices, and improve maintainability. Modules support input variables for customization, outputs for exposing important values, and nested modules for creating complex infrastructure hierarchies. For example, a module can define a network environment with VPCs, subnets, and security groups. This module can then be reused across dev, staging, and production environments simply by supplying different variable values, avoiding duplication and ensuring consistency. Modules also facilitate team collaboration by allowing centralized updates that propagate to all dependent configurations. This approach reduces errors, accelerates deployment, and improves operational efficiency. Modules are essential in scalable Terraform deployments, providing encapsulation, reusability, and modular architecture.
C) Workspaces
Workspaces enable multiple isolated state files for the same configuration, facilitating environment separation. However, workspaces do not encapsulate resources or create reusable units; they focus on state management and environment isolation. C is incorrect.
D) Locals
Locals allow internal computation of intermediate values within a module to avoid repetition and improve maintainability. While helpful for internal logic, locals do not group resources into reusable units. D is incorrect.
Modules are crucial for building reusable, maintainable, and scalable Terraform infrastructure, making B the correct choice.
Question 97
Which Terraform command is used to initialize a working directory, install provider plugins, and configure the backend?
A) terraform plan
B) terraform apply
C) terraform init
D) terraform validate
ANSWER: C) terraform init
EXPLANATION
Terraform init is the first command executed in a new working directory and is essential for preparing the environment to manage infrastructure effectively.
A) terraform plan
Terraform plan generates an execution plan by comparing the current state with the desired configuration but does not initialize the working directory or install providers. Plan depends on init having been executed first. A is incorrect.
B) terraform apply
Terraform apply executes changes in real infrastructure but assumes that the working directory has already been initialized. It does not install provider plugins or configure the backend on its own. B is incorrect.
C) terraform init
This is the correct answer. Terraform init performs several critical tasks: it initializes the working directory, installs required provider plugins, downloads modules, and configures the backend where the state file will be stored. Backend configuration is essential for remote state storage, locking, and collaboration. Terraform init can also upgrade providers or override backend configurations with flags. Running terraform init ensures that all dependencies and configurations are ready for subsequent terraform plan or terraform apply commands. Without proper initialization, Terraform commands may fail due to missing plugins, unconfigured backends, or unresolved modules. Init is thus a prerequisite for almost all Terraform operations, ensuring a consistent, reliable environment for managing infrastructure.
D) terraform validate
Terraform validate checks the configuration for syntax and logical consistency but does not initialize the working directory or download provider plugins. D is incorrect.
Terraform init prepares the working environment, installs providers, and configures the backend, making C the correct choice.
Question 98
Which Terraform feature allows you to isolate multiple environments, such as dev, staging, and production, while using the same configuration?
A) Workspaces
B) Modules
C) Providers
D) Locals
ANSWER: A) Workspaces
EXPLANATION
Workspaces in Terraform are used to manage multiple isolated instances of state for the same configuration. This allows teams to deploy the same infrastructure configuration to different environments safely.
A) Workspaces
This is the correct answer. Workspaces create isolated state files for the same configuration, ensuring that resources in dev, staging, and production do not interfere with each other. Each workspace maintains its own state, variables, and outputs, providing environment separation. Workspaces simplify environment management by avoiding the need to duplicate configurations for each environment. They also integrate with remote backends, allowing multiple team members to collaborate safely without state conflicts. Switching between environments is as simple as using terraform workspace select, and changes applied in one workspace do not affect others. Workspaces are critical for maintaining separation between testing and production environments, enabling safer deployment pipelines and reducing the risk of accidental cross-environment changes.
B) Modules
Modules encapsulate resources for reuse but do not provide isolated state management for multiple environments. B is incorrect.
C) Providers
Providers define APIs and resources Terraform can manage but do not handle environment isolation or state separation. C is incorrect.
D) Locals
Locals compute intermediate values within a module for readability and maintainability but do not manage environment isolation. D is incorrect.
Workspaces ensure safe, isolated environment management while using a single configuration, making A the correct choice.
Question 99
Which Terraform construct is used to expose values from a module to other modules or configurations?
A) Variables
B) Outputs
C) Locals
D) Providers
ANSWER: B) Outputs
EXPLANATION
Outputs are a key Terraform construct that allows modules to communicate with other modules or external systems by exposing important values.
A) Variables
Variables are used to pass input into modules or configurations. They do not expose internal values to other modules or external systems. A is incorrect.
B) Outputs
This is the correct answer. Outputs provide a mechanism for modules to return values such as resource IDs, IP addresses, or other computed attributes. Outputs can be used by calling modules, other configurations, or automation pipelines. They can also be marked as sensitive to prevent accidental exposure of confidential information. Outputs improve modularity by enabling decoupled communication between modules. For example, a networking module can output the VPC ID and subnet IDs, which can then be referenced by compute or database modules. Outputs support automation, testing, and modular design by providing reusable and dynamic values without duplicating configurations. Using outputs ensures that modules remain self-contained yet interoperable, enhancing maintainability and scalability.
C) Locals
Locals compute intermediate values within a module for internal reuse but cannot expose values externally. C is incorrect.
D) Providers
Providers define APIs and resources that Terraform can manage but do not expose module values. D is incorrect.
Outputs enable modules to share key information with other modules or configurations, making B the correct choice.
Question 100
Which Terraform command is used to remove a resource from the state file without destroying the actual resource in the infrastructure?
A) terraform destroy
B) terraform state rm
C) terraform apply
D) terraform plan
ANSWER: B) terraform state rm
EXPLANATION
Terraform state manipulation commands are critical for advanced state management, allowing safe detachment of resources from Terraform tracking.
A) terraform destroy
Terraform destroy deletes resources from both the state file and the actual infrastructure. It is destructive and cannot be used to remove resources safely without affecting the infrastructure. A is incorrect.
B) terraform state rm
This is the correct answer. Terraform state rm removes a resource from the Terraform state file while leaving the actual resource intact. This is useful for scenarios such as migrating resource management, reorganizing modules, or detaching certain resources from Terraform control. After removal, Terraform no longer tracks the resource, and subsequent apply operations will not affect it. State rm ensures that dependencies and other resources remain unaffected. This command allows administrators to manipulate the state safely, maintain infrastructure consistency, and support advanced workflows like refactoring or selective resource management. It is a non-destructive and controlled way to modify Terraform state while preserving the underlying infrastructure.
C) terraform apply
Terraform apply executes changes to align infrastructure with the configuration. It does not independently remove resources from the state without affecting them. C is incorrect.
D) terraform plan
Terraform plan previews changes but does not modify the state or remove resources. D is incorrect.
Terraform state rm provides a safe method to remove resources from state without destruction, making B the correct choice.
Popular posts
Recent Posts
