How to fix Error: Error creating Cloud Run Service: PermissionDenied in Terraform
When deploying Cloud Run services with Terraform, a PermissionDenied error occurs because the Terraform service account or Cloud Run Service Agent lacks required IAM roles. This prevents Terraform from creating or updating Cloud Run services.
Quick fix
Fixes Error: Error creating Cloud Run Service: PermissionDenied
provider "google" {
credentials = file("path/to/service-account-key.json")
project = "your-gcp-project-id"
region = "us-central1"
}- Verify Terraform provider credentials configuration
- Grant Cloud Run Admin role to Terraform service account
- Grant IAM Service Account User role
- Ensure Cloud Run Service Agent has correct role
provider "google" {credentials = file("path/to/service-account-key.json")project = "your-gcp-project-id"region = "us-central1"}Error: Error creating Cloud Run Service: PermissionDenied
What this error means
This error indicates that the authenticated identity (your Terraform service account) does not have sufficient permissions to create or manage Cloud Run services in your GCP project. The error typically stems from missing IAM roles on either the Terraform execution account or the Cloud Run Service Agent. GCP uses Identity and Access Management (IAM) to control access to resources, and attempting operations without the required roles triggers this permission denied response.
Typical symptoms
- Terraform apply fails during google_cloud_run_v2_service or google_cloud_run_service resource creation
- Error message explicitly mentions PermissionDenied when attempting to create Cloud Run services
- Service deployment works via gcloud CLI but fails in Terraform
- Partial infrastructure is created (some resources succeed but Cloud Run creation fails)
Common causes
- Terraform service account lacks roles/run.admin or basic roles like roles/editor
- Cloud Run Service Agent (service-[PROJECT_NUMBER]@serverless-robot-prod.iam.gserviceaccount.com) does not have the roles/run.serviceAgent role
- Service account used for Cloud Run does not have iam.serviceAccounts.getAccessToken permission
- Required Google APIs (Cloud Run, Cloud Resource Manager, IAM) are not enabled on the project
- IAM role propagation delay after newly creating a service account (transient issue)
How to fix "Error: Error creating Cloud Run Service: PermissionDenied"
Check your Terraform provider block to ensure it references the correct credentials file and project ID:
provider "google" {
credentials = file("path/to/service-account-key.json")
project = "your-gcp-project-id"
region = "us-central1"
}Confirm the service account key file exists and is valid.
Use the gcloud CLI to grant the Terraform service account the Cloud Run Admin role:
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:terraform-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/run.admin"Replace terraform-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com with your actual Terraform service account email.
The Terraform service account must be able to use the Cloud Run service account. Grant this permission:
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:terraform-sa@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/iam.serviceAccountUser"This allows Terraform to deploy services that use service accounts.
Grant the Cloud Run Service Agent the required roles/run.serviceAgent role:
PROJECT_NUMBER=$(gcloud projects describe YOUR_PROJECT_ID --format='value(projectNumber)')
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
--member="serviceAccount:service-${PROJECT_NUMBER}@serverless-robot-prod.iam.gserviceaccount.com" \
--role="roles/run.serviceAgent"This gives the Cloud Run system the permissions it needs to manage services.
Ensure the necessary APIs are enabled on your GCP project:
gcloud services enable run.googleapis.com \
cloudresourcemanager.googleapis.com \
iam.googleapis.com \
YOUR_PROJECT_IDThe Cloud Run API, Cloud Resource Manager API, and IAM API must be active for Terraform operations.
After granting roles, wait 10-30 seconds for IAM changes to propagate across GCP systems, then retry:
terraform applyIf the error persists, roles may still be propagating. Wait a minute and try again. If it continues to fail, verify all roles were granted to the correct service accounts using:
gcloud projects get-iam-policy YOUR_PROJECT_ID --flatten="bindings[].members"Advanced notes
For service accounts created recently, IAM permission propagation can cause transient 403 errors. These typically resolve after waiting 30-60 seconds and retrying. If you are deploying a Cloud Run service that accesses other GCP resources (Cloud SQL, Secrets Manager, Cloud Storage), ensure the Cloud Run service account (not the Terraform account) has the appropriate roles on those resources. Use the google_cloud_run_service_iam_binding resource in Terraform to manage who can invoke your service. When using service account impersonation in Terraform, ensure the impersonating account has the roles/iam.serviceAccountTokenCreator role.
Frequently asked questions
- What does "Error: Error creating Cloud Run Service: PermissionDenied" mean?
- This error indicates that the authenticated identity (your Terraform service account) does not have sufficient permissions to create or manage Cloud Run services in your GCP project. The error typically stems from missing IAM roles on either the Terraform execution account or the Cloud Run Service Agent. GCP uses Identity and Access Management (IAM) to control access to resources, and attempting operations without the required roles triggers this permission denied response.
- How do I fix "Error: Error creating Cloud Run Service: PermissionDenied"?
- When deploying Cloud Run services with Terraform, a PermissionDenied error occurs because the Terraform service account or Cloud Run Service Agent lacks required IAM roles. This prevents Terraform from creating or updating Cloud Run services.
- What causes "Error: Error creating Cloud Run Service: PermissionDenied"?
- Terraform service account lacks roles/run.admin or basic roles like roles/editor Cloud Run Service Agent (service-[PROJECT_NUMBER]@serverless-robot-prod.iam.gserviceaccount.com) does not have the roles/run.serviceAgent role Service account used for Cloud Run does not have iam.serviceAccounts.getAccessToken permission
- Is the "Error: Error creating Cloud Run Service: PermissionDenied" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐Terraform Official Documentation
- ๐
- ๐
- ๐
- ๐
- ๐