Prepare and test your skills
Prepare and test your skills
Resource modularization and reusability is the practice of packaging infrastructure code into standard, repeatable components. Terraform (opens in a new tab) defines Google Cloud infrastructure in human-readable configuration files that can be versioned, reused, and shared to produce the same desired state every time. This approach keeps development, test, and production environments consistent while letting teams manage infrastructure changes like application code.
Terraform modules are reusable packages of Terraform configuration that provision a related set of Google Cloud resources. A module can create virtual machines, database connections, storage, and networking together as one tested unit. Every configuration must have an entrypoint called a Terraform root module, which is the starting directory Terraform loads. Infrastructure Manager (Infra Manager) (opens in a new tab) uses the term blueprint to mean a Terraform configuration that it deploys. Google Cloud provides prebuilt blueprints and modules for common patterns, and teams can also write their own modules to enforce standards for naming, networking, security, and sizing. Once defined, the same module can be called many times with different input values.
Variables and outputs let Terraform modules stay generic while adapting to each environment. Input values customize a module for a specific deployment, such as project, region, network name, or machine size, without changing the module code. Outputs expose key attributes created by the module, such as resource IDs or endpoints, so other modules and deployments can reference them.
This separation keeps the core resource definition stable while allowing controlled variation. The same virtual private cloud (VPC) module, for example, can create reproducible dev and prod networks from one definition by supplying different values per environment. Because the desired state is declared in code, redeploying the same configuration produces consistent infrastructure and reduces manual error and drift.
Reusing one configuration means defining infrastructure once from requirements and deploying that same code to create multiple environments consistently. Infrastructure as Code (IaC) configurations are stored in source control and can use continuous integration and continuous deployment (CI/CD) pipelines, so changes are reviewed, validated, audited, and reversible. This model provides a single source of truth and a full history of configuration changes.
A common GitOps pattern uses branches to represent environments, with dev and prod branches mapping to dev and prod VPC networks:
terraform plan for review without applying changes to any environment.dev branch, which applies the Terraform manifests to the development environment for testing.dev branch into the prod branch, which applies the same tested configuration to the production environment.This ordered flow moves code from proposal to development to production, so production only receives changes already validated in development.
Versioning and lifecycle management track which configuration created which infrastructure over time. Infra Manager is a managed Google Cloud service that automates deployment and management of Terraform blueprints without maintaining a separate toolchain. Terraform configurations for Infra Manager can be stored in Cloud Storage buckets, Git repositories, or local directories. Versioning is supported through Git history or through Cloud Storage Object Versioning for bucket-stored configurations. Infra Manager manages the infrastructure lifecycle through deployments and revisions, with support for preview deployments to see effects before applying and integrations with Cloud Build for automated workflows. For each deployment and revision, Infra Manager stores the logs, the configuration used, the list of Google Cloud resources created, and the state file. Infra Manager does not manage application deployment onto resources; application rollout uses products such as Cloud Build, Cloud Deploy, or third-party tools.
Exam tip: An Infra Manager blueprint must include a valid Terraform root module, and backend blocks should not be defined in the configuration.
Lifecycle execution and CI/CD integration is the practice of moving declarative infrastructure code from commit through plan, validation, and apply without manual console changes. Terraform (opens in a new tab) configuration describes the desired end state of Google Cloud resources, and automation compares that desired state to real infrastructure, previews differences, and then actuates changes. Infrastructure Manager (Infra Manager) (opens in a new tab) provides the managed Google Cloud toolchain for this lifecycle, while Cloud Build and Git workflows provide the continuous integration and continuous delivery path.
The Terraform execution lifecycle turns a declarative configuration into provisioned resources through an ordered plan-and-apply flow. The configuration declares the end state without describing how to build it, so the same files can create reproducible development, test, and production environments. Execution starts with terraform init to initialize providers, followed by terraform validate to check syntax, then terraform plan to evaluate the configuration and generate an execution plan for review. Execution finishes with terraform apply to provision resources by invoking Google Cloud APIs in the background, or terraform destroy when a deployment is being deleted.
The Terraform state file is the JSON mapping that lets Terraform decide whether to create, update, or leave each resource unchanged. When terraform apply runs, Terraform compares the configuration file to the mapping in the state file that records the most recent infrastructure state. If a resource object is defined in the configuration but does not exist in the state file, Terraform creates it; if an object exists in the state file but has a different configuration, Terraform updates it to match the configuration; if the state file object matches the configuration, Terraform leaves it unchanged. This comparison detects drift between desired and actual infrastructure and makes repeated deployments safe and repeatable.
Infra Manager deployments are managed executions of Terraform configurations, also called blueprints, from a stored source. A deployment is created from a configuration stored in a Cloud Storage bucket, a Git repository, or a local directory, with versioning handled in Git or with Object Versioning in Cloud Storage. An update to an existing deployment is applied as a revision, and Infra Manager stores the complete record for each deployment and revision for audit and rollback. For each revision, Infra Manager stores the related artifacts together:
A preview deployment shows how a proposed change would affect infrastructure before it is applied, so reviewers can verify effects without changing live resources. After a deployment completes, the deployment can be queried for its status, the list of deployed resources, and details about individual resources. Infra Manager executes each deployment through an ordered automated workflow:
terraform init, then terraform validate, then actuates with terraform apply or terraform destroy, while streaming logs into a storage bucket created by Infra Manager.terraform init or terraform apply a limited number of times, then creates a Cloud Storage bucket entry that stores deployment metadata.The runtime environment for this workflow is an ephemeral Cloud Build environment that runs the Terraform commands and is then discarded. The operating system and installed software in that environment are internal details that can change, but the supported Terraform version can be specified for each deployment. Infra Manager does not deploy applications onto resources; application deployment flows to products such as Cloud Build and Cloud Deploy.
The GitOps CI/CD pipeline uses a Git repository as the single source of truth for the desired environment state. Cloud Build (opens in a new tab) automatically applies Terraform manifests when branches change, while Terraform state persists in a Cloud Storage bucket. Developers propose changes on feature branches through pull requests, then promote tested changes forward in order: first merge into dev, then merge dev into prod after testing.
A process flow showing Terraform code moving from a feature branch (terraform plan only, nothing applied) to the dev branch (apply to dev VPC) and then to the prod branch (apply to prod VPC), with Cloud Build triggering each step.
| Branch pushed | Cloud Build action | Environment effect |
|---|---|---|
| Feature or other non-environment branch | Runs terraform plan for validation and testing | No resources are applied to any environment |
dev branch | Triggers and applies Terraform manifests | Desired state is actuated in the dev Virtual Private Cloud (VPC) environment |
prod branch | Triggers and applies Terraform manifests | Desired state is actuated in the prod VPC environment |
This branch promotion preserves environment integrity because production changes must first succeed in development. The same pattern extends to other integrations: a Terraform Cloud workspace connected to a version control system such as GitHub, GitLab, or Bitbucket can automatically trigger plans on new commits, and Infra Manager exposes integrations with Cloud Build for automated workflows. The Cloud Build service account that runs the pipeline must hold roles that permit it to access and change the target project resources.
Exam tip: A push to a feature branch runs terraform plan only with nothing applied, while a push to dev or prod applies manifests to that respective environment.
Shift-left validation is a pre-apply check that compares a Terraform plan against effective organization policies before resources are created. Security Command Center (opens in a new tab) validation analyzes the plan code against policies deployed on the Google Cloud organization, including policies defined through security postures, to determine whether new or modified definitions for clusters, buckets, or instances would violate controls. Validation supports Terraform plan files only, and supports organization policies and Security Health Analytics detectors only, so developers can find and fix security issues before the configuration reaches the environment.
Validation runs as part of the CI/CD workflow through an ordered sequence. First, the configuration is prepared with required context, including the parent attribute for every resource, supported asset types and policies only, and separate changes for assets versus policies because combined changes are not supported. Then the plan is built and converted for analysis using terraform init with Google provider version v5.5 or later, followed by terraform plan -out to create the binary plan, followed by terraform show -json to convert that plan to JSON. Finally, the JSON plan is submitted for a validation report through the Google Cloud CLI, Cloud Build, Jenkins with the Google Analyze Code Security plugin, or GitHub Actions, with optional build-failure criteria based on counts of critical, high, medium, and low findings.
Environment integrity also depends on change control and safe handling of configuration data. Storing provisioning logic in source control enables review, automatic validation, history, audit, and revert of every change, with production managed through change-controlled processes. Sensitive values such as passwords must not be included in Terraform configuration values, and fields marked as sensitive in resource changes are removed during validation. Together, versioned configurations, preview and plan review, automated apply, and policy validation keep deployed infrastructure aligned with the declared desired state.
State management and backend configuration is how Terraform tracks what it manages and where that tracking information lives. Terraform state (opens in a new tab) is the record that maps configuration files to real Google Cloud resources, such as virtual machines, database connections, and storage. Terraform backend configuration decides where that state file is stored and how all team members and automation access the same shared record.
Terraform state is needed because Infrastructure as Code works from a desired state. The configuration describes the end state you want, and Terraform compares that desired state with the state file and the actual infrastructure to decide what to create, change, or delete. If each engineer keeps a separate local state file, the team loses consistency and cannot safely reproduce development, test, and production environments from the same configuration. A remote backend solves this by providing a single source of truth for cloud infrastructure. The shared state lets teams reuse the same configuration consistently, automate resource management, and keep a history that can be audited and reverted. In practice, this shared record means:
When state is shared, changes made by one person or pipeline are visible to the next plan and apply, which keeps the desired state and real resources aligned.
Remote state in Cloud Storage means Terraform stores its state file in a Cloud Storage bucket instead of on a single laptop. The goal in team workflows is to configure Terraform to store state in a Cloud Storage bucket so engineers and Cloud Build (opens in a new tab) jobs use the same state during continuous integration and continuous delivery. The Terraform configuration itself is versioned in a Git repository or in a Cloud Storage bucket with Object Versioning, while the state bucket holds the result of what was actually deployed.
This pattern supports a GitOps workflow where branches dev and prod represent real environments defined by Virtual Private Cloud (VPC) networks dev and prod. Data and control flow from the branch push to the build system to the environment in order:
terraform plan, but nothing is applied to any environment.dev branch triggers Cloud Build to apply Terraform manifests to achieve the wanted state in the dev environment for testing.dev branch into the prod branch triggers installation of the same tested change into the production environment.Because Cloud Build performs the apply using the shared Cloud Storage state, the dev and prod VPC networks each converge to the state defined by their branch without depending on any one engineer's machine.
Infrastructure Manager (Infra Manager) (opens in a new tab) is a managed Google Cloud service that deploys Terraform configurations, also called blueprints, without maintaining a separate Terraform toolchain. Infra Manager runs deployments through a Cloud Build job using a Google-owned container image, so initialization, validation, and actuation happen in a managed runtime environment. The flow moves from configuration source to deployment to provisioned resources.
When a deployment is created, Infra Manager first validates the request and checks that the Terraform configuration location was specified. Then the Cloud Build job downloads the Terraform module from the storage bucket or Git repository, runs terraform init to initialize the backend and providers, runs terraform validate to check the configuration, and runs terraform apply or terraform destroy to actuate the change. Logs stream into a storage bucket created by Infra Manager, and the operation is retried a limited number of times for certain failures such as intermittent network outages.
For each deployment and each revision that updates it, Infra Manager stores the state file created by that operation along with the logs, the configuration used for the revision, and the list of Google Cloud resources created by the revision. This ties the state file to the deployment lifecycle: a preview deployment shows how changes would affect infrastructure before applying, and a new revision records a new state after the change is applied. Teams can then query the deployment for its status, the list of deployed resources, and details about individual resources.
A state management option is the combination of where the state file lives and who operates the automation around it. On Google Cloud, the same Terraform configuration can use a self-managed remote backend, a Google-managed deployment service, or a HashiCorp software as a service (SaaS) platform, depending on how much change management and hosting the team needs.
| Option | Where state lives | Choose when |
|---|---|---|
| Cloud Storage backend with Cloud Build | State file in a Cloud Storage bucket shared by engineers and build triggers | The team wants GitOps automation where branch pushes drive plan and apply |
| Infrastructure Manager | State file stored by Infra Manager for each deployment and revision, with logs and resource list | The team wants a managed Google Cloud toolchain and revision history without running Terraform infrastructure |
| Terraform Cloud | State and secrets stored securely in a stable, remote Terraform Cloud environment | The organization needs full change management, VCS connections that trigger plans, and API integration |
Terraform Cloud also connects to common version control systems like GitHub, GitLab, and Bitbucket, so new commits can automatically trigger Terraform plans. Terraform Enterprise is the self-hosted distribution of Terraform Cloud with customizable resource limits for strict security and compliance requirements.
Exam tip: Use a Cloud Storage backend when Cloud Build must share one state file across branches, while Infra Manager automatically stores a separate state file for each deployment and revision.
terraform init, then terraform validate, then terraform plan, then terraform apply, with terraform destroy used to delete a deployment.terraform plan only, while merging to dev or prod applies Terraform manifests to that respective VPC environment.The Terraform execution order is terraform init to initialize providers, then terraform validate to check syntax, then terraform plan to generate an execution plan for review, then terraform apply to provision resources by invoking Google Cloud APIs, with terraform destroy used to delete a deployment. The Terraform state file is the JSON mapping that records the most recent infrastructure state, and Terraform compares the configuration file to that mapping to create a resource defined in configuration but missing from state, update a resource whose state entry has a different configuration, or leave a matching resource unchanged.
Use a Cloud Storage backend with Cloud Build when the team wants GitOps automation where branch pushes drive plan and apply from a state file shared in a Cloud Storage bucket, use Infrastructure Manager (Infra Manager) when the team wants a managed Google Cloud toolchain that stores logs, configuration, resource list, and state file for each deployment and revision without running Terraform infrastructure, and use Terraform Cloud when the organization needs full change management with version control system connections that trigger plans and API integration. Terraform Cloud stores state and secrets securely in a stable remote environment and connects to GitHub, GitLab, and Bitbucket so new commits can automatically trigger plans.
Pushing Terraform code to a feature branch triggers terraform plan for review without applying changes to any environment, merging the reviewed change to the dev branch applies the Terraform manifests to the development environment for testing, and after testing merging the dev branch into the prod branch applies the same tested configuration to the production environment. Because Terraform state persists in a Cloud Storage bucket and Cloud Build performs the apply from shared state, the dev and prod Virtual Private Cloud (VPC) networks each converge to the state defined by their branch so production only receives changes already validated in development.
A distributed engineering team is collaborating on Google Cloud infrastructure provisioning using Terraform. The team is experiencing issues where concurrent execution of terraform apply causes state corruption and race conditions. In addition, the team needs to protect sensitive state data and ensure that previous versions of the state file can be restored if corrupted.
Which backend configuration should you implement to satisfy these requirements?
Last updated