-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
145 lines (130 loc) · 5.64 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
locals {
prefix = var.project_prefix != "" ? var.project_prefix : "${random_string.prefix.0.result}"
ssh_key_ids = var.existing_ssh_key != "" ? [data.ibm_is_ssh_key.sshkey[0].id] : [ibm_is_ssh_key.generated_key[0].id]
cos_instance = var.existing_cos_instance != "" ? data.ibm_resource_instance.cos.0.id : null
cos_guid = var.existing_cos_instance != "" ? data.ibm_resource_instance.cos.0.guid : module.cos.cos_instance_guid
zones = length(data.ibm_is_zones.regional.zones)
vpc_zones = {
for zone in range(local.zones) : zone => {
zone = "${var.region}-${zone + 1}"
}
}
frontend_rules = [
for r in var.frontend_rules : {
name = r.name
direction = r.direction
remote = lookup(r, "remote", null)
ip_version = lookup(r, "ip_version", null)
icmp = lookup(r, "icmp", null)
tcp = lookup(r, "tcp", null)
udp = lookup(r, "udp", null)
}
]
tags = [
"provider:ibm",
"workspace:${terraform.workspace}",
]
}
# IF a resource group was not provided, create a new one
module "resource_group" {
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-resource-group.git?ref=v1.0.5"
resource_group_name = var.existing_resource_group == null ? "${local.prefix}-resource-group" : null
existing_resource_group_name = var.existing_resource_group
}
# Generate a random string if a project prefix was not provided
resource "random_string" "prefix" {
count = var.project_prefix != "" ? 0 : 1
length = 4
special = false
upper = false
numeric = false
}
# Generate a new SSH key if one was not provided
resource "tls_private_key" "ssh" {
count = var.existing_ssh_key != "" ? 0 : 1
algorithm = "RSA"
rsa_bits = 4096
}
# Add a new SSH key to the region if one was created
resource "ibm_is_ssh_key" "generated_key" {
count = var.existing_ssh_key != "" ? 0 : 1
name = "${local.prefix}-${var.region}-key"
public_key = tls_private_key.ssh.0.public_key_openssh
resource_group = module.resource_group.resource_group_id
tags = local.tags
}
# Write private key to file if it was generated
resource "null_resource" "create_private_key" {
count = var.existing_ssh_key != "" ? 0 : 1
provisioner "local-exec" {
command = <<-EOT
echo '${tls_private_key.ssh.0.private_key_pem}' > ./'${local.prefix}'.pem
chmod 400 ./'${local.prefix}'.pem
EOT
}
}
module "vpc" {
source = "terraform-ibm-modules/vpc/ibm//modules/vpc"
version = "1.1.1"
create_vpc = true
vpc_name = "${local.prefix}-vpc"
resource_group_id = module.resource_group.resource_group_id
classic_access = var.classic_access
default_address_prefix = var.default_address_prefix
default_network_acl_name = "${local.prefix}-default-network-acl"
default_security_group_name = "${local.prefix}-default-security-group"
default_routing_table_name = "${local.prefix}-default-routing-table"
vpc_tags = local.tags
locations = [local.vpc_zones[0].zone, local.vpc_zones[1].zone, local.vpc_zones[2].zone]
number_of_addresses = "128"
create_gateway = true
subnet_name = "${local.prefix}-frontend-subnet"
public_gateway_name = "${local.prefix}-pub-gw"
gateway_tags = local.tags
}
module "security_group" {
source = "terraform-ibm-modules/vpc/ibm//modules/security-group"
version = "1.1.1"
create_security_group = true
name = "${local.prefix}-frontend-sg"
vpc_id = module.vpc.vpc_id[0]
resource_group_id = module.resource_group.resource_group_id
security_group_rules = local.frontend_rules
}
resource "ibm_is_floating_ip" "example" {
name = "${local.prefix}-${local.vpc_zones[0].zone}-fip"
zone = local.vpc_zones[0].zone
resource_group = module.resource_group.resource_group_id
tags = local.tags
}
module "cos" {
create_cos_instance = var.existing_cos_instance != "" ? false : true
depends_on = [module.vpc]
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-cos?ref=v6.7.0"
resource_group_id = module.resource_group.resource_group_id
region = var.region
bucket_name = "${local.prefix}-${local.vpc_zones[0].zone}-collector-bucket"
create_hmac_key = (var.existing_cos_instance != "" ? false : true)
create_cos_bucket = true
kms_encryption_enabled = false
hmac_key_name = (var.existing_cos_instance != "" ? null : "${local.prefix}-hmac-key")
cos_instance_name = (var.existing_cos_instance != "" ? null : "${local.prefix}-cos-instance")
cos_tags = local.tags
existing_cos_instance_id = (var.existing_cos_instance != "" ? local.cos_instance : null)
}
resource "ibm_iam_authorization_policy" "cos_flowlogs" {
count = var.existing_cos_instance != "" ? 0 : 1
depends_on = [module.cos]
source_service_name = "is"
source_resource_type = "flow-log-collector"
target_service_name = "cloud-object-storage"
target_resource_instance_id = local.cos_guid
roles = ["Writer", "Reader"]
}
resource "ibm_is_flow_log" "frontend_collector" {
depends_on = [ibm_iam_authorization_policy.cos_flowlogs]
name = "${local.prefix}-frontend-subnet-collector"
target = module.vpc.subnet_ids[0]
active = true
storage_bucket = module.cos.bucket_name
}