Use Terraform to create and manage secrets in Google Cloud Secret Manager—so API keys, passwords, and certificates live in a central, auditable store instead of config files or code.
GCP Secret Manager is a secure storage service for sensitive data. It gives you a single place to manage, access, and audit secrets across Google Cloud. Terraform lets you define that setup as code (HCL or JSON) and apply it consistently across projects and environments.
Important: Terraform stores secret values in the state file in plain text. Use remote state with encryption, restrict access to state, and consider external secret injection for production.
Related reading:
- How to Create a Service Account for Terraform in GCP
- How to Use External Secrets with GCP Secrets Manager
- How to Use Terraform to Create a VPC and Cloud SQL in GCP
- How to Use Terraform to Create a VPC and GKE in GCP
- How to Use Terraform to Create a VPC and Compute Instance in GCP
- How to Use Terraform to Create a Redis Instance in GCP
Prerequisites
You need:
- A GCP account with a project. If you don’t have one, create a free account at cloud.google.com.
- A GCP service account for Terraform. See How to Create a Service Account for Terraform in GCP.
- An existing GCP project where you will store secrets.
Log in to Google Cloud with the Cloud SDK:
| |
1. Enable the Secret Manager API
Enable the Secret Manager API for your project (required). In the GCP Console, search for Secret Manager and click Enable if it isn’t already. You only need to do this once per project.
With the gcloud CLI:
| |
Or with Terraform:
| |
2. Grant the Service Account Secret Manager Permissions
Your Terraform service account needs permission to manage secrets. In the GCP Console: IAM & Admin → IAM, find the service account, click the pencil icon (Edit), and add the Secret Manager Admin role.
With gcloud:
| |
3. Add Terraform Provider and Create a Secret
Configure the Google provider and a Secret Manager secret with automatic replication and a first version.
Provider and locals:
| |
Create a secret named live-app-password with automatic replication and a secret version:
| |
Grant access to a user or service account:
| |
Optional: Secret with User-Managed Replication
To control exactly where secrets are replicated, use user-managed replication and list the locations. Example: replicate only in europe-west1 and europe-west2:
| |
4. Read Secret Data in Terraform
Use a data source to read a secret version. Specify a version to get that version; omit it to use the latest.
Specific version (e.g. version 1):
| |
Latest version:
| |
5. Use the Secret in Outputs (or Other Resources)
Reference the data source to expose the secret value (e.g. in an output). Avoid outputting secrets in production; use them only in resources that need them.
| |
Setting sensitive = true prevents the value from appearing in plan/apply logs.
Summary and Next Steps
You now use Terraform to create GCP Secret Manager secrets, set replication (automatic or user-managed), create versions, grant IAM access, and read secret data via data sources.
Next steps:
- Use remote state (e.g. GCS backend) with encryption and access controls so the state file isn’t stored locally.
- Prefer least-privilege roles (e.g.
roles/secretmanager.secretAccessorfor apps) instead of Secret Manager Admin where possible. - To sync Secret Manager into Kubernetes, use External Secrets with GCP Secret Manager.