|
| 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_group_auth_create_instance] |
| 18 | +resource "google_sql_database_instance" "default" { |
| 19 | + name = "postgres-iam-group-auth-instance-name" |
| 20 | + region = "us-west4" |
| 21 | + database_version = "POSTGRES_16" |
| 22 | + settings { |
| 23 | + tier = "db-custom-2-7680" |
| 24 | + database_flags { |
| 25 | + name = "cloudsql.iam_authentication" |
| 26 | + value = "on" |
| 27 | + } |
| 28 | + } |
| 29 | + # set `deletion_protection` to true, will ensure that one cannot accidentally |
| 30 | + # delete this instance by use of Terraform whereas |
| 31 | + # `deletion_protection_enabled` flag protects this instance at the GCP level. |
| 32 | + deletion_protection = false |
| 33 | +} |
| 34 | + |
| 35 | +# Specify the email address of the Cloud Identity group to add to the instance |
| 36 | +# This resource does not create a Cloud Identity group; the group must |
| 37 | +# already exist |
| 38 | + |
| 39 | +resource "google_sql_user" "iam_group" { |
| 40 | + |
| 41 | + instance = google_sql_database_instance.default.name |
| 42 | + type = "CLOUD_IAM_GROUP" |
| 43 | +} |
| 44 | + |
| 45 | +# [START cloud_sql_postgres_instance_iam_group_auth_grant_roles] |
| 46 | +data "google_project" "project" { |
| 47 | +} |
| 48 | + |
| 49 | +resource "google_project_iam_binding" "cloud_sql_user" { |
| 50 | + project = data.google_project.project.project_id |
| 51 | + role = "roles/cloudsql.instanceUser" |
| 52 | + members = [ |
| 53 | + |
| 54 | + ] |
| 55 | +} |
| 56 | +# [END cloud_sql_postgres_instance_iam_group_auth_grant_roles] |
| 57 | +# [END cloud_sql_postgres_instance_iam_group_auth_create_instance] |
0 commit comments