Mastering Authentication for Terraform on Google Cloud Platform

Terraform serves as a cornerstone for Infrastructure as Code (IaC), allowing engineers to define, provision, and manage Google Cloud Platform (GCP) resources through HashiCorp Configuration Language (HCL) files. Unlike manual configuration via the Google Cloud Console—which lacks a historical record of changes and is prone to human error—Terraform provides a declarative approach. It maintains a state file to track the current deployment and calculates the delta between the desired state described in the code and the actual state of the cloud environment.

However, before Terraform can execute a single API call to create a storage bucket or a Compute Engine instance, it must prove its identity to GCP. Authentication is the critical bridge between the Terraform binary and the Google Cloud APIs. Depending on whether you are running Terraform on a local workstation, within a CI/CD pipeline, or via HCP Terraform, the authentication strategy varies significantly. Using the wrong method can lead to security vulnerabilities, such as leaked service account keys, or operational friction in automated environments.

The Core Logic of Terraform for GCP

To understand authentication, one must first understand how Terraform interacts with GCP. Terraform operates on a cycle of describe, plan, and apply. You describe the resources you want (e.g., VPC networks, Cloud Run services) in HCL. Terraform then reads these files, queries the existing GCP project, and generates a "plan"—a detailed list of what will be created, modified, or deleted. Once the user approves the plan, Terraform makes the necessary API calls to reach the desired state.

Because these API calls are performed on behalf of a user or a service, the Google Cloud provider within Terraform requires specific credentials. These credentials define not only who is making the request but also what permissions (roles) they have to modify the infrastructure.

Local Development Authentication: Application Default Credentials (ADC)

For developers working on local machines, Application Default Credentials (ADC) is the industry-recommended authentication strategy. ADC is a sophisticated mechanism used by Google's authentication libraries to automatically discover credentials based on the environment in which the application is running. This allows the same Terraform code to move from a developer's laptop to a production environment without requiring changes to the authentication logic in the HCL.

Authenticating with a User Account

The most straightforward way to set up ADC locally is by using the Google Cloud CLI (gcloud). This method leverages your own identity, which is ideal for testing and development.

The process involves the following steps:
- Install the Google Cloud SDK (gcloud) on your local machine.
- Initialize the environment using gcloud init.
- If you are utilizing an external identity provider (IdP), you must ensure you are signed in with your federated identity.
- Run the following command to create local authentication credentials for your user account:
gcloud auth application-default login

Upon executing this command, a browser sign-in screen appears. Once authenticated, Google Cloud creates a local JSON file containing the credentials that Terraform's Google provider can automatically detect and use. Note that if you are using Google Cloud Shell, this step is unnecessary as the environment is pre-authenticated.

Automated Authentication: Service Account Keys

While user-based ADC is perfect for local work, it is unsuitable for automation, such as Jenkins, GitHub Actions, or GitLab CI pipelines, because it requires an interactive browser login. For these scenarios, Service Accounts are used. A service account is a special Google account intended for non-human users, acting as the identity of the application or workload.

Implementation Steps for Service Accounts

To use a service account for Terraform authentication, follow these technical requirements:
- Create a Google Cloud Service Account via the IAM & Admin console.
- Grant the service account the necessary IAM roles (e.g., Project Editor or specific resource roles) to manage the intended infrastructure.
- Generate and download the Service Account Key file in JSON format.

Once the JSON key is obtained, there are two primary ways to pass it to Terraform.

Method A: Environment Variables (Recommended for Security)

The most secure method is to avoid putting the key path in the code and instead use the GOOGLE_APPLICATION_CREDENTIALS environment variable. This tells the Google provider exactly where to look for the identity file on the disk.

bash export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/keyfile.json"

Method B: Direct Provider Configuration (Discouraged)

You can explicitly define the credentials path within the Terraform provider block. While this is functional, it is generally discouraged because it hardcodes local file paths into the configuration, which reduces portability and can lead to security risks if the configuration is committed to version control.

hcl provider "google" { credentials = file("/path/to/your/keyfile.json") project = "your-project-id" region = "your-region" zone = "your-zone" }

Advanced Authentication in HCP Terraform

HCP Terraform (formerly Terraform Cloud) introduces unique challenges because the Terraform runs happen on HashiCorp's managed infrastructure, not on your local machine. Therefore, providing a local file path to a JSON key (as shown in the file() function above) will fail because the HCP Terraform worker cannot access your local hard drive.

Option 1: Using Terraform Variables for JSON Keys

To get a service account key into HCP Terraform, you must store the contents of the JSON file as a variable.

  1. Define a variable in your HCL to accept the JSON string:

```hcl
variable "gcp-creds" {
description = "GCP service account credentials JSON."
type = string
default = ""
}

provider "google" {
project = ""
region = ""
zone = ""
credentials = var.gcp-creds
}
```

  1. In the HCP Terraform workspace UI, create a variable named gcp-creds and paste the entire content of the JSON key file into the value field.

Option 2: Dynamic Credentials via OIDC (The Modern Standard)

The most secure way to authenticate HCP Terraform to GCP is through dynamic credentials using OpenID Connect (OIDC). This eliminates the need for "long-lived" JSON keys, which are a major security liability because they do not expire and can be stolen.

Dynamic credentials allow HCP Terraform to request a short-lived access token from GCP on the fly. The token is valid only for the duration of the plan or apply phase.

Configuration Requirements for Dynamic Credentials

To enable dynamic credentials, a trust relationship must be established between GCP and HCP Terraform. This involves configuring a Workload Identity Pool and Provider on GCP.

The configuration in HCP Terraform is managed through specific environment variables. These variables tell the GCP provider how to authenticate and which identity to assume.

Variable Required Value Technical Notes
TFC_GCP_USE_DYNAMIC_CREDENTIALS true Must be set to true; otherwise, HCP Terraform ignores dynamic credential settings.
TFC_GCP_PRINCIPAL_TYPE service_account or workload_pool Specifies the identity type. Defaults to service_account. Requires v1.28.3+ for self-managed agents.
TFC_GCP_WORKLOAD_PROVIDER_NAME Canonical Name The full canonical name of the workload identity provider. Takes precedence over separate pool/project IDs.
TFC_GCP_RUN_SERVICE_ACCOUNT_EMAIL Email Address The primary service account HCP Terraform uses to authenticate.
TFC_GCP_PLAN_SERVICE_ACCOUNT_EMAIL Email Address Specific service account for the plan phase. Falls back to RUN email if not set.
TFC_GCP_APPLY_SERVICE_ACCOUNT_EMAIL Email Address Specific service account for the apply phase. Falls back to RUN email if not set.

Workload Identity Configuration Logic

When configuring the Workload Identity Provider, you can provide the identity in two formats:
1. A single unified variable (TFC_GCP_WORKLOAD_PROVIDER_NAME) containing the canonical name.
2. Three separate variables consisting of the project number, the pool ID, and the provider ID.

If both formats are provided, the unified canonical name takes precedence. Additionally, the TFC_GCP_WORKLOAD_PROVIDER_NAME requires version 1.7.0 or later if self-managing agents.

Critical Limitation: Dynamic credentials will not function if your Terraform Enterprise instance is utilizing a custom or self-signed certificate, as this is a restriction imposed by GCP.

Authentication Method Comparison

Choosing the right authentication method depends on the environment and the security requirements of the organization.

Method Use Case Security Level Lifespan of Credential Setup Complexity
User ADC Local Development Medium Session-based Low
Service Account Key (JSON) Basic CI/CD Low Permanent (until rotated) Medium
HCP Var (JSON) Legacy HCP Terraform Low Permanent Medium
Dynamic Credentials (OIDC) Enterprise CI/CD / HCP High Short-lived (Ephemeral) High

Detailed Technical Implementation Workflow

To implement a professional-grade authentication pipeline for a GCP project using Terraform, the following architectural workflow is recommended:

Stage 1: Local Sandbox

Developers use gcloud auth application-default login. This ensures that no secrets are stored in the local git repository. Terraform is configured without a credentials attribute in the provider "google" block, allowing it to naturally fall back to ADC.

Stage 2: CI/CD Integration

In the automation server, a service account is created with the "Least Privilege" principle. Instead of "Project Editor," the account is granted only the specific roles needed (e.g., roles/compute.admin, roles/storage.admin). The JSON key is stored as an encrypted secret in the CI/CD system (like GitHub Secrets) and injected as the GOOGLE_APPLICATION_CREDENTIALS environment variable during the runtime.

Stage 3: Enterprise Scale with HCP Terraform

For organizations using HCP Terraform, the OIDC integration is configured. A Workload Identity Pool is created in GCP that trusts the HCP Terraform OIDC issuer. In the HCP Terraform workspace, TFC_GCP_USE_DYNAMIC_CREDENTIALS is set to true. This ensures that even if a workspace is compromised, there are no permanent keys to steal, as the access tokens expire immediately after the Terraform run completes.

Conclusion

Authentication is the most critical security layer when managing Google Cloud infrastructure with Terraform. For individuals and local testers, Application Default Credentials (ADC) via the gcloud CLI provide a seamless and secure entry point. For automation, while Service Account JSON keys are common, they introduce significant risk due to their permanent nature and the potential for accidental exposure in version control.

The industry is rapidly moving toward identity federation and ephemeral credentials. The implementation of dynamic credentials in HCP Terraform via OIDC represents the gold standard for security, removing the need for secret management entirely by leveraging short-lived tokens and trust relationships between the identity provider and the cloud provider. By carefully selecting the authentication method based on the environment—ADC for local, managed secrets for basic CI, and OIDC for enterprise automation—engineers can ensure their infrastructure is both scalable and secure.

Sources

  1. Managing Google Cloud Credentials with Terraform
  2. Dynamic Provider Credentials - GCP Configuration
  3. Terraform for Google Cloud
  4. How to set up Google Cloud (GCP) credentials in HCP Terraform
  5. Authenticate to Google Cloud when using Terraform

Related Posts