-
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Repurpose PSE terraform config to initialize cloud resources #2
Changes from 3 commits
97edb19
82a1196
53fde4c
33a7cd3
2b157c9
0a25c2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
terraform { | ||
backend "gcs" { | ||
bucket = "f3441e415e6e5e7d-bucket-tfstate" | ||
prefix = "terraform/state" | ||
jdangerx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
required_providers { | ||
google = { | ||
source = "hashicorp/google" | ||
version = "4.51.0" | ||
} | ||
} | ||
} | ||
|
||
variable "project_id" { | ||
type = string | ||
default = "catalyst-cooperative-pudl" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there anything weird about using the PUDL project from a different repo? Should we just create a mozilla project? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're billing infrastructure costs to Mozilla, we should probably make a separate mozilla project - that will make the bookkeeping much easier. If not, I think it's fine to use the same GCP project - though if we do that, I'd rather this terraform configuration live in the |
||
} | ||
|
||
variable "catalyst_people" { | ||
type = list(string) | ||
default = [ | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might want to add @bendnorman too. |
||
] | ||
} | ||
|
||
provider "google" { | ||
project = var.project_id | ||
region = "us-east1" | ||
zone = "us-east1-c" | ||
} | ||
|
||
resource "google_service_account" "mozilla_dev_sa" { | ||
account_id = "mozilla-dev-sa" | ||
display_name = "Mozilla dev" | ||
} | ||
|
||
resource "random_id" "bucket_prefix" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. super-nit: might be a little easier to read later if this bucket prefix is defined right next to the only place it's used - the tfstate bucket. |
||
byte_length = 8 | ||
} | ||
|
||
resource "google_storage_bucket" "sec_10ks" { | ||
name = "${random_id.bucket_prefix.hex}-bucket-sec-10ks" | ||
location = "US" | ||
storage_class = "STANDARD" | ||
versioning { | ||
enabled = true | ||
} | ||
} | ||
|
||
resource "google_storage_bucket" "tfstate" { | ||
name = "${random_id.bucket_prefix.hex}-tfstate" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if we create a new Mozilla project, we'll need to actually follow this guide which basically says:
If you want to stay in the PUDL project because the billing isn't separate, you can avoid making this new bucket and just use what we have already set up (the |
||
location = "US" | ||
storage_class = "STANDARD" | ||
versioning { | ||
enabled = true | ||
} | ||
} | ||
|
||
resource "google_storage_bucket_iam_binding" "catalyst_gcs_access" { | ||
bucket = google_storage_bucket.sec_10ks.name | ||
role = "roles/storage.admin" | ||
members = ["serviceAccount:${google_service_account.mozilla_dev_sa.email}"] | ||
} | ||
|
||
resource "google_project_iam_binding" "catalyst_cloudsql_instance_user" { | ||
project = var.project_id | ||
role = "roles/cloudsql.instanceUser" | ||
members = ["serviceAccount:${google_service_account.mozilla_dev_sa.email}"] | ||
} | ||
|
||
resource "google_project_iam_binding" "catalyst_cloudsql_client" { | ||
project = var.project_id | ||
role = "roles/cloudsql.client" | ||
members = ["serviceAccount:${google_service_account.mozilla_dev_sa.email}"] | ||
} | ||
|
||
resource "google_project_iam_binding" "catalyst_people_editors" { | ||
jdangerx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
project = var.project_id | ||
role = "roles/editor" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this making everyone an editor on the entire project? |
||
members = [for email in var.catalyst_people : "user:${email}"] | ||
} | ||
|
||
resource "google_project_iam_binding" "catalyst_iam_act_as_sa" { | ||
jdangerx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
project = var.project_id | ||
role = "roles/iam.serviceAccountUser" | ||
members = [for email in var.catalyst_people : "user:${email}"] | ||
} | ||
|
||
# cloud sql instance for usage | ||
resource "google_sql_database_instance" "mozilla" { | ||
name = "pg-mozilla" | ||
database_version = "POSTGRES_14" | ||
region = "us-central1" | ||
|
||
settings { | ||
tier = "db-f1-micro" | ||
activation_policy = "ALWAYS" | ||
database_flags { | ||
name = "cloudsql.iam_authentication" | ||
value = "on" | ||
} | ||
} | ||
} | ||
|
||
resource "google_sql_database" "database" { | ||
name = "mozilla" | ||
instance = google_sql_database_instance.mozilla.name | ||
} | ||
|
||
resource "google_sql_user" "default_user" { | ||
name = "${google_service_account.mozilla_dev_sa.account_id}@${var.project_id}.iam" | ||
instance = google_sql_database_instance.mozilla.name | ||
type = "CLOUD_IAM_SERVICE_ACCOUNT" | ||
} | ||
|
||
resource "google_sql_user" "catalyst_users" { | ||
for_each = toset(var.catalyst_people) | ||
name = each.value | ||
instance = google_sql_database_instance.mozilla.name | ||
type = "CLOUD_IAM_USER" | ||
} | ||
|
||
resource "google_secret_manager_secret" "postgres_pass" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that you'll have to create a new "secret version" manually to actually hold a secret password value. |
||
secret_id = "mozilla-postgres-pass" | ||
|
||
replication { | ||
automatic = true | ||
} | ||
} | ||
|
||
# DB permissions can't be granted with vanilla TF, so we have to do that manually: | ||
|
||
# 1. gcloud sql connect <instance> --user=postgres (using password stored in Secret Manager) | ||
# 2. CREATE ROLE mozillareadwrite; | ||
# 3. GRANT ALL ON DATABASE <db name> TO mozillareadwrite; | ||
# 4. GRANT mozillareadwrite to "[email protected]", ...; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the right shape of things, but you'll run into issues with
f34...
bucket not existing in the new project! You'll have to:gcs
backendgcs
backend with the new state bucketIt's fine to do that all locally before committing the changes, which should look exactly like this but with a different bucket name.