|
| 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 | +locals { |
| 18 | + default_suffix = var.suffix == "" ? random_string.suffix.result : "${random_string.suffix.result}-${var.suffix}" |
| 19 | + key_name = "${var.key}-${local.default_suffix}" |
| 20 | +} |
| 21 | + |
| 22 | +resource "random_string" "suffix" { |
| 23 | + length = 4 |
| 24 | + special = false |
| 25 | + upper = false |
| 26 | +} |
| 27 | + |
| 28 | +module "kms" { |
| 29 | + source = "terraform-google-modules/kms/google" |
| 30 | + version = "2.3.0" |
| 31 | + |
| 32 | + keyring = "${var.keyring}-${local.default_suffix}" |
| 33 | + location = var.location |
| 34 | + project_id = var.project_id |
| 35 | + keys = [local.key_name] |
| 36 | + purpose = "ENCRYPT_DECRYPT" |
| 37 | + key_protection_level = "HSM" |
| 38 | + prevent_destroy = false |
| 39 | +} |
| 40 | + |
| 41 | +resource "google_service_account" "default" { |
| 42 | + project = var.project_id |
| 43 | + account_id = "confidential-compute-sa" |
| 44 | + display_name = "Custom SA for confidential VM Instance" |
| 45 | +} |
| 46 | + |
| 47 | +resource "google_project_iam_member" "service_account_roles" { |
| 48 | + for_each = toset(var.service_account_roles) |
| 49 | + |
| 50 | + project = var.project_id |
| 51 | + role = each.key |
| 52 | + member = "serviceAccount:${google_service_account.default.email}" |
| 53 | +} |
| 54 | + |
| 55 | +data "google_project" "project" { |
| 56 | + project_id = var.project_id |
| 57 | +} |
| 58 | + |
| 59 | +resource "google_kms_crypto_key_iam_binding" "crypto_key" { |
| 60 | + crypto_key_id = module.kms.keys[local.key_name] |
| 61 | + role = "roles/cloudkms.cryptoKeyEncrypterDecrypter" |
| 62 | + members = [ |
| 63 | + "serviceAccount:service-${data.google_project.project.number}@compute-system.iam.gserviceaccount.com", |
| 64 | + ] |
| 65 | +} |
| 66 | + |
| 67 | +module "instance_template" { |
| 68 | + source = "../../modules/instance_template" |
| 69 | + |
| 70 | + region = var.region |
| 71 | + project_id = var.project_id |
| 72 | + subnetwork = var.subnetwork |
| 73 | + |
| 74 | + name_prefix = "confidential-encrypted-template" |
| 75 | + source_image_project = "ubuntu-os-cloud" |
| 76 | + source_image = "ubuntu-2004-lts" |
| 77 | + machine_type = "n2d-standard-2" |
| 78 | + min_cpu_platform = "AMD Milan" |
| 79 | + enable_confidential_vm = true |
| 80 | + confidential_instance_type = "SEV" |
| 81 | + |
| 82 | + service_account = { |
| 83 | + email = google_service_account.default.email |
| 84 | + scopes = ["cloud-platform"] |
| 85 | + } |
| 86 | + disk_encryption_key = module.kms.keys[local.key_name] |
| 87 | +} |
| 88 | + |
| 89 | +module "compute_instance" { |
| 90 | + source = "terraform-google-modules/vm/google//modules/compute_instance" |
| 91 | + version = "~> 11.0" |
| 92 | + |
| 93 | + region = var.region |
| 94 | + subnetwork = var.subnetwork |
| 95 | + hostname = "confidential-encrypted-instance" |
| 96 | + instance_template = module.instance_template.self_link |
| 97 | + deletion_protection = false |
| 98 | +} |
0 commit comments