-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
237 lines (201 loc) · 7.76 KB
/
main.tf
File metadata and controls
237 lines (201 loc) · 7.76 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#############################################################################
# © Copyright IBM Corp. 2023, 2023
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#############################################################################
##############################################################################
# Terraform Main IaC
##############################################################################
# Generate random identifier
resource "random_string" "resource_identifier" {
length = 5
upper = false
numeric = false
lower = true
special = false
}
data "ibm_resource_group" "resource_group" {
name = var.resource_group
}
# Create LogDNA
module "logdna" {
count = var.logdna_ingestion_key == "" && var.logdna_integration ? 1 : 0
source = "./modules/logdna"
plan = var.logdna_plan
default_receiver = var.logdna_enable_platform
region = var.region
name = "${var.basename}-${var.logdna_name}"
resource_group_name = var.resource_group
}
locals {
uuid = random_string.resource_identifier.result
ingestion_key = var.logdna_ingestion_key == "" && var.logdna_integration ? module.logdna[0].ingestion_key : var.logdna_ingestion_key
}
# Create VPC
resource "ibm_is_vpc" "sandbox-vpc" {
name = "${var.basename}-vpc-${local.uuid}"
resource_group = data.ibm_resource_group.resource_group.id
default_security_group_name = "${var.basename}-dashboard-sg"
address_prefix_management = length(var.address_prefix_cidrs) != 0 ? "manual" : "auto"
}
resource "ibm_is_vpc_address_prefix" "prefixes" {
count = length(var.address_prefix_cidrs)
name = format("%s-prefix-%d", ibm_is_vpc.sandbox-vpc.name, count.index)
zone = var.zones[count.index]
vpc = ibm_is_vpc.sandbox-vpc.id
cidr = var.address_prefix_cidrs[count.index]
}
# Create one subnet per zone
resource "ibm_is_subnet" "subnets" {
depends_on = [
ibm_is_vpc_address_prefix.prefixes
]
count = length(var.zones)
name = "${var.basename}-subnet-${count.index + 1}-${local.uuid}"
vpc = ibm_is_vpc.sandbox-vpc.id
zone = var.zones[count.index]
total_ipv4_address_count = length(var.address_prefix_cidrs) == 0 ? var.subnet_ipv4_count : null
ipv4_cidr_block = length(var.address_prefix_cidrs) != 0 ? var.subnet_cidrs[count.index] : null
resource_group = data.ibm_resource_group.resource_group.id
public_gateway = ibm_is_public_gateway.gateway[count.index].id
}
# Create one public gateway per zone
resource "ibm_is_public_gateway" "gateway" {
count = length(var.zones)
name = "${var.basename}-pgw-${count.index + 1}-${local.uuid}"
vpc = ibm_is_vpc.sandbox-vpc.id
zone = var.zones[count.index]
resource_group = data.ibm_resource_group.resource_group.id
}
# Create security group rules
# This will allow users to access dashboard api from bastion host
resource "ibm_is_security_group_rule" "dashboard_api_rule" {
group = ibm_is_vpc.sandbox-vpc.default_security_group
direction = "inbound"
remote = ibm_is_security_group.login_sg.id
tcp {
port_min = 8080
port_max = 8080
}
}
# This will allow users to access dashboard ui from bastion host
resource "ibm_is_security_group_rule" "dashboard_ui_rule" {
group = ibm_is_vpc.sandbox-vpc.default_security_group
direction = "inbound"
remote = ibm_is_security_group.login_sg.id
tcp {
port_min = 80
port_max = 80
}
}
# This will allow users to ssh to the dashboard vsi from bastion host.
resource "ibm_is_security_group_rule" "dashboard_ssh_self" {
group = ibm_is_vpc.sandbox-vpc.default_security_group
direction = "inbound"
remote = ibm_is_security_group.login_sg.id
tcp {
port_min = 22
port_max = 22
}
}
resource "ibm_is_security_group_rule" "dashboard_outbound" {
group = ibm_is_vpc.sandbox-vpc.default_security_group
direction = "outbound"
remote = "0.0.0.0/0"
}
# Create Virtual Server Instances
# Get Linux image for Virtual Server creates
data "ibm_is_image" "linux" {
name = "ibm-redhat-9-6-minimal-amd64-1"
}
### Create Trusted Profile
resource "ibm_iam_trusted_profile" "profile_id" {
name = "${var.basename}-trusted-profile-${local.uuid}"
}
resource "ibm_iam_trusted_profile_policy" "vpc_policy" {
profile_id = ibm_iam_trusted_profile.profile_id.id
roles = ["Writer", "Viewer", "Reader", "Editor"]
resources {
service = "is"
}
}
resource "ibm_iam_trusted_profile_policy" "rg_policy" {
profile_id = ibm_iam_trusted_profile.profile_id.id
roles = ["Writer", "Viewer", "Reader", "Editor"]
resources {
resource_type = "resource-group"
resource = data.ibm_resource_group.resource_group.id
}
}
resource "tls_private_key" "generated_key" {
algorithm = "RSA"
rsa_bits = 4096
}
resource "ibm_is_ssh_key" "dynamic_ssh_key" {
name = "${var.ibmcloud_ssh_key_name}-${local.uuid}"
public_key = tls_private_key.generated_key.public_key_openssh
}
resource "local_file" "private_key" {
content = tls_private_key.generated_key.private_key_openssh
filename = "${path.module}/generated_key.pem"
file_permission = "0600"
}
data "http" "schematics_public_ip" {
url = "https://api.ipify.org" # Public IP detection service
}
locals {
schematics_ip = chomp(data.http.schematics_public_ip.response_body)
all_allowed_ips = distinct(concat(var.remote_allowed_ips, [local.schematics_ip]))
}
### Create Dashboard VM ####
resource "ibm_is_instance" "dashboard-vm" {
count = 1
name = "${var.basename}-dashboard-${local.uuid}"
image = data.ibm_is_image.linux.id
resource_group = data.ibm_resource_group.resource_group.id
profile = var.dashboard-machine-type
primary_network_interface {
subnet = ibm_is_subnet.subnets[count.index % length(var.zones)].id
security_groups = [ibm_is_vpc.sandbox-vpc.default_security_group]
}
default_trusted_profile_target = ibm_iam_trusted_profile.profile_id.id
metadata_service {
enabled = true
response_hop_limit = 2
}
vpc = ibm_is_vpc.sandbox-vpc.id
zone = element(var.zones, count.index)
keys = [var.ibmcloud_ssh_key_id, ibm_is_ssh_key.dynamic_ssh_key.id]
user_data = templatefile("${path.module}/scripts/dashboard-userdata.sh", {
"ingestion_key" = local.ingestion_key
"region" = var.region
"iam_trustedprofile" = ibm_iam_trusted_profile.profile_id.id
"sandbox_uipassword" = var.sandbox_uipassword
"personal_access_token" = var.personal_access_token
"sandbox_ui_repo_url" = var.sandbox_ui_repo_url
"bastion_ssh_key_id" = var.ibmcloud_ssh_key_id
})
}
# null resource to wait for the dashboard VM to be ready
resource "null_resource" "check_status" {
provisioner "local-exec" {
command = "${path.module}/scripts/check_status.sh"
interpreter = ["bash", "-c"]
environment = {
LOCAL_KEY_PATH = local_file.private_key.filename
BASTION_USER = "vpcuser"
BASTION_HOST = local.floating_ip
DASHBOARD_USER = "vpcuser"
DASHBOARD_IP = local.dashboardVM_address
KEY_NAME = ibm_is_ssh_key.dynamic_ssh_key.name
}
}
depends_on = [ibm_is_instance.dashboard-vm]
}