about summary refs log tree commit diff
path: root/ops/gcp-backups/main.tf
blob: f12e9cdc42695c575b4c30d5c0ab719fb4e737b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
locals {
  terraform_service_account = "terraform@fcuny-homelab.iam.gserviceaccount.com"
}

provider "google" {
  alias = "impersonation"
  scopes = [
    "https://www.googleapis.com/auth/cloud-platform",
    "https://www.googleapis.com/auth/userinfo.email",
  ]
}

data "google_service_account_access_token" "default" {
  provider               = google.impersonation
  target_service_account = local.terraform_service_account
  scopes                 = ["userinfo-email", "cloud-platform"]
  lifetime               = "1200s"
}

provider "google" {
  project         = "fcuny-backups"
  region          = "us-west1"
  zone            = "us-west1-c"
  access_token    = data.google_service_account_access_token.default.access_token
  request_timeout = "60s"
}

terraform {
  backend "gcs" {
    bucket                      = "world-tf-state"
    prefix                      = "backups/state"
    impersonate_service_account = "terraform@fcuny-homelab.iam.gserviceaccount.com"
  }
}

resource "google_service_account" "restic" {
  account_id   = "restic"
  description  = "For backups with restic"
  display_name = "Restic Service Account"
}

resource "google_storage_bucket" "archives" {
  name                        = "fcuny-archives"
  location                    = "US"
  storage_class               = "NEARLINE"
  uniform_bucket_level_access = true
  versioning {
    enabled = false
  }
  lifecycle_rule {
    action {
      type          = "SetStorageClass"
      storage_class = "ARCHIVE"
    }
    condition {
      matches_storage_class = ["NEARLINE"]
      age                   = 10
    }
  }
}

resource "google_storage_bucket" "backups-systems" {
  name                        = "fcuny-backups-systems"
  location                    = "US"
  storage_class               = "NEARLINE"
  uniform_bucket_level_access = true
  versioning {
    enabled = false
  }
}

resource "google_storage_bucket_iam_member" "backups-systems" {
  bucket = google_storage_bucket.backups-systems.name
  role   = "roles/storage.objectAdmin"
  member = "serviceAccount:${google_service_account.restic.email}"
}

resource "google_storage_bucket_iam_binding" "backups-systems-create" {
  bucket = google_storage_bucket.backups-systems.name
  role   = "roles/storage.objectCreator"
  members = [
    "serviceAccount:${google_service_account.restic.email}",
  ]
}

resource "google_storage_bucket_iam_binding" "backups-systems-view" {
  bucket = google_storage_bucket.backups-systems.name
  role   = "roles/storage.objectViewer"
  members = [
    "serviceAccount:${google_service_account.restic.email}",
  ]
}

resource "google_storage_bucket" "backups-users" {
  name                        = "fcuny-backups-users"
  location                    = "US"
  storage_class               = "NEARLINE"
  uniform_bucket_level_access = true
  versioning {
    enabled = false
  }
}

resource "google_storage_bucket_iam_member" "backups-users" {
  bucket = google_storage_bucket.backups-users.name
  role   = "roles/storage.objectAdmin"
  member = "serviceAccount:${google_service_account.restic.email}"
}

resource "google_storage_bucket_iam_binding" "backups-users-create" {
  bucket = google_storage_bucket.backups-users.name
  role   = "roles/storage.objectCreator"
  members = [
    "serviceAccount:${google_service_account.restic.email}",
  ]
}

resource "google_storage_bucket_iam_binding" "backups-users-view" {
  bucket = google_storage_bucket.backups-users.name
  role   = "roles/storage.objectViewer"
  members = [
    "serviceAccount:${google_service_account.restic.email}",
  ]
}

resource "google_storage_bucket" "restic" {
  name                        = "fcuny-restic"
  location                    = "US"
  storage_class               = "COLDLINE"
  uniform_bucket_level_access = true
  versioning {
    enabled = false
  }
  lifecycle_rule {
    action {
      type          = "SetStorageClass"
      storage_class = "ARCHIVE"
    }
    condition {
      matches_storage_class = ["COLDLINE"]
      age                   = 30
    }
  }
}

resource "google_storage_bucket" "repositories" {
  name                        = "fcuny-repositories"
  location                    = "US"
  storage_class               = "COLDLINE"
  uniform_bucket_level_access = true
  versioning {
    enabled = false
  }
  lifecycle_rule {
    action {
      type          = "SetStorageClass"
      storage_class = "ARCHIVE"
    }
    condition {
      matches_storage_class = ["COLDLINE"]
      age                   = 30
    }
  }
}