-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.tf
190 lines (154 loc) · 5.37 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
provider "aws" {
region = "${var.aws_region}"
profile = "${var.aws_profile}"
}
# Create VPC
resource "aws_vpc" "auar_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags {
Name = "automate-up-and-running-training-vpc"
}
}
# Create Internet Gateway
resource "aws_internet_gateway" "auar_internet_gateway" {
vpc_id = "${aws_vpc.auar_vpc.id}"
tags {
Name = "automate-up-and-running-training-gateway"
}
}
# Create Route to the Internet
resource "aws_route" "auar_internet_access" {
route_table_id = "${aws_vpc.auar_vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.auar_internet_gateway.id}"
}
# Create VPC Subnet
resource "aws_subnet" "auar_public_subnet" {
vpc_id = "${aws_vpc.auar_vpc.id}"
cidr_block = "10.0.0.0/24"
map_public_ip_on_launch = true
tags {
Name = "automate-up-and-running-training-subnet"
}
}
# Create All-Open Security Group
resource "aws_security_group" "auar_allow_all" {
name = "auar_allow_all"
description = "Chef Automate Up and Running Security Group - Insecure"
vpc_id = "${aws_vpc.auar_vpc.id}"
tags {
Name = "automate-up-and-running-training-security-group"
}
}
resource "aws_security_group_rule" "ingress_allow_all" {
type = "ingress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.auar_allow_all.id}"
}
resource "aws_security_group_rule" "egress_allow_all" {
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = "${aws_security_group.auar_allow_all.id}"
}
# Create Student Automate Servers
resource "aws_instance" "student_automate_server" {
connection {
type = "ssh"
user = "${var.aws_ami_user}"
private_key = "${file("${var.aws_key_path}")}"
}
ami = "${var.aws_ami}"
count = "${var.student_count}"
instance_type = "${var.student_automate_instance_type}"
key_name = "${var.aws_key_name}"
subnet_id = "${aws_subnet.auar_public_subnet.id}"
vpc_security_group_ids = ["${aws_security_group.auar_allow_all.id}"]
provisioner "file" {
source = "./cookbooks"
destination = "/tmp/cookbooks"
}
provisioner "local-exec" {
command = "./update_dns.rb create \"${element(var.card_table, count.index)}.automate\" \"${self.public_ip}\""
}
provisioner "local-exec" {
command = "./update_dns.rb delete \"${element(var.card_table, count.index)}.automate\""
when = "destroy"
}
provisioner "remote-exec" {
inline = [
"sudo hostnamectl set-hostname ${element(var.card_table, count.index)}.automate.e9.io",
"sudo /bin/rpm -Uv ${var.chefdk_url}",
"sudo /bin/chef-solo -c /tmp/cookbooks/solo.rb -o recipe[automate_lab],recipe[automate_lab::student_automate]"
]
}
root_block_device {
volume_type = "gp2"
volume_size = 30
delete_on_termination = true
}
tags {
Name = "automate-up-and-running-training-${element(var.card_table, count.index)}-automate-server",
X-Contact = "[email protected]",
X-Dept = "Training"
}
}
# Create Student Workstations
resource "aws_instance" "student_workstation" {
connection {
type = "ssh"
user = "${var.aws_ami_user}"
private_key = "${file("${var.aws_key_path}")}"
}
ami = "${var.aws_ami}"
count = "${var.student_count}"
instance_type = "${var.student_workstation_instance_type}"
key_name = "${var.aws_key_name}"
subnet_id = "${aws_subnet.auar_public_subnet.id}"
vpc_security_group_ids = ["${aws_security_group.auar_allow_all.id}"]
provisioner "file" {
source = "./cookbooks"
destination = "/tmp/cookbooks"
}
provisioner "local-exec" {
command = "./update_dns.rb create \"${element(var.card_table, count.index)}.workstation\" \"${self.public_ip}\""
}
provisioner "local-exec" {
command = "./update_dns.rb delete \"${element(var.card_table, count.index)}.workstation\""
when = "destroy"
}
provisioner "remote-exec" {
inline = [
"sudo hostnamectl set-hostname ${element(var.card_table, count.index)}.workstation.e9.io",
"sudo /bin/rpm -Uv ${var.chefdk_url}",
"sudo /bin/chef-solo -c /tmp/cookbooks/solo.rb -o recipe[automate_lab],recipe[automate_lab::student_workstation]"
]
}
provisioner "file" {
destination = "/tmp/config.rb"
content = <<EOL
node_name "${element(var.card_table, count.index)}.workstation.e9.io"
data_collector.server_url "https://${element(var.card_table, count.index)}.automate.e9.io/data-collector/v0/"
data_collector.token "93a49a4f2482c64126f7b6015e6b0f30284287ee4054ff8807fb63d9cbd1c506"
ssl_verify_mode :verify_none
verify_api_cert false
EOL
}
root_block_device {
volume_type = "gp2"
volume_size = 30
delete_on_termination = true
}
tags {
Name = "automate-up-and-running-training-${element(var.card_table, count.index)}-workstation"
X-Contact = "[email protected]",
X-Dept = "Training"
}
}