|
| 1 | +############################################################################## |
| 2 | +# Resource Group |
| 3 | +############################################################################## |
| 4 | + |
| 5 | +module "resource_group" { |
| 6 | + source = "terraform-ibm-modules/resource-group/ibm" |
| 7 | + version = "1.0.6" |
| 8 | + # if an existing resource group is not set (null) create a new one using prefix |
| 9 | + resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null |
| 10 | + existing_resource_group_name = var.resource_group |
| 11 | +} |
| 12 | + |
| 13 | +############################################################################## |
| 14 | +# VPC |
| 15 | +############################################################################## |
| 16 | + |
| 17 | +resource "ibm_is_vpc" "example_vpc" { |
| 18 | + name = "${var.prefix}-vpc" |
| 19 | + resource_group = module.resource_group.resource_group_id |
| 20 | + tags = var.resource_tags |
| 21 | +} |
| 22 | + |
| 23 | +resource "ibm_is_subnet" "testacc_subnet" { |
| 24 | + name = "${var.prefix}-subnet" |
| 25 | + vpc = ibm_is_vpc.example_vpc.id |
| 26 | + zone = "${var.region}-1" |
| 27 | + total_ipv4_address_count = 256 |
| 28 | + resource_group = module.resource_group.resource_group_id |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +############################################################################## |
| 33 | +# Get Cloud Account ID |
| 34 | +############################################################################## |
| 35 | + |
| 36 | +data "ibm_iam_account_settings" "iam_account_settings" { |
| 37 | +} |
| 38 | + |
| 39 | +############################################################################## |
| 40 | +# Create CBR Zone |
| 41 | +############################################################################## |
| 42 | + |
| 43 | +module "cbr_zone_vpc" { |
| 44 | + source = "terraform-ibm-modules/cbr/ibm//modules/cbr-zone-module" |
| 45 | + version = "1.9.0" |
| 46 | + name = "${var.prefix}-VPC-network-zone" |
| 47 | + zone_description = "CBR Network zone containing VPC" |
| 48 | + account_id = data.ibm_iam_account_settings.iam_account_settings.account_id |
| 49 | + addresses = [{ |
| 50 | + type = "vpc", # to bind a specific vpc to the zone |
| 51 | + value = ibm_is_vpc.example_vpc.crn, |
| 52 | + }] |
| 53 | +} |
| 54 | + |
| 55 | +module "cos_instance_and_bucket" { |
| 56 | + source = "terraform-ibm-modules/cos/ibm" |
| 57 | + version = "6.12.2" |
| 58 | + resource_group_id = module.resource_group.resource_group_id |
| 59 | + region = var.region |
| 60 | + create_cos_instance = true |
| 61 | + create_cos_bucket = true |
| 62 | + bucket_name = "${var.prefix}-cos-bucket" |
| 63 | + kms_encryption_enabled = false |
| 64 | + skip_iam_authorization_policy = true |
| 65 | + cos_instance_name = "${var.prefix}-cos-instance" |
| 66 | +} |
| 67 | + |
| 68 | +locals { |
| 69 | + # List of resources to apply rules to |
| 70 | + resource_list = [ |
| 71 | + [{ |
| 72 | + attributes = [ |
| 73 | + { |
| 74 | + name = "accountId" |
| 75 | + value = data.ibm_iam_account_settings.iam_account_settings.account_id |
| 76 | + operator = "stringEquals" |
| 77 | + }, |
| 78 | + { |
| 79 | + name = "serviceInstance" |
| 80 | + value = module.cos_instance_and_bucket.cos_instance_guid |
| 81 | + operator = "stringEquals" |
| 82 | + }, |
| 83 | + { |
| 84 | + name = "serviceName" |
| 85 | + value = "cloud-object-storage" |
| 86 | + operator = "stringEquals" |
| 87 | + } |
| 88 | + ] }], |
| 89 | + [{ |
| 90 | + attributes = [ |
| 91 | + { |
| 92 | + name = "accountId" |
| 93 | + value = data.ibm_iam_account_settings.iam_account_settings.account_id |
| 94 | + operator = "stringEquals" |
| 95 | + }, |
| 96 | + { |
| 97 | + name = "serviceInstance" |
| 98 | + value = module.cos_instance_and_bucket.bucket_crn |
| 99 | + operator = "stringEquals" |
| 100 | + }, |
| 101 | + { |
| 102 | + name = "serviceName" |
| 103 | + value = "cloud-object-storage" |
| 104 | + operator = "stringEquals" |
| 105 | + } |
| 106 | + ] }] |
| 107 | + ] |
| 108 | + |
| 109 | + # rule to be applied for each resource |
| 110 | + rule = { |
| 111 | + enforcement_mode = "report" |
| 112 | + rule_contexts = [ |
| 113 | + { |
| 114 | + attributes = [ |
| 115 | + { |
| 116 | + "name" : "endpointType", |
| 117 | + "value" : "private" |
| 118 | + }, |
| 119 | + { |
| 120 | + name = "networkZoneId" |
| 121 | + value = module.cbr_zone_vpc.zone_id |
| 122 | + } |
| 123 | + ] |
| 124 | + } |
| 125 | + ] |
| 126 | + operations = [ |
| 127 | + { |
| 128 | + api_types = [ |
| 129 | + { |
| 130 | + api_type_id = "crn:v1:bluemix:public:context-based-restrictions::::api-type:" |
| 131 | + } |
| 132 | + ] |
| 133 | + } |
| 134 | + ] |
| 135 | + } |
| 136 | + |
| 137 | + # List of rule descriptions |
| 138 | + rule_descriptions = [ |
| 139 | + "sample rule for the instance ${module.cos_instance_and_bucket.cos_instance_guid} access from vpc zone", |
| 140 | + "sample rule for the bucket ${module.cos_instance_and_bucket.bucket_name} access from vpc zone", |
| 141 | + ] |
| 142 | +} |
| 143 | + |
| 144 | +# Create CBR Rules Last |
| 145 | +# |
| 146 | +module "cbr_rules" { |
| 147 | + count = length(local.resource_list) |
| 148 | + source = "../../modules/cbr-rule-module" |
| 149 | + rule_description = local.rule_descriptions[count.index] != null ? local.rule_descriptions[count.index] : "sample rule" |
| 150 | + enforcement_mode = local.rule.enforcement_mode |
| 151 | + rule_contexts = local.rule.rule_contexts |
| 152 | + resources = local.resource_list[count.index] |
| 153 | + operations = local.rule.operations |
| 154 | + |
| 155 | +} |
0 commit comments