|
| 1 | +############################################################################## |
| 2 | +# Resource Group |
| 3 | +############################################################################## |
| 4 | + |
| 5 | +module "resource_group" { |
| 6 | + source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-resource-group.git?ref=v1.0.5" |
| 7 | + # if an existing resource group is not set (null) create a new one using prefix |
| 8 | + resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null |
| 9 | + existing_resource_group_name = var.resource_group |
| 10 | +} |
| 11 | + |
| 12 | +############################################################################## |
| 13 | +# Key Protect All Inclusive |
| 14 | +############################################################################## |
| 15 | + |
| 16 | +module "key_protect_all_inclusive" { |
| 17 | + providers = { |
| 18 | + restapi = restapi.kp |
| 19 | + } |
| 20 | + source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-key-protect-all-inclusive.git?ref=v3.1.1" |
| 21 | + resource_group_id = module.resource_group.resource_group_id |
| 22 | + # Note: Database instance and Key Protect must be created in the same region when using BYOK |
| 23 | + # See https://cloud.ibm.com/docs/cloud-databases?topic=cloud-databases-key-protect&interface=ui#key-byok |
| 24 | + region = var.region |
| 25 | + key_protect_instance_name = "${var.prefix}-kp" |
| 26 | + resource_tags = var.resource_tags |
| 27 | + key_map = { "icd-pg" = ["${var.prefix}-pg"] } |
| 28 | +} |
| 29 | + |
| 30 | +# Create IAM Access Policy to allow Key protect to access Postgres instance |
| 31 | +resource "ibm_iam_authorization_policy" "policy" { |
| 32 | + source_service_name = "databases-for-postgresql" |
| 33 | + source_resource_group_id = module.resource_group.resource_group_id |
| 34 | + target_service_name = "kms" |
| 35 | + target_resource_instance_id = module.key_protect_all_inclusive.key_protect_guid |
| 36 | + roles = ["Reader"] |
| 37 | +} |
| 38 | + |
| 39 | +############################################################################## |
| 40 | +# Get Cloud Account ID |
| 41 | +############################################################################## |
| 42 | + |
| 43 | +data "ibm_iam_account_settings" "iam_account_settings" { |
| 44 | +} |
| 45 | + |
| 46 | +############################################################################## |
| 47 | +# VPC |
| 48 | +############################################################################## |
| 49 | +resource "ibm_is_vpc" "example_vpc" { |
| 50 | + name = "${var.prefix}-vpc" |
| 51 | + resource_group = module.resource_group.resource_group_id |
| 52 | + tags = var.resource_tags |
| 53 | +} |
| 54 | + |
| 55 | +resource "ibm_is_subnet" "testacc_subnet" { |
| 56 | + name = "${var.prefix}-subnet" |
| 57 | + vpc = ibm_is_vpc.example_vpc.id |
| 58 | + zone = "${var.region}-1" |
| 59 | + total_ipv4_address_count = 256 |
| 60 | + resource_group = module.resource_group.resource_group_id |
| 61 | +} |
| 62 | + |
| 63 | +############################################################################## |
| 64 | +# Create CBR Zone |
| 65 | +############################################################################## |
| 66 | +module "cbr_zone" { |
| 67 | + source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-cbr//cbr-zone-module?ref=v1.1.2" |
| 68 | + name = "${var.prefix}-VPC-network-zone" |
| 69 | + zone_description = "CBR Network zone representing VPC" |
| 70 | + account_id = data.ibm_iam_account_settings.iam_account_settings.account_id |
| 71 | + addresses = [{ |
| 72 | + type = "vpc", # to bind a specific vpc to the zone |
| 73 | + value = ibm_is_vpc.example_vpc.crn, |
| 74 | + }] |
| 75 | +} |
| 76 | + |
| 77 | +############################################################################## |
| 78 | +# Postgres Instance |
| 79 | +############################################################################## |
| 80 | + |
| 81 | +module "postgresql_db" { |
| 82 | + source = "../../profiles/fscloud" |
| 83 | + resource_group_id = module.resource_group.resource_group_id |
| 84 | + name = "${var.prefix}-postgres" |
| 85 | + region = var.region |
| 86 | + pg_version = var.pg_version |
| 87 | + key_protect_key_crn = module.key_protect_all_inclusive.keys["icd-pg.${var.prefix}-pg"].crn |
| 88 | + resource_tags = var.resource_tags |
| 89 | + allowlist = var.allowlist |
| 90 | + cbr_rules = [ |
| 91 | + { |
| 92 | + description = "${var.prefix}-postgres access only from vpc" |
| 93 | + enforcement_mode = "enabled" #Postgresql does not support report mode |
| 94 | + account_id = data.ibm_iam_account_settings.iam_account_settings.account_id |
| 95 | + rule_contexts = [{ |
| 96 | + attributes = [ |
| 97 | + { |
| 98 | + "name" : "endpointType", |
| 99 | + "value" : "private" |
| 100 | + }, |
| 101 | + { |
| 102 | + name = "networkZoneId" |
| 103 | + value = module.cbr_zone.zone_id |
| 104 | + }] |
| 105 | + }] |
| 106 | + } |
| 107 | + ] |
| 108 | +} |
0 commit comments