Terraform template for Google Cloud (GCP)
Workflow features
- Authenticating via Workload Identity Federation
- Run
terraform apply
- Automatically running on
main
branch - Manual running on any branch
- Automatically running on
- Run
terraform plan
,terraform fmt
and tflint - Post
terraform plan
report to PullRequest comment and Job Summaries - Slack notification
- GitHub Actions
- Terraform v1.0+
- tfenv
- Cloud SDK
- You can also use the Cloud SDK already installed in Cloud Shell
gcloud auth login
# or
gcloud auth application-default login
gcloud config set project ${GCP_PROJECT_ID}
At first, enable Cloud Deployment Manager V2 API
Add roles/iam.securityAdmin
to [GCP_PROJECT_NUMBER]@cloudservices.gserviceaccount.com
gcloud projects add-iam-policy-binding ${GCP_PROJECT_ID} --member=serviceAccount:${GCP_PROJECT_NUMBER}@cloudservices.gserviceaccount.com --role=roles/iam.securityAdmin
- c.f. https://cloud.google.com/sdk/gcloud/reference/projects/add-iam-policy-binding
- NOTE: This is required for Deployment Manager to bind the IAM role to the Terraform service account.
Download deployment-manager/setup-terraform.jinja and deployment-manager/setup-terraform.jinja.schema
Run Deployment Manager
gcloud deployment-manager deployments create setup-terraform --template /path/to/setup-terraform.jinja --properties backendBucketName:${BACKEND_BUCKET_NAME},backendBucketLocation:${BACKEND_BUCKET_LOCATION}
backendBucketName
(Required)- Bucket name used as the backend of Terraform
- e.g.
${GCP_PROJECT_ID}-terraform
- c.f. https://www.terraform.io/language/settings/backends/gcs
backendBucketLocation
(optional)- Location of backend bucket (e.g.
us
,us-central1
) - c.f. https://cloud.google.com/storage/docs/locations
- default:
us
- Location of backend bucket (e.g.
SLACK_WEBHOOK
(optional)- Create from https://slack.com/apps/A0F7XDUAZ
- Upgrade to the latest version if necessary
Edit followings
gcp_project_id
- GCP project ID
provider_region
- Provider region
- see. https://cloud.google.com/compute/docs/regions-zones
terraform_service_account_id
- Account ID for the service account used by GitHub Actions
- This is usually
terraform
when service account is created by deployment-manager/setup-terraform.jinja
github_username
- GitHub user name (e.g.
octocat
)
- GitHub user name (e.g.
github_repository
- GitHub repository name (e.g.
Hello-World
)
- GitHub repository name (e.g.
Edit followings
terraform.backend.bucket
- Same to
BACKEND_BUCKET_NAME
- Same to
Upgrade to the latest version if necessary
terraform.required_providers.google.version
terraform.required_providers.google-beta.version
terraform.required_version
tfenv install
terraform init
# Run followings if you upgraded providers
terraform init -upgrade
git add .terraform.lock.hcl
git commit -m "terraform init -upgrade"
terraform plan
terraform apply
Edit followings
WORKLOAD_IDENTITY_PROVIDER
- This is created by Terraform
- See. https://console.cloud.google.com/iam-admin/workload-identity-pools
SERVICE_ACCOUNT_EMAIL
- This is created by Deployment Manager
- See. https://console.cloud.google.com/iam-admin/serviceaccounts
git push
and check your repository
API is activated within Deployment Manager, but it takes time for it to actually be activated, resulting in the following error.
Waiting for create [operation-1661583070797-5e73374b31d17-d7e061b5-aef21baf]...failed.
ERROR: (gcloud.deployment-manager.deployments.create) Error in Operation [operation-1661583070797-5e73374b31d17-d7e061b5-aef21baf]: errors:
- code: RESOURCE_ERROR
location: /deployments/setup-terraform/resources/terraform
message: '{"ResourceType":"iam.v1.serviceAccount","ResourceErrorCode":"403","ResourceErrorMessage":{"code":403,"errors":[{"domain":"usageLimits","message":"Identity
and Access Management (IAM) API has not been used in project 111111111111 before
or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/iam.googleapis.com/overview?project=111111111111
Please run gcloud deployment-manager deployments update
(NOT create
) after a few minutes. (Arguments are the same as for create
)
- Check latest version
- Edit .terraform-version
- Run
tfenv install
- Edit .github/dependabot.yml
- Wait for Dependabot to create a PullRequests
- Check latest versions
- Edit
terraform.required_providers.google.version
andterraform.required_providers.google-beta.version
in versions.tf - Run
terraform init -upgrade