|
| 1 | +############################################################################## |
| 2 | +# Resource Group |
| 3 | +############################################################################## |
| 4 | +module "resource_group" { |
| 5 | + source = "terraform-ibm-modules/resource-group/ibm" |
| 6 | + version = "1.0.6" |
| 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 | +# Create a VPC for this example using defaults from terraform-ibm-landing-zone-vpc |
| 14 | +# ( 3 subnets across the 3 AZs in the region ) |
| 15 | +############################################################################## |
| 16 | + |
| 17 | +module "vpc" { |
| 18 | + count = var.vpc_id != null ? 0 : 1 |
| 19 | + source = "terraform-ibm-modules/landing-zone-vpc/ibm" |
| 20 | + version = "7.5.0" |
| 21 | + resource_group_id = module.resource_group.resource_group_id |
| 22 | + region = var.region |
| 23 | + prefix = var.prefix |
| 24 | + name = var.vpc_name |
| 25 | + tags = var.resource_tags |
| 26 | +} |
| 27 | + |
| 28 | +############################################################################## |
| 29 | +# Demonstrate how to create a custom security group that is applied to the VPEs |
| 30 | +# This examples allow all workload associated with the default VPC security group |
| 31 | +# to interact with the VPEs |
| 32 | +############################################################################## |
| 33 | + |
| 34 | +data "ibm_is_vpc" "vpc" { |
| 35 | + # Explicit depends as the vpc_name is known prior to VPC creation |
| 36 | + depends_on = [ |
| 37 | + module.vpc |
| 38 | + ] |
| 39 | + name = var.vpc_id != null ? var.vpc_id : module.vpc[0].vpc_name |
| 40 | +} |
| 41 | + |
| 42 | +data "ibm_is_security_group" "default_sg" { |
| 43 | + name = data.ibm_is_vpc.vpc.default_security_group_name |
| 44 | +} |
| 45 | + |
| 46 | +module "vpe_security_group" { |
| 47 | + source = "terraform-ibm-modules/security-group/ibm" |
| 48 | + version = "2.0.0" |
| 49 | + security_group_name = "${var.prefix}-vpe-sg" |
| 50 | + add_ibm_cloud_internal_rules = false # No need for the internal ibm cloud rules for SG associated with VPEs |
| 51 | + |
| 52 | + security_group_rules = [{ |
| 53 | + name = "allow-all-default-sg-inbound" |
| 54 | + direction = "inbound" |
| 55 | + remote = data.ibm_is_security_group.default_sg.id |
| 56 | + }] |
| 57 | + |
| 58 | + resource_group = module.resource_group.resource_group_id |
| 59 | + vpc_id = var.vpc_id != null ? var.vpc_id : module.vpc[0].vpc_id |
| 60 | +} |
| 61 | + |
| 62 | +############################################################################## |
| 63 | +# Create Reserved IPs in the VPC |
| 64 | +############################################################################## |
| 65 | +module "ips" { |
| 66 | + source = "../../modules/reserved-ips" |
| 67 | + region = var.region |
| 68 | + subnet_zone_list = var.vpc_id != null ? var.subnet_zone_list : module.vpc[0].subnet_zone_list |
| 69 | + reserved_ips = {} |
| 70 | + reserved_ip_cloud_services = [ |
| 71 | + { |
| 72 | + service_name = "kms" |
| 73 | + }, |
| 74 | + { |
| 75 | + service_name = "cloud-object-storage" |
| 76 | + } |
| 77 | + ] |
| 78 | +} |
| 79 | + |
| 80 | +############################################################################## |
| 81 | +# Create VPEs in the VPC |
| 82 | +############################################################################## |
| 83 | +module "vpes" { |
| 84 | + source = "../../" |
| 85 | + region = var.region |
| 86 | + prefix = var.prefix |
| 87 | + vpc_name = var.vpc_name |
| 88 | + vpc_id = var.vpc_id != null ? var.vpc_id : module.vpc[0].vpc_id |
| 89 | + subnet_zone_list = var.vpc_id != null ? var.subnet_zone_list : module.vpc[0].subnet_zone_list |
| 90 | + resource_group_id = module.resource_group.resource_group_id |
| 91 | + security_group_ids = var.security_group_ids != null ? var.security_group_ids : [module.vpe_security_group.security_group_id] |
| 92 | + service_endpoints = var.service_endpoints |
| 93 | + reserved_ips = module.ips.reserved_ip_map |
| 94 | +} |
| 95 | + |
| 96 | +############################################################################## |
0 commit comments