
Answer-first summary for fast verification
Answer: Confirm that the pipeline is storing and retrieving the terraform.tfstate file from Cloud Storage with the Terraform gcs backend.
The issue arises because Terraform's state file (`terraform.tfstate`) is not being managed correctly, leading to duplicate infrastructure stacks. Terraform uses the state file to track the real-world resources it manages. If the state file isn't stored in a centralized, shared location with locking mechanisms, each pipeline run might create a new state file or use an outdated one, resulting in new resource deployments instead of updates. - **Option A**: Creating a cleanup pipeline addresses symptoms (orphaned stacks) but doesn't prevent duplication. It adds operational overhead and isn't Google's primary recommendation for state management. - **Option B**: Storing the state file in Google Cloud Storage (GCS) using Terraform's `gcs` backend is a Google-recommended practice. It ensures a single source of truth for the state, enables locking to prevent concurrent modifications, and allows Terraform to detect and update existing resources instead of creating new ones. This directly solves the duplication issue and optimizes costs. - **Option C**: Storing state in source control (e.g., Git) is discouraged because state files contain sensitive data (e.g., secrets) and lack built-in locking. This can cause conflicts, corruption, or unintended duplication during concurrent pipeline runs. - **Option D**: Removing infrastructure before applying changes causes downtime and inefficiency. It doesn’t leverage Terraform’s ability to manage incremental updates and is not a scalable or recommended practice. Thus, **Option B** aligns with Google’s best practices for Terraform state management in CI/CD pipelines, ensuring efficient resource updates and cost optimization.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
No comments yet.
You are managing infrastructure as code with Terraform in a CI/CD pipeline and observe that multiple copies of the entire infrastructure stack are being created in your Google Cloud project, with a new copy generated for each change. To optimize cloud costs by maintaining only a single instance of the infrastructure stack at any time while adhering to Google-recommended practices, what should you do?
A
Create a new pipeline to delete old infrastructure stacks when they are no longer needed.
B
Confirm that the pipeline is storing and retrieving the terraform.tfstate file from Cloud Storage with the Terraform gcs backend.
C
Verify that the pipeline is storing and retrieving the terraform.tfstate file from a source control.
D
Update the pipeline to remove any existing infrastructure before you apply the latest configuration.