|
| 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 | +data "google_project" "project" { |
| 25 | +} |
| 26 | + |
| 27 | +resource "google_service_account" "default" { |
| 28 | + account_id = "gke-node-service-account" |
| 29 | + display_name = "GKE node service account" |
| 30 | +} |
| 31 | + |
| 32 | +resource "google_project_iam_member" "default" { |
| 33 | + project = data.google_project.project.project_id |
| 34 | + role = "roles/container.defaultNodeServiceAccount" |
| 35 | + member = "serviceAccount:${google_service_account.default.email}" |
| 36 | +} |
| 37 | + |
| 38 | +# [START gke_autopilot_custom_service_account] |
| 39 | +resource "google_container_cluster" "default" { |
| 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] |
0 commit comments