Skip to content

Commit 94ec14d

Browse files
committed
[feat]: Add sample for custom node service account
1 parent fd51484 commit 94ec14d

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2025 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+
# Create an Autopilot cluster with a custom IAM node service account
18+
19+
# [START gke_node_service_account_create]
20+
resource "google_service_account" "default" {
21+
account_id = "gke-node-service-account"
22+
display_name = "GKE node service account"
23+
}
24+
# [END gke_node_service_account_create]
25+
26+
# [START gke_node_service_account_role]
27+
data "google_project" "project" {
28+
}
29+
30+
resource "google_project_iam_member" "project_role" {
31+
project = data.google_project.project.project_id
32+
role = "roles/container.defaultNodeServiceAccount"
33+
member = "serviceAccount:${google_service_account.default.email}"
34+
}
35+
# [END gke_node_service_account_role]
36+
37+
38+
# [START gke_autopilot_custom_service_account]
39+
resource "google_container_cluster" "autopilot_cluster" {
40+
name = "autopilot-custom-account"
41+
location = "us-central1"
42+
43+
enable_autopilot = true
44+
45+
cluster_autoscaling {
46+
auto_provisioning_defaults {
47+
service_account = google_service_account.default.email
48+
}
49+
}
50+
}
51+
# [END gke_autopilot_custom_service_account]

gke/node_service_account/main.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2025 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+
# Create a custom IAM node service account with the minimum role for GKE
18+
19+
# [START gke_node_service_account]
20+
# [START gke_node_service_account_create]
21+
resource "google_service_account" "default" {
22+
account_id = "gke-node-service-account"
23+
display_name = "GKE node service account"
24+
}
25+
# [END gke_node_service_account_create]
26+
27+
# [START gke_node_service_account_role]
28+
data "google_project" "project" {
29+
}
30+
31+
resource "google_project_iam_member" "default" {
32+
project = data.google_project.project.project_id
33+
role = "roles/container.defaultNodeServiceAccount"
34+
member = "serviceAccount:${google_service_account.default.email}"
35+
}
36+
# [END gke_node_service_account_role]
37+
# [END gke_node_service_account]

gke/standard/regional/node_pool/main.tf

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2024 Google LLC
2+
* Copyright 2025 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,18 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17+
# [START gke_standard_regional_node_pool_custom_sa]
1718
resource "google_service_account" "default" {
1819
account_id = "service-account-id"
1920
display_name = "Service Account"
2021
}
2122

23+
data "google_project" "project" {
24+
}
25+
26+
resource "google_project_iam_member" "default" {
27+
project = data.google_project.project.project_id
28+
role = "roles/container.defaultNodeServiceAccount"
29+
member = "serviceAccount:${google_service_account.default.email}"
30+
}
31+
# [END gke_standard_regional_node_pool_custom_sa]
32+
33+
# [START gke_standard_regional_cluster]
2234
resource "google_container_cluster" "default" {
2335
name = "gke-standard-regional-cluster"
2436
location = "us-central1"
2537

2638
initial_node_count = 1
2739
remove_default_node_pool = true
2840
}
41+
# [END gke_standard_regional_cluster]
2942

3043
# [START gke_standard_regional_node_pool]
3144
resource "google_container_node_pool" "default" {

0 commit comments

Comments
 (0)