-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
369 lines (333 loc) · 9.87 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
provider "aws" {
profile = "${var.aws_profile}"
region = "${var.region}"
}
/*
Create a new AWS VPC
*/
resource "aws_vpc" "lab" {
cidr_block = "${var.vpc_cidr}"
enable_dns_hostnames = true
tags {
Name = "VPC for ONTAP Cloud",
"Owned By" = "${var.owner_name}",
"Deployed Using" = "Terraform",
"Designed by" = "www.exospheredata.com"
}
}
resource "aws_subnet" "lab" {
vpc_id = "${aws_vpc.lab.id}"
cidr_block = "${cidrsubnet(var.vpc_cidr, 8, 1)}"
tags {
Name = "Subnet for Infrastructure",
"Owned By" = "${var.owner_name}",
"Deployed Using" = "Terraform",
"Designed by" = "www.exospheredata.com"
}
}
resource "aws_route_table_association" "main" {
subnet_id = "${aws_subnet.lab.id}"
route_table_id = "${aws_vpc.lab.default_route_table_id}"
depends_on = [
"aws_subnet.lab"
]
}
resource "aws_internet_gateway" "gw" {
vpc_id = "${aws_vpc.lab.id}"
depends_on = [
"aws_vpc.lab"
]
tags {
Name = "IGW for ONTAP Cloud VPC",
"Owned By" = "${var.owner_name}",
"Deployed Using" = "Terraform",
"Designed by" = "www.exospheredata.com"
}
}
resource "aws_route" "igw" {
route_table_id = "${aws_vpc.lab.default_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gw.id}"
}
/*
Create a new IAM Role that can be assigned to the OnCommand Cloud Manager
EC2 instance and provide access controls to the connected AWS account.
*/
resource "aws_iam_role" "occm_ec2_role" {
name = "occm_instance_profile_${replace(lower(var.owner_name)," ","_")}"
description = "Grants access to services required by NetApp's OnCommand Cloud Manager"
assume_role_policy = "${file("${path.module}/files/occm-ec2-role.json")}"
}
/*
EC2 Instance Profile based on the provided role
*/
resource "aws_iam_instance_profile" "occm_instance_profile" {
name = "occm_instance_profile_${replace(lower(var.owner_name)," ","_")}"
role = "${aws_iam_role.occm_ec2_role.id}"
depends_on = [
"aws_iam_role.occm_ec2_role"
]
}
/*
Default policy document for privileges requireed by OnCommand Cloud Manager
*/
resource "aws_iam_role_policy" "occm_role_policy" {
name = "occm_instance_profile_${replace(lower(var.owner_name)," ","_")}"
role = "${aws_iam_role.occm_ec2_role.id}"
depends_on = [
"aws_iam_role.occm_ec2_role"
]
policy = "${file("${path.module}/files/occm-role-policy.json")}"
}
/*
New Security group granting Web access and SSH access to the OnCommand Cloud Manager host. Also,
allow all outgoing traffic.
*/
resource "aws_security_group" "occm_access" {
name = "occm_access_${replace(lower(var.owner_name)," ","_")}"
description = "Allow all inbound traffic on ports 80, 443, and 22."
vpc_id = "${aws_subnet.lab.vpc_id}"
depends_on = [
"aws_route.igw"
]
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = "OnCommand Cloud Manager Access",
"Deployed Using" = "Terraform",
"Designed by" = "www.exospheredata.com"
}
}
/*
Launch a new AWS Marketplace instance of Netapp's OnCommand Cloud Manager and assign the newly
created EC2 Instance Profile.
*/
resource "aws_instance" "OCCM" {
depends_on = [
"aws_iam_role_policy.occm_role_policy",
"aws_iam_role.occm_ec2_role",
"aws_security_group.occm_access"
]
ami = "${lookup(var.occm_amis, var.region)}"
instance_type = "t2.medium"
subnet_id = "${aws_subnet.lab.id}"
vpc_security_group_ids = ["${aws_security_group.occm_access.id}"]
key_name = "${var.key_name}"
associate_public_ip_address = "true"
iam_instance_profile = "${aws_iam_instance_profile.occm_instance_profile.id}"
tags {
Name = "OnCommand Cloud Manager",
"Owned By" = "${var.owner_name}",
"Deployed Using" = "Terraform",
"Provisioned Using" = "CHEF",
"Designed by" = "www.exospheredata.com"
}
/*
Establish a connection to the OnCommand Cloud Manager system using
ssh and the private key file provided.
*/
connection {
host = "${aws_instance.OCCM.0.public_ip}"
type = "ssh"
user = "ec2-user"
private_key = "${file("${var.key_file_path}")}"
}
/*
This provisioner set will ensure that we are able to connect to OnCommand Cloud Manager url
before the rest of the provisioning occurs. This fixes a bug where the OCCM server responds
to SSH long before the service is online.
*/
provisioner "remote-exec" {
inline = [
"mkdir -p /tmp/"
]
}
provisioner "file" {
source = "${path.module}/scripts/check_server_health.sh"
destination = "/tmp/check_server_health.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/check_server_health.sh",
"/tmp/check_server_health.sh",
"echo The Instance is ready to be provisioned"
]
}
/*
Establish a connection to the OnCommand Cloud Manager system using ssh and the private
key file provided.
The deployment process of an ONTAP Cloud system can take upwards of 25+ minutes depending
on the state of the AWS Region. We are setting a 60 minute timeout on this connection
due to this potential length.
*/
connection {
host = "${aws_instance.OCCM.0.public_ip}"
type = "ssh"
user = "ec2-user" # OCCM Default username in AWS EC2 instances
private_key = "${file("${var.key_file_path}")}"
timeout = "60m"
}
/*
===========
These steps will only be run when the terraform destroy command is sent
===========
The following will only run on a destroy of the environment and needs to be run
to properly destroy the ONTAP Cloud instance.
*/
provisioner "remote-exec" {
when = "destroy"
inline = [
"rm -rf /tmp/chef",
"rm -rf /tmp/cookbooks.tar.gz",
"mkdir -p /tmp/chef/data_bags/occm",
"mkdir -p /tmp/ontap_cloud_cookbooks"
]
}
/*
Create archive of the cookbooks and dependencies. Upon creation, upload the file to the server and then remove the local copy.
the path_root variable tells the system to look at the top level of the modules and not in the module path.
*/
provisioner "file" {
when = "destroy"
source = "${path.module}/files/Berksfile"
destination = "/tmp/ontap_cloud_cookbooks/Berksfile"
}
/*
Send all interpolated files to the remote server
*/
provisioner "file" {
when = "destroy"
source = "${path.module}/files/solo.rb"
destination = "/tmp/chef/solo.rb"
}
provisioner "file" {
when = "destroy"
content = "${data.template_file.destroy_ontap.rendered}"
destination = "/tmp/chef/dna.json"
}
provisioner "file" {
when = "destroy"
source = "${path.module}/scripts/bootstrap.sh"
destination = "/tmp/chef/bootstrap.sh"
}
/*
Setup the CHEF Data bags
*/
provisioner "file" {
when = "destroy"
content = "${data.template_file.admin_credentials.rendered}"
destination = "/tmp/chef/data_bags/occm/admin_credentials.json"
}
/*
Execute Bootstrap script to apply CHEF configuration and setup.
*/
provisioner "remote-exec" {
when = "destroy"
inline = [
"chmod +x /tmp/chef/bootstrap.sh",
"sudo /tmp/chef/bootstrap.sh",
]
}
}
/*
Deploy ONTAP Cloud system and configuration via CHEF
*/
resource "null_resource" "ontap_cloud" {
triggers {
occm_id = "${aws_instance.OCCM.id}",
converge = "${var.converge}"
}
depends_on = [
"aws_iam_role_policy.occm_role_policy",
"aws_iam_role.occm_ec2_role",
"aws_instance.OCCM"
]
/*
Establish a connection to the OnCommand Cloud Manager system using ssh and the private
key file provided.
The deployment process of an ONTAP Cloud system can take upwards of 25+ minutes depending
on the state of the AWS Region. We are setting a 60 minute timeout on this connection
due to this potential length.
*/
connection {
host = "${aws_instance.OCCM.0.public_ip}"
type = "ssh"
user = "ec2-user" # OCCM Default username in AWS EC2 instances
private_key = "${file("${var.key_file_path}")}"
timeout = "60m"
}
/*
===========
These steps will only be run when the terraform apply command is sent
===========
*/
provisioner "remote-exec" {
inline = [
"rm -rf /tmp/chef",
"rm -rf /tmp/ontap_cloud_cookbooks",
"mkdir -p /tmp/chef/data_bags/occm",
"mkdir -p /tmp/ontap_cloud_cookbooks"
]
}
/*
Send all interpolated files to the remote server
*/
provisioner "file" {
source = "${path.module}/files/Berksfile"
destination = "/tmp/ontap_cloud_cookbooks/Berksfile"
}
provisioner "file" {
source = "${path.module}/files/solo.rb"
destination = "/tmp/chef/solo.rb"
}
provisioner "file" {
content = "${data.template_file.create_ontap.rendered}"
destination = "/tmp/chef/dna.json"
}
provisioner "file" {
source = "${path.module}/scripts/bootstrap.sh"
destination = "/tmp/chef/bootstrap.sh"
}
/*
Setup the CHEF Data bags
*/
provisioner "file" {
content = "${data.template_file.admin_credentials.rendered}"
destination = "/tmp/chef/data_bags/occm/admin_credentials.json"
}
provisioner "file" {
content = "${data.template_file.ontap_credentials.rendered}"
destination = "/tmp/chef/data_bags/occm/${var.ontap_name}.json"
}
/*
Execute Bootstrap script to apply CHEF configuration and setup.
*/
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/chef/bootstrap.sh",
"sudo /tmp/chef/bootstrap.sh",
]
}
}