TERRAFORM-004 Practice Questions: Terraform Fundamentals Domain
Test your TERRAFORM-004 knowledge with 10 practice questions from the Terraform Fundamentals domain. Includes detailed explanations and answers.
TERRAFORM-004 Practice Questions
Master the Terraform Fundamentals Domain
Test your knowledge in the Terraform Fundamentals domain with these 10 practice questions. Each question is designed to help you prepare for the TERRAFORM-004 certification exam with detailed explanations to reinforce your learning.
Question 1
Your team uses a remote backend that supports state locking. A teammate reports that `terraform apply` is hanging longer than usual. You suspect another run is holding the lock. You want to safely continue your work without corrupting state. Which action is most appropriate?
Show Answer & Explanation
Correct Answer: C
When using a backend that supports state locking, only one operation can modify state at a time. If `terraform apply` is hanging, another run likely holds the lock. The correct approach is to coordinate with the person or system holding the lock (C), wait for that run to finish or be canceled, and then rerun your command. Forcing recreation with `-replace=*` (A) does not bypass locking and is dangerous. Manually editing remote state (B) risks corruption and is not supported. Switching to local state (D) to avoid locking can lead to divergent or corrupted state and should not be used as a workaround.
Question 2
Your team uses a remote backend with state locking supported (e.g., Terraform Cloud or an S3 backend with DynamoDB locking). Two engineers accidentally run `terraform apply` at the same time in the same workspace. What is the expected behavior regarding state and concurrency?
Show Answer & Explanation
Correct Answer: B
When using a backend that supports locking, Terraform acquires a state lock at the start of operations like `apply` and `plan` (with refresh). The first operation obtains the lock; subsequent operations either block until the lock is released or fail with a locking error, depending on the backend and CLI options. A: Terraform does not merge concurrent state updates. C: Conflicts are prevented by locking, not detected afterward. D: Terraform does not automatically create or switch workspaces to avoid conflicts.
Question 3
Your team is migrating a manually created Google Cloud Storage bucket into Terraform management. The bucket already exists in production. You have written a matching `google_storage_bucket` resource in main.tf. You want to bring this existing bucket under Terraform control with minimal risk and clear tracking in configuration. Which approach is most appropriate for Terraform 1.x?
Show Answer & Explanation
Correct Answer: B
Terraform 1.x supports declarative imports via `import` blocks. B: Correct. An `import` block in configuration documents the mapping between the existing bucket and the Terraform resource; `terraform apply` will perform the import and then manage the bucket. A: The `terraform import` CLI works but is more imperative and less self-documenting than `import` blocks; the exam emphasizes the newer workflow. C: Terraform does not automatically adopt existing resources; without import, it will try to create another bucket or fail. D: Deleting a production bucket is risky and unnecessary when import is available.
Question 4
You manage an existing AWS security group created outside Terraform. You now added this resource to your Terraform configuration and want Terraform to manage it without recreating it. The security group ID is `sg-1234567890abcdef0`. Which approach aligns with Terraform 1.x recommended workflows?
Show Answer & Explanation
Correct Answer: B
To bring an existing resource under Terraform management, you must both define the resource in configuration and associate it with the real infrastructure object. In Terraform 1.x, this is done with `terraform import` (or import blocks). B correctly imports the existing security group into the state for the resource `aws_security_group.web`. A: Terraform will attempt to create a new security group, not adopt the existing one. C: Data sources are read-only and cannot be imported. D: `prevent_destroy` only affects destroy behavior; it does not link to existing resources.
Question 5
You are writing a reusable module that creates an AWS EC2 instance. You want to ensure callers pass a valid instance type from a small allowed list. If they pass anything else, `terraform plan` should fail with a clear message. Which configuration in `variables.tf` best implements this requirement?
Show Answer & Explanation
Correct Answer: B
Terraform 1.x supports variable validation blocks to enforce custom conditions. B uses `validation` with `contains()` to restrict `instance_type` to an explicit allowlist and provides a clear error message, causing `terraform plan` (and `apply`) to fail if the condition is not met. A: Provides no validation; any string is accepted. C: Uses the wrong type (list) and only checks non-emptiness, not allowed values. D: `lifecycle` is not valid inside a variable block and `prevent_destroy` applies to resources, not variables.
Question 6
You have the following configuration to create three identical web servers in AWS: ```hcl resource "aws_instance" "web" { ami = var.ami_id instance_type = "t3.micro" count = 3 tags = { Name = "web-${count.index}" } } ``` Later, you need to give each instance a different `instance_type` based on a map variable. You also want Terraform to minimize resource recreation. What is the best approach?
Show Answer & Explanation
Correct Answer: C
To assign different `instance_type` values per instance and preserve resources as much as possible, you should refactor from `count` to `for_each` using a map keyed by stable identifiers (for example, `web1`, `web2`, `web3`). Then, use `moved` blocks to tell Terraform how existing instances (by index) map to the new keys. This minimizes recreation. Option C is correct.
A: Refactoring to `for_each` without `moved` blocks will cause Terraform to destroy and recreate resources because the addresses change.
B: Using `count.index` with conditionals quickly becomes brittle and doesn’t scale; it also doesn’t express a clear mapping per instance.
D: Changing `count` length alone does not allow per-instance types and does not preserve identity when the order or size changes.
Question 7
You are starting a new Terraform project to provision a few test VMs in a single cloud provider. The team wants to keep things simple and store state locally on each developer’s machine. However, they are concerned about accidentally committing state files to version control. What is the most appropriate action to take before running `terraform init` for the first time?
Show Answer & Explanation
Correct Answer: C
With local state (the default), Terraform writes `terraform.tfstate` and backup files like `terraform.tfstate.backup` in the working directory. To avoid committing sensitive state to version control, you should add `terraform.tfstate` and `terraform.tfstate.*` patterns to `.gitignore`. Option C is correct.
A: A remote backend would avoid local state, but the scenario explicitly wants local state and simplicity.
B: Ignoring `.terraform/` is good practice (it contains plugins and metadata) but does not prevent committing the state file itself.
D: There is no `TF_STATE_IGNORE` environment variable; Terraform always writes state unless configured with a backend that behaves differently.
Question 8
You refactor a configuration that manages a GCP Compute Engine instance. Originally, it was defined as `google_compute_instance.app`, but you move it into a child module `modules/compute` and expose it as `module.compute.google_compute_instance.app`. After refactoring, `terraform plan` shows it will destroy the old instance and create a new one. You want to preserve the existing instance. What is the best Terraform 1.x approach?
Show Answer & Explanation
Correct Answer: B
When refactoring resources (e.g., moving them into modules) and you want to keep the existing infrastructure, you use `moved` blocks to tell Terraform how the resource address changed. This updates state without destroying and recreating the resource. `-replace` (A) does the opposite: it forces recreation. Deleting `.terraform` and re-initializing (C) does not inform Terraform about address changes. `depends_on` (D) only affects dependency ordering, not state mapping or addresses.
Question 9
You are starting a new Terraform project to provision a small VPC on AWS. You copied HCL files from another repo and changed the provider version constraint. When you run `terraform init`, Terraform reports that the provider version in `.terraform.lock.hcl` does not match the new constraint. You want to use the new provider version and ensure the lock file is updated correctly for your team. What should you do next?
Show Answer & Explanation
Correct Answer: B
The dependency lock file `.terraform.lock.hcl` records the exact provider versions used. When you change version constraints and want Terraform to select newer versions and update the lock file, you should run `terraform init -upgrade`. This re-evaluates version constraints and updates the lock file accordingly. Deleting the lock file (A) works but discards the benefits of controlled, reproducible versions and is not the recommended workflow. Manually editing the lock file (C) is discouraged; Terraform manages it. `terraform apply` (D) does not ignore the lock file and will not resolve the mismatch correctly without re-initialization.
Question 10
Your configuration creates multiple AWS S3 buckets using `for_each` on a map of bucket names. You now need to remove one bucket from the map, but the bucket still exists in production and must be kept. You want Terraform to forget this bucket without deleting it. Which Terraform 1.x feature should you use?
Show Answer & Explanation
Correct Answer: C
Terraform 1.x introduced `removed` blocks to declare resources that should be removed from state without affecting real infrastructure. This is ideal when you want Terraform to stop managing a specific instance (e.g., a bucket) while leaving it intact in the provider. A: `prevent_destroy` blocks deletion but does not remove the resource from state. B: `moved` blocks are for refactoring addresses, not for forgetting resources. D: `-replace` forces recreation of a resource; it does not detach it from state and would risk deleting/recreating the bucket.
Ready to Accelerate Your TERRAFORM-004 Preparation?
Join thousands of professionals who are advancing their careers through expert certification preparation with FlashGenius.
- ✅ Unlimited practice questions across all TERRAFORM-004 domains
- ✅ Full-length exam simulations with real-time scoring
- ✅ AI-powered performance tracking and weak area identification
- ✅ Personalized study plans with adaptive learning
- ✅ Mobile-friendly platform for studying anywhere, anytime
- ✅ Expert explanations and study resources
Already have an account? Sign in here
About TERRAFORM-004 Certification
The TERRAFORM-004 certification validates your expertise in terraform fundamentals and other critical domains. Our comprehensive practice questions are carefully crafted to mirror the actual exam experience and help you identify knowledge gaps before test day.