This repo contains example Terragrunt setup, allowing you to bootstrap three different environments (dev, staging and prod) on Google Cloud Platform using Terragrunt, based on common Terraform codebase.
| GCP service | Dev | Staging | Prod |
|---|---|---|---|
| Compute Engine | ✅ | ✅ | ✅ |
| Cloud Storage | ✅ | ✅ | ✅ |
| Cloud SQL | ❌ | ✅ db-f1-micro |
✅ db-g1-small |
Shared modules containing Terraform code only are located in modules directory. Each environment has its own directory in environments containing Terragrunt code and references to the earlier mentioned modules.
├── environments
│ ├── dev
│ │ ├── cloud-storage
│ │ │ └── terragrunt.hcl
│ │ ├── compute-engine
│ │ │ └── terragrunt.hcl
│ │ └── env.hcl
│ ├── prod
│ │ ├── cloud-sql
│ │ │ └── terragrunt.hcl
│ │ ├── cloud-storage
│ │ │ └── terragrunt.hcl
│ │ ├── compute-engine
│ │ │ └── terragrunt.hcl
│ │ └── env.hcl
│ └── staging
│ ├── cloud-sql
│ │ └── terragrunt.hcl
│ ├── cloud-storage
│ │ └── terragrunt.hcl
│ ├── compute-engine
│ │ └── terragrunt.hcl
│ └── env.hcl
├── modules
│ ├── cloud-sql
│ │ ├── main.tf
│ │ ├── terraform.tf
│ │ └── variables.tf
│ ├── cloud-storage
│ │ ├── main.tf
│ │ ├── terraform.tf
│ │ └── variables.tf
│ └── compute-engine
│ ├── main.tf
│ ├── terraform.tf
│ └── variables.tf
└── terragrunt.hcl
- Make sure you have following tools installed:
- Create a GCP project and set its ID for next commands:
% TERRAGRUNT_GCP_PROJECT_ID=your-project-id
- Set GCP project ID in your local gcloud configuration:
% gcloud config set project $TERRAGRUNT_GCP_PROJECT_ID
- Create service account for Terraform:
% gcloud iam service-accounts create terraform
- Create access key for the service account:
% TERRAGRUNT_GCP_SA_NAME=$(gcloud iam service-accounts list --filter="email ~ ^terraform" --format='value(email)')
% gcloud iam service-accounts keys create terraform.json --iam-account $TERRAGRUNT_GCP_SA_NAME
- Grant service account the project admin role (Note: you shouldn't be doing this in production, this is only for demo purposes):
% gcloud projects add-iam-policy-binding <projectId> --member=serviceAccount:$TERRAGRUNT_GCP_SA_NAME --role=roles/owner
- Make sure Google APIs we're going to use are enabled in project:
gcloud services enable compute
gcloud services enable sqladmin
- Adjust Cloud Storage bucket name for Terragrunt state and the project name/location in
terragrunt.hcl:
state_bucket = "terragrunt-gcp-demo-state"
project = "my-sandbox"
location = "europe-west4"
- Set path to service account key generated in steps above:
GOOGLE_APPLICATION_CREDENTIALS=terraform.json
- Initialize Terragrunt and create the state bucket:
% terragrunt init
Remote state GCS bucket terragrunt-gcp-demo-state does not exist or you don't have permissions to access it. Would you like Terragrunt to create it? (y/n) y
Terraform initialized in an empty directory!
The directory has no Terraform configuration files. You may begin working
with Terraform immediately by creating Terraform configuration files.
- Run plan to initialize all modules:
% terragrunt run-all plan
- Run apply to create all resources:
% terragrunt run-all apply
- Done! All your resources are created now.
- (Optional) After testing & playing around, destroy all previously created resources:
% terragrunt run-all destroy
Terragrunt commands can be ran within subdirectories, which correspond to environments. For instance, you can apply only dev environment changes:
% cd environments/dev/
% terragrunt run-all plan
INFO[0000] The stack at /Users/user/projects/terragrunt-gcp-demo/environments/dev will be processed in the following order for command plan:
Group 1
- Module /Users/user/projects/terragrunt-gcp-demo/environments/dev/cloud-storage
- Module /Users/user/projects/terragrunt-gcp-demo/environments/dev/compute-engine