Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ jobs:
submodules: true

- name: Generate fake ./terraform/live/secrets.auto.tfvars
run: |
echo "project_id_map = { stage = \"mapswipe-stage\", prod = \"mapswipe-prod\" }" > ./terraform/live/secrets.auto.tfvars
run: cp terraform/live/secrets-sample.auto.tfvars terraform/live/secrets.auto.tfvars

# TODO: Cache plugins?
- uses: terraform-linters/setup-tflint@v5
Expand Down
1 change: 1 addition & 0 deletions terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.terragrunt-cache
.terraform
*.tfvars
!secrets-sample.auto.tfvars
20 changes: 14 additions & 6 deletions terraform/README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
## Project ID

**live/secrets.auto.tfvars**
```hcl
project_id_map = {
stage = "project-id-not-number"
prod = "project-id-not-number"
}
```
> NOTE: Sample is available here (./live/secrets-sample.auto.tfvars)[./live/secrets-sample.auto.tfvars]

## Apply changes

### Stage

```bash
# Google auth
gcloud auth application-default login

# Enable some api if not already
gcloud services enable storage.googleapis.com --project=YOUR_PROJECT_ID
gcloud services enable cloudresourcemanager.googleapis.com --project=YOUR_PROJECT_ID
gcloud services enable billingbudgets.googleapis.com --project=YOUR_PROJECT_ID
gcloud services enable iam.googleapis.com --project=YOUR_PROJECT_ID

# List all enabled apis
gcloud services list --enabled --project=YOUR_PROJECT_ID

# Terragrunt
cd live/stage

terragrunt plan
Expand Down
1 change: 1 addition & 0 deletions terraform/live/prod/terragrunt.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ terraform {
}

inputs = {
budget_amount = 50
}
8 changes: 8 additions & 0 deletions terraform/live/secrets-sample.auto.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
project_id_map = {
stage = "project-id-not-number"
prod = "project-id-not-number"
}

gcs_billing_account_id = "XXXXXX-YYYYYY-ZZZZZZ"

togglecorp_dev_email_address = "xxxxxxxxxxxx@yyyyyyyyyy.zzz"
1 change: 1 addition & 0 deletions terraform/live/stage/terragrunt.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ terraform {
}

inputs = {
budget_amount = 3
}
8 changes: 5 additions & 3 deletions terraform/live/terragrunt.root.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ remote_state {
}

inputs = {
env_name = local.env_name
gcs_project_id = local.secrets_config.project_id_map[local.env_name]
gcs_region = "EU"
env_name = local.env_name
gcs_billing_account_id = local.secrets_config.gcs_billing_account_id
gcs_project_id = local.secrets_config.project_id_map[local.env_name]
gcs_region = "EU"
togglecorp_dev_email_address = local.secrets_config.togglecorp_dev_email_address
}
53 changes: 53 additions & 0 deletions terraform/resources/budget.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
resource "google_monitoring_notification_channel" "toggle_dev" {
display_name = "Toggle dev"
type = "email"

labels = {
email_address = var.togglecorp_dev_email_address
}
}


resource "google_billing_budget" "gcp_budget" {
billing_account = var.gcs_billing_account_id

display_name = "Monthly Budget [${var.env_name}]"

budget_filter {
projects = ["projects/${data.google_project.mapswipe.number}"]
}

amount {
specified_amount {
currency_code = "GBP" # £
units = var.budget_amount
}
}

threshold_rules {
spend_basis = "CURRENT_SPEND"
threshold_percent = 0.9
}

threshold_rules {
spend_basis = "CURRENT_SPEND"
threshold_percent = 1.2
}

threshold_rules {
spend_basis = "CURRENT_SPEND"
threshold_percent = 1.4
}

threshold_rules {
spend_basis = "CURRENT_SPEND"
threshold_percent = 1.6
}

all_updates_rule {
monitoring_notification_channels = [
google_monitoring_notification_channel.toggle_dev.id,
]
disable_default_iam_recipients = true
}
}
3 changes: 3 additions & 0 deletions terraform/resources/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "google_project" "mapswipe" {
project_id = var.gcs_project_id
}
6 changes: 4 additions & 2 deletions terraform/resources/providers.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
provider "google" {
project = var.gcs_project_id
region = var.gcs_region
project = var.gcs_project_id
region = var.gcs_region
billing_project = var.gcs_project_id
user_project_override = true
}
16 changes: 16 additions & 0 deletions terraform/resources/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ variable "gcs_region" {
description = "GCS region"
type = string
}

variable "gcs_billing_account_id" {
description = "GCS billing account id"
type = string
sensitive = true
}

variable "budget_amount" {
description = "Budget amount in GBP (£)"
type = number
}

variable "togglecorp_dev_email_address" {
description = "Togglecorp dev email address"
type = string
}