|
| 1 | +/** |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +# [START cloud_sql_postgres_instance_iam_db_auth] |
| 18 | +# [START cloud_sql_postgres_instance_iam_db_auth_create_instance] |
| 19 | +# [START cloud_sql_postgres_instance_iam_db_auth_add_users] |
| 20 | +resource "google_sql_database_instance" "default" { |
| 21 | + name = "postgres-db-auth-instance-name-test" |
| 22 | + region = "us-west4" |
| 23 | + database_version = "POSTGRES_14" |
| 24 | + settings { |
| 25 | + tier = "db-custom-2-7680" |
| 26 | + database_flags { |
| 27 | + name = "cloudsql.iam_authentication" |
| 28 | + value = "on" |
| 29 | + } |
| 30 | + } |
| 31 | + # set `deletion_protection` to true, will ensure that one cannot accidentally |
| 32 | + # delete this instance by use of Terraform whereas |
| 33 | + # `deletion_protection_enabled` flag protects this instance at the GCP level. |
| 34 | + deletion_protection = false |
| 35 | +} |
| 36 | +# [END cloud_sql_postgres_instance_iam_db_auth_create_instance] |
| 37 | + |
| 38 | +# Specify the email address of the IAM user to add to the instance |
| 39 | +# This resource does not create a new IAM user account; this account must |
| 40 | +# already exist |
| 41 | + |
| 42 | +resource "google_sql_user" "iam_user" { |
| 43 | + |
| 44 | + instance = google_sql_database_instance.default.name |
| 45 | + type = "CLOUD_IAM_USER" |
| 46 | +} |
| 47 | + |
| 48 | +# Specify the email address of the IAM service account to add to the instance |
| 49 | +# This resource does not create a new IAM service account; this service account |
| 50 | +# must already exist |
| 51 | + |
| 52 | +# Create a new IAM service account |
| 53 | + |
| 54 | +resource "google_service_account" "default" { |
| 55 | + account_id = "cloud-sql-postgres-sa" |
| 56 | + display_name = "Cloud SQL for Postgres Service Account" |
| 57 | +} |
| 58 | + |
| 59 | +resource "google_sql_user" "iam_service_account_user" { |
| 60 | + # Note: for PostgreSQL only, Google Cloud requires that you omit the |
| 61 | + # ".gserviceaccount.com" suffix |
| 62 | + # from the service account email due to length limits on database usernames. |
| 63 | + name = trimsuffix(google_service_account.default.email, ".gserviceaccount.com") |
| 64 | + instance = google_sql_database_instance.default.name |
| 65 | + type = "CLOUD_IAM_SERVICE_ACCOUNT" |
| 66 | +} |
| 67 | +# [END cloud_sql_postgres_instance_iam_db_auth_add_users] |
| 68 | + |
| 69 | +# [START cloud_sql_postgres_instance_iam_db_grant_roles] |
| 70 | +data "google_project" "project" { |
| 71 | +} |
| 72 | + |
| 73 | +resource "google_project_iam_binding" "cloud_sql_user" { |
| 74 | + project = data.google_project.project.project_id |
| 75 | + role = "roles/cloudsql.instanceUser" |
| 76 | + members = [ |
| 77 | + |
| 78 | + "serviceAccount:${google_service_account.default.email}" |
| 79 | + ] |
| 80 | +} |
| 81 | + |
| 82 | +resource "google_project_iam_binding" "cloud_sql_client" { |
| 83 | + project = data.google_project.project.project_id |
| 84 | + role = "roles/cloudsql.client" |
| 85 | + members = [ |
| 86 | + |
| 87 | + "serviceAccount:${google_service_account.default.email}" |
| 88 | + ] |
| 89 | +} |
| 90 | +# [END cloud_sql_postgres_instance_iam_db_grant_roles] |
| 91 | +# [END cloud_sql_postgres_instance_iam_db_auth] |
0 commit comments