cloud-foundation-fabric/blueprints/data-solutions/cmek-via-centralized-kms/main.tf

168 lines
5.4 KiB
Terraform
Raw Permalink Normal View History

2023-09-15 07:05:36 -07:00
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2023-02-04 22:52:33 -08:00
locals {
# Needed when you create KMS keys and encrypted resources in the same terraform state but different projects.
kms_keys = {
2023-02-05 12:12:46 -08:00
gce = "projects/${module.project-kms.project_id}/locations/${var.region}/keyRings/${var.prefix}-${var.region}/cryptoKeys/key-gcs"
gcs = "projects/${module.project-kms.project_id}/locations/${var.region}/keyRings/${var.prefix}-${var.region}/cryptoKeys/key-gcs"
2023-02-04 22:52:33 -08:00
}
}
###############################################################################
# Projects #
###############################################################################
module "project-service" {
2022-01-10 06:09:38 -08:00
source = "../../../modules/project"
2023-02-05 12:12:46 -08:00
name = var.project_config.project_ids.service
parent = var.project_config.parent
billing_account = var.project_config.billing_account_id
project_create = var.project_config.billing_account_id != null
prefix = var.project_config.billing_account_id == null ? null : var.prefix
services = [
"compute.googleapis.com",
"servicenetworking.googleapis.com",
2023-02-04 22:52:33 -08:00
"storage.googleapis.com",
"storage-component.googleapis.com",
]
service_encryption_key_ids = {
compute = [
local.kms_keys.gce
]
storage = [
local.kms_keys.gcs
]
}
service_config = {
disable_on_destroy = false, disable_dependent_services = false
}
depends_on = [
module.kms
]
}
module "project-kms" {
2022-01-10 06:09:38 -08:00
source = "../../../modules/project"
2023-02-05 12:12:46 -08:00
name = var.project_config.project_ids.encryption
parent = var.project_config.parent
billing_account = var.project_config.billing_account_id
project_create = var.project_config.billing_account_id != null
prefix = var.project_config.billing_account_id == null ? null : var.prefix
services = [
"cloudkms.googleapis.com",
"servicenetworking.googleapis.com"
]
2023-02-04 22:52:33 -08:00
service_config = {
disable_on_destroy = false, disable_dependent_services = false
}
}
###############################################################################
# Networking #
###############################################################################
module "vpc" {
2022-01-10 06:09:38 -08:00
source = "../../../modules/net-vpc"
project_id = module.project-service.project_id
2023-02-04 22:52:33 -08:00
name = "${var.prefix}-vpc"
subnets = [
{
2023-02-04 22:52:33 -08:00
ip_cidr_range = "10.0.0.0/20"
name = "${var.prefix}-${var.region}"
region = var.region
}
]
}
module "vpc-firewall" {
source = "../../../modules/net-vpc-firewall"
project_id = module.project-service.project_id
network = module.vpc.name
default_rules_config = {
2023-02-04 22:52:33 -08:00
admin_ranges = ["10.0.0.0/20"]
}
}
###############################################################################
# KMS #
###############################################################################
module "kms" {
2022-01-10 06:09:38 -08:00
source = "../../../modules/kms"
project_id = module.project-kms.project_id
keyring = {
2023-02-05 12:12:46 -08:00
name = "${var.prefix}-${var.region}",
2023-02-04 22:52:33 -08:00
location = var.region
}
2023-09-15 07:05:36 -07:00
keys = {
key-gce = {}
key-gcs = {}
}
}
###############################################################################
# GCE #
###############################################################################
module "vm_example" {
2022-01-10 06:09:38 -08:00
source = "../../../modules/compute-vm"
project_id = module.project-service.project_id
zone = "${var.region}-b"
2023-02-04 22:52:33 -08:00
name = "${var.prefix}-vm"
network_interfaces = [{
network = module.vpc.self_link,
2023-02-04 22:52:33 -08:00
subnetwork = module.vpc.subnet_self_links["${var.region}/${var.prefix}-${var.region}"],
nat = false,
addresses = null
}]
attached_disks = [
{
name = "data"
size = 10
source = null
source_type = null
options = null
}
]
boot_disk = {
initialize_params = {
image = "projects/debian-cloud/global/images/family/debian-10"
type = "pd-ssd"
size = 10
}
}
tags = ["ssh"]
encryption = {
encrypt_boot = true
disk_encryption_key_raw = null
2023-02-04 22:52:33 -08:00
kms_key_self_link = local.kms_keys.gce
}
}
###############################################################################
# GCS #
###############################################################################
module "kms-gcs" {
2022-01-10 06:09:38 -08:00
source = "../../../modules/gcs"
2020-10-29 15:22:58 -07:00
project_id = module.project-service.project_id
2023-02-04 22:52:33 -08:00
prefix = var.prefix
name = "${var.prefix}-bucket"
location = var.region
storage_class = "REGIONAL"
encryption_key = local.kms_keys.gcs
force_destroy = !var.deletion_protection
}