Skip to content

Commit ac6625e

Browse files
authored
feat(cloud_sql): Add IAM group authentication samples for MySQL and Postgres (terraform-google-modules#710)
* Add IAM group authentication samples for MySQL and Postgres * docs: Update Cloud SQL IAM group auth samples * fix: Fix whitespace issue Cloud SQL IAM group auth samples
1 parent 96a3e2c commit ac6625e

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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_mysql_instance_iam_group_auth_create_instance]
18+
resource "google_sql_database_instance" "default" {
19+
name = "mysql-iam-group-auth-instance-name"
20+
region = "us-west4"
21+
database_version = "MYSQL_8_0"
22+
settings {
23+
tier = "db-f1-micro"
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_mysql_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_mysql_instance_iam_group_auth_grant_roles]
57+
# [END cloud_sql_mysql_instance_iam_group_auth_create_instance]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: mysql_instance_iam_group_auth
19+
spec:
20+
skip: true
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: postgres_instance_iam_group_auth
19+
spec:
20+
skip: true

0 commit comments

Comments
 (0)