Skip to content

Commit deae6f0

Browse files
committed
feat: Add samples for custom node service account
1 parent fd51484 commit deae6f0

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
/**
18+
* Create an Autopilot cluster that uses a custom IAM node service account
19+
* with the minimum role for GKE.
20+
* Before you run this sample, create a custom service account by following
21+
* https://cloud.google.com/kubernetes-engine/docs/how-to/hardening-your-cluster#use_least_privilege_sa
22+
*/
23+
24+
25+
# [START gke_autopilot_custom_service_account]
26+
resource "google_container_cluster" "autopilot_cluster" {
27+
name = "autopilot-custom-account"
28+
location = "us-central1"
29+
30+
enable_autopilot = true
31+
32+
cluster_autoscaling {
33+
auto_provisioning_defaults {
34+
service_account = google_service_account.default.email
35+
}
36+
}
37+
}
38+
# [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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)