GCS Backend
The GCS (Google Cloud Storage) backend stores state as an object in a Google Cloud Storage bucket.Implementation
Location:/internal/backend/remote-state/gcs/backend.go
Use Cases
- Managing Google Cloud Platform infrastructure
- Team collaboration on GCP projects
- Integration with Google Cloud Build
- Multi-cloud environments with GCP component
Basic Configuration
Required Configuration
bucket
- Type: String
- Required: Yes
- Description: The name of the GCS bucket
Optional Configuration
prefix
- Type: String
- Optional: Yes
- Default:
"" - Description: GCS prefix inside the bucket where state files will be saved
/ and will have a trailing / added if not present.
Authentication
The GCS backend supports multiple authentication methods:1. Application Default Credentials
Uses Google Application Default Credentials (ADC):2. Service Account Key (JSON)
GOOGLE_BACKEND_CREDENTIALSGOOGLE_CREDENTIALS
3. Access Token
GOOGLE_OAUTH_ACCESS_TOKEN
4. Service Account Impersonation
GOOGLE_BACKEND_IMPERSONATE_SERVICE_ACCOUNTGOOGLE_IMPERSONATE_SERVICE_ACCOUNT
Encryption
Customer-Supplied Encryption Keys (CSEK)
- 32 bytes long
- Base64 encoded
- Can be a file path or the key itself
GOOGLE_ENCRYPTION_KEY
Customer-Managed Encryption Keys (CMEK)
projects/{{project}}/locations/{{location}}/keyRings/{{keyRing}}/cryptoKeys/{{name}}
Environment Variable: GOOGLE_KMS_ENCRYPTION_KEY
Note: You cannot use both encryption_key and kms_encryption_key simultaneously.
Custom Storage Endpoint
For GCS-compatible storage systems:GOOGLE_BACKEND_STORAGE_CUSTOM_ENDPOINTGOOGLE_STORAGE_CUSTOM_ENDPOINT
Workspaces
The GCS backend supports workspaces. Each workspace stores its state in a separate object:State Locking
The GCS backend does not provide built-in state locking. For state locking, consider:- Using a different backend that supports locking
- Implementing external locking mechanisms
- Using Terraform Cloud/Enterprise
Configuration Options Summary
| Option | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | GCS bucket name |
prefix | string | No | Path prefix for state files |
credentials | string | No | Service account key JSON or file path |
access_token | string | No | OAuth2 access token |
impersonate_service_account | string | No | Service account email to impersonate |
impersonate_service_account_delegates | list(string) | No | Delegation chain for impersonation |
encryption_key | string | No | Base64-encoded customer-supplied encryption key |
kms_encryption_key | string | No | Cloud KMS crypto key name |
storage_custom_endpoint | string | No | Custom storage API endpoint |
Example: Development and Production
Development
Production with Encryption
IAM Permissions
The service account or user needs these permissions on the bucket:storage.buckets.getstorage.objects.createstorage.objects.deletestorage.objects.getstorage.objects.liststorage.objects.update
roles/storage.objectAdmin
For KMS encryption:
cloudkms.cryptoKeyVersions.useToEncryptcloudkms.cryptoKeyVersions.useToDecrypt
Best Practices
- Enable versioning on the GCS bucket for state history
- Use KMS encryption for production environments
- Service account impersonation instead of long-lived keys
- Separate buckets for different environments
- Lifecycle policies to manage old state versions
- Bucket policies to restrict access