Skip to content

Commit b8cd734

Browse files
authored
feat: add working examples using root module (#289)
1 parent 6fd5fd8 commit b8cd734

File tree

15 files changed

+369
-112
lines changed

15 files changed

+369
-112
lines changed

.gitmodules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[submodule "common-dev-assets"]
22
path = common-dev-assets
33
url = https://github.com/terraform-ibm-modules/common-dev-assets
4+
branch = main

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ statement instead the previous block.
136136
| Name | Version |
137137
|------|---------|
138138
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >=1.3 |
139-
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | ~>1.43.0 |
139+
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >=1.43.0 |
140140

141141
## Modules
142142

@@ -161,7 +161,7 @@ No modules.
161161
| <a name="input_resource_group_id"></a> [resource\_group\_id](#input\_resource\_group\_id) | ID of the resource group where endpoint gateways will be provisioned | `string` | `null` | no |
162162
| <a name="input_security_group_ids"></a> [security\_group\_ids](#input\_security\_group\_ids) | List of security group ids to attach to each endpoint gateway. | `list(string)` | `null` | no |
163163
| <a name="input_service_endpoints"></a> [service\_endpoints](#input\_service\_endpoints) | Service endpoints to use to create endpoint gateways. Can be `public`, or `private`. | `string` | `"private"` | no |
164-
| <a name="input_subnet_zone_list"></a> [subnet\_zone\_list](#input\_subnet\_zone\_list) | List of subnets in the VPC where gateways and reserved IPs will be provisioned. This value is intended to use the `subnet_zone_list` output from the ICSE VPC Subnet Module (https://github.com/Cloud-Schematics/vpc-subnet-module) or from templates using that module for subnet creation. | <pre>list(<br> object({<br> name = string<br> id = string<br> zone = optional(string)<br> cidr = optional(string)<br> })<br> )</pre> | `[]` | no |
164+
| <a name="input_subnet_zone_list"></a> [subnet\_zone\_list](#input\_subnet\_zone\_list) | List of subnets in the VPC where gateways and reserved IPs will be provisioned. This value is intended to use the `subnet_zone_list` output from the Landing Zone VPC Subnet Module (https://github.com/terraform-ibm-modules/terraform-ibm-landing-zone-vpc) or from templates using that module for subnet creation. | <pre>list(<br> object({<br> name = string<br> id = string<br> zone = optional(string)<br> cidr = optional(string)<br> })<br> )</pre> | `[]` | no |
165165
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | ID of the VPC where the Endpoint Gateways will be created | `string` | `null` | no |
166166
| <a name="input_vpc_name"></a> [vpc\_name](#input\_vpc\_name) | Name of the VPC where the Endpoint Gateways will be created. This value is used to dynamically generate VPE names. | `string` | `"vpc"` | no |
167167

examples/default/main.tf

Lines changed: 33 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,53 @@
11
##############################################################################
2-
# VPE Locals
2+
# Resource Group
33
##############################################################################
4-
5-
locals {
6-
# List of Gateways to create
7-
gateway_list = var.vpc_id == null ? [] : concat([
8-
# Create object for each service
9-
for service in var.cloud_services :
10-
{
11-
name = "${var.vpc_name}-${service}"
12-
service = service
13-
crn = null
14-
}
15-
],
16-
[
17-
for service in var.cloud_service_by_crn :
18-
{
19-
name = "${var.vpc_name}-${service.name}"
20-
service = null
21-
crn = service.crn
22-
}
23-
]
24-
)
25-
26-
# List of IPs to create
27-
endpoint_ip_list = var.vpc_id == null ? [] : flatten([
28-
# Create object for each subnet
29-
for subnet in var.subnet_zone_list :
30-
[
31-
for service in var.cloud_services :
32-
{
33-
ip_name = "${subnet.name}-${service}-gateway-${replace(subnet.zone, "/${var.region}-/", "")}-ip"
34-
subnet_id = subnet.id
35-
gateway_name = "${var.vpc_name}-${service}"
36-
}
37-
]
38-
])
39-
40-
# Map of Services to endpoints
41-
service_to_endpoint_map = {
42-
kms = "crn:v1:bluemix:public:kms:${var.region}:::endpoint:${var.service_endpoints}.${var.region}.kms.cloud.ibm.com"
43-
hs-crypt = "crn:v1:bluemix:public:hs-crypto:${var.region}:::endpoint:api.${var.service_endpoints}.${var.region}.hs-crypto.cloud.ibm.com"
44-
cloud-object-storage = "crn:v1:bluemix:public:cloud-object-storage:global:::endpoint:s3.direct.${var.region}.cloud-object-storage.appdomain.cloud"
45-
container-registry = "crn:v1:bluemix:public:container-registry:${var.region}:::endpoint:vpe.${var.region}.container-registry.cloud.ibm.com"
46-
}
4+
module "resource_group" {
5+
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-resource-group.git?ref=v1.0.5"
6+
# if an existing resource group is not set (null) create a new one using prefix
7+
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
8+
existing_resource_group_name = var.resource_group
479
}
4810

4911
##############################################################################
5012

5113
##############################################################################
52-
# Create Reserved IPs
14+
# Create VPC
5315
##############################################################################
5416

55-
resource "ibm_is_subnet_reserved_ip" "ip" {
56-
for_each = {
57-
# Create a map based on endpoint IP name
58-
for gateway_ip in local.endpoint_ip_list :
59-
(gateway_ip.ip_name) => gateway_ip
60-
}
61-
subnet = each.value.subnet_id
17+
locals {
18+
# input variable validation
19+
# tflint-ignore: terraform_unused_declarations
20+
validate_vpc_inputs = var.vpc_id == null && !var.create_vpc ? tobool("var.create_vpc should be set to true if var.vpc_id is set to null") : true
21+
# tflint-ignore: terraform_unused_declarations
22+
validate_vpc_id_and_create_vpc_both_not_set_inputs = var.vpc_id != null && var.create_vpc ? tobool("var.vpc_id cannot be set whilst var.create_vpc is set to true") : true
23+
vpc_instance_id = var.vpc_id == null ? tolist(ibm_is_vpc.vpc[*].id)[0] : var.vpc_id
6224
}
6325

6426
##############################################################################
65-
66-
##############################################################################
67-
# Create Endpoint Gateways
27+
# Create a VPC for this example
6828
##############################################################################
6929

70-
resource "ibm_is_virtual_endpoint_gateway" "vpe" {
71-
for_each = {
72-
# Create map based on gateway name if enabled
73-
for gateway in local.gateway_list :
74-
(gateway.name) => gateway
75-
}
76-
77-
name = "${var.prefix}-${each.key}-endpoint-gateway"
78-
vpc = var.vpc_id
79-
resource_group = var.resource_group_id
80-
security_groups = var.security_group_ids
81-
target {
82-
crn = each.value.service == null ? each.value.crn : local.service_to_endpoint_map[each.value.service]
83-
resource_type = "provider_cloud_service"
84-
}
30+
resource "ibm_is_vpc" "vpc" {
31+
count = var.create_vpc ? 1 : 0
32+
name = "${var.prefix}-${var.vpc_name}"
33+
resource_group = module.resource_group.resource_group_id
8534
}
8635

8736
##############################################################################
88-
89-
##############################################################################
90-
# Attach Endpoint Gateways to Reserved IPs
91-
##############################################################################
92-
93-
resource "ibm_is_virtual_endpoint_gateway_ip" "endpoint_gateway_ip" {
94-
for_each = {
95-
# Create a map based on endpoint IP
96-
for gateway_ip in local.endpoint_ip_list :
97-
(gateway_ip.ip_name) => gateway_ip
98-
}
99-
gateway = ibm_is_virtual_endpoint_gateway.vpe[each.value.gateway_name].id
100-
reserved_ip = ibm_is_subnet_reserved_ip.ip[each.key].reserved_ip
37+
# Create VPEs in the VPC
38+
##############################################################################
39+
module "vpes" {
40+
source = "../../"
41+
region = var.region
42+
prefix = var.prefix
43+
vpc_name = var.vpc_name
44+
vpc_id = local.vpc_instance_id
45+
subnet_zone_list = var.subnet_zone_list
46+
resource_group_id = module.resource_group.resource_group_id
47+
security_group_ids = var.security_group_ids
48+
cloud_services = var.cloud_services
49+
cloud_service_by_crn = var.cloud_service_by_crn
50+
service_endpoints = var.service_endpoints
10151
}
10252

10353
##############################################################################

examples/default/provider.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "ibm" {
2+
ibmcloud_api_key = var.ibmcloud_api_key
3+
region = var.region
4+
}

examples/default/variables.tf

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
##############################################################################
2-
# VPC Variables
3-
##############################################################################
1+
variable "ibmcloud_api_key" {
2+
type = string
3+
description = "The IBM Cloud API Key"
4+
sensitive = true
5+
}
46

57
variable "region" {
68
description = "The region where VPC and services are deployed"
@@ -14,10 +16,20 @@ variable "prefix" {
1416
default = "vpe"
1517
}
1618

19+
variable "resource_group" {
20+
type = string
21+
description = "An existing resource group name to use for this example, if unset a new resource group will be created"
22+
default = null
23+
}
24+
25+
##############################################################################
26+
# VPC Variables
27+
##############################################################################
28+
1729
variable "vpc_name" {
1830
description = "Name of the VPC where the Endpoint Gateways will be created. This value is used to dynamically generate VPE names."
1931
type = string
20-
default = "vpc"
32+
default = "my-vpc-instance"
2133
}
2234

2335
variable "vpc_id" {
@@ -26,6 +38,18 @@ variable "vpc_id" {
2638
default = null
2739
}
2840

41+
variable "create_vpc" {
42+
description = "Create a VPC instance."
43+
type = bool
44+
default = true
45+
}
46+
47+
##############################################################################
48+
49+
##############################################################################
50+
# VPE Variables
51+
##############################################################################
52+
2953
variable "subnet_zone_list" {
3054
description = "List of subnets in the VPC where gateways and reserved IPs will be provisioned. This value is intended to use the `subnet_zone_list` output from the ICSE VPC Subnet Module (https://github.com/Cloud-Schematics/vpc-subnet-module) or from templates using that module for subnet creation."
3155
type = list(
@@ -39,18 +63,6 @@ variable "subnet_zone_list" {
3963
default = []
4064
}
4165

42-
##############################################################################
43-
44-
##############################################################################
45-
# VPE Variables
46-
##############################################################################
47-
48-
variable "resource_group_id" {
49-
description = "ID of the resource group where endpoint gateways will be provisioned"
50-
type = string
51-
default = null
52-
}
53-
5466
variable "security_group_ids" {
5567
description = "List of security group ids to attach to each endpoint gateway."
5668
type = list(string)

examples/default/versions.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
##############################################################################
44

55
terraform {
6+
required_version = ">= 1.3.0"
67
required_providers {
8+
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
79
ibm = {
810
source = "IBM-Cloud/ibm"
9-
version = "~>1.43.0"
11+
version = "1.51.0"
1012
}
1113
}
12-
required_version = ">=1.3"
1314
}
1415

1516
##############################################################################

examples/security-group/main.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
##############################################################################
2+
# Resource Group
3+
##############################################################################
4+
module "resource_group" {
5+
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-resource-group.git?ref=v1.0.5"
6+
# if an existing resource group is not set (null) create a new one using prefix
7+
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
8+
existing_resource_group_name = var.resource_group
9+
}
10+
11+
##############################################################################
12+
13+
##############################################################################
14+
# Create VPC
15+
##############################################################################
16+
17+
locals {
18+
# input variable validation
19+
# tflint-ignore: terraform_unused_declarations
20+
validate_vpc_inputs = var.vpc_id == null && !var.create_vpc ? tobool("var.create_vpc should be set to true if var.vpc_id is set to null") : true
21+
# tflint-ignore: terraform_unused_declarations
22+
validate_vpc_id_and_create_vpc_both_not_set_inputs = var.vpc_id != null && var.create_vpc ? tobool("var.vpc_id cannot be set whilst var.create_vpc is set to true") : true
23+
vpc_instance_id = var.vpc_id == null ? tolist(ibm_is_vpc.vpc[*].id)[0] : var.vpc_id
24+
}
25+
26+
##############################################################################
27+
# Create a VPC for this example
28+
##############################################################################
29+
30+
resource "ibm_is_vpc" "vpc" {
31+
count = var.create_vpc ? 1 : 0
32+
name = "${var.prefix}-${var.vpc_name}"
33+
resource_group = module.resource_group.resource_group_id
34+
}
35+
36+
##############################################################################
37+
# Update security group
38+
##############################################################################
39+
40+
module "create_sgr_rule" {
41+
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-security-group.git?ref=v1.0.0"
42+
add_ibm_cloud_internal_rules = var.add_ibm_cloud_internal_rules
43+
security_group_name = "${var.prefix}-1"
44+
security_group_rules = var.security_group_rules
45+
resource_group = module.resource_group.resource_group_id
46+
vpc_id = local.vpc_instance_id
47+
}
48+
49+
##############################################################################
50+
# Create VPEs in the VPC
51+
##############################################################################
52+
module "vpes" {
53+
source = "../../"
54+
region = var.region
55+
prefix = var.prefix
56+
vpc_name = var.vpc_name
57+
vpc_id = local.vpc_instance_id
58+
subnet_zone_list = var.subnet_zone_list
59+
cloud_services = var.cloud_services
60+
cloud_service_by_crn = var.cloud_service_by_crn
61+
service_endpoints = var.service_endpoints
62+
security_group_ids = [module.create_sgr_rule.security_group_id]
63+
resource_group_id = module.resource_group.resource_group_id
64+
}
65+
66+
##############################################################################

examples/security-group/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
##############################################################################
2+
# Please open an issue to suggest outputs for this module
3+
##############################################################################
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "ibm" {
2+
ibmcloud_api_key = var.ibmcloud_api_key
3+
region = var.region
4+
}

0 commit comments

Comments
 (0)