-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.tf
215 lines (170 loc) · 6.51 KB
/
instance.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
resource "aws_instance" "chef-server" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
instance_type = "t2.2xlarge"
root_block_device {
volume_type = "gp2"
volume_size = "100"
delete_on_termination = "true"
}
tags {
Name = "${var.chef_server_name}"
Department = "organization"
}
iam_instance_profile = "${aws_iam_instance_profile.tr_s3_instance_profile.id}"
user_data = <<HEREDOC
#!/bin/bash
sudo yum update
sudo yum -y install curl
echo $(curl -s http://169.254.169.254/latest/meta-data/public-hostname) | xargs sudo hostname
# create staging directories
if [ ! -d /drop ]; then
mkdir /drop
fi
if [ ! -d /downloads ]; then
mkdir /downloads
fi
# download the Chef server package
if [ ! -f /downloads/chef-server-core_12.17.33_amd64.rpm ]; then
echo "Downloading the Chef server package..."
wget -nv -P /downloads https://packages.chef.io/files/stable/chef-server/12.17.33/el/7/chef-server-core-12.17.33-1.el7.x86_64.rpm
wget -nv -P /downloads https://packages.chef.io/files/stable/chef-manage/2.5.16/el/7/chef-manage-2.5.16-1.el7.x86_64.rpm
fi
# install Chef server
if [ ! $(which chef-server-ctl) ]; then
echo "Installing Chef server..."
rpm -Uvh /downloads/chef-server-core-12.17.33-1.el7.x86_64.rpm
sudo chef-server-ctl reconfigure
echo "Waiting for services..."
until (curl -D - http://localhost:8000/_status) | grep "200 OK"; do sleep 15s; done
while (curl http://localhost:8000/_status) | grep "fail"; do sleep 15s; done
echo "Creating initial user and organization..."
sudo chef-server-ctl user-create chefadmin Chef Admin [email protected] Island988876 --filename /drop/chefadmin.pem
echo $(curl -s http://169.254.169.254/latest/meta-data/local-hostname)>/drop/hostname.txt
aws s3 cp /drop/chefadmin.pem s3://chef-automate-novpc/
aws s3 cp /drop/hostname.txt s3://chef-automate-novpc/
sudo chef-server-ctl org-create myorg "myorg" --association_user chefadmin --filename myorg.pem
sleep 5
rpm -Uvh /downloads/chef-manage-2.5.16-1.el7.x86_64.rpm
sudo chef-server-ctl reconfigure
sudo chef-manage-ctl reconfigure
sudo ACCEPT_EULA=Yes
fi
echo "Your Chef server is ready!"
HEREDOC
iam_instance_profile = "${aws_iam_instance_profile.tr_s3_instance_profile.id}"
# the VPC subnet
subnet_id = "${var.public_subnet_id_1}"
# the security group
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
# the public SSH key
key_name = "${aws_key_pair.mykeypair.key_name}"
}
resource "aws_instance" "automate-server" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
instance_type = "t2.2xlarge"
root_block_device {
volume_type = "gp2"
volume_size = "100"
delete_on_termination = "true"
}
tags {
Name = "${var.automate_server_name}"
Department = "organization"
}
iam_instance_profile = "${aws_iam_instance_profile.tr_s3_instance_profile.id}"
user_data = <<HEREDOC
#!/bin/bash -x
sudo yum update
sudo yum -y install curl
echo $(curl -s http://169.254.169.254/latest/meta-data/public-hostname) | xargs sudo hostname
# create downloads directory
if [ ! -d /downloads ]; then
mkdir /downloads
fi
# download the Chef Automate package
if [ ! -f /downloads/automate_1.8.38-1_amd64.rpm ]; then
echo "Downloading the Chef Automate package..."
wget -nv -P /downloads https://packages.chef.io/files/stable/automate/1.8.68/el/7/automate-1.8.68-1.el7.x86_64.rpm
fi
# install Chef Automate
if [ ! $(which automate-ctl) ]; then
echo "Installing Chef Automate..."
rpm -Uvh /downloads/automate-1.8.68-1.el7.x86_64.rpm
# run preflight check
sudo automate-ctl preflight-check
# run setup
sleep 300
aws s3 cp s3://ist-chef-license/hostname.txt /tmp/
chef_server_fqdn=$(cat /tmp/hostname.txt)
aws s3 cp s3://chef-automate-novpc/chefadmin.pem /tmp/
aws s3 cp s3://chef-automate-novpc/automate.license /tmp/
sleep 30
sudo automate-ctl setup --license /tmp/automate.license --key /tmp/chefadmin.pem --server-url https://$chef_server_fqdn/organizations/ist --fqdn $(hostname) --enterprise default --configure --no-build-node
sudo automate-ctl reconfigure
# wait for all services to come online
echo "Waiting for services..."
until (curl --insecure -D - https://localhost/api/_status) | grep "200 OK"; do sleep 1m && automate-ctl restart; done
while (curl --insecure https://localhost/api/_status) | grep "fail"; do sleep 15s; done
# create an initial user
echo "Creating chefadmin user..."
sudo automate-ctl create-user default chefadmin --password Island988876 --roles "admin"
fi
echo "Your Chef Automate server is ready!"
HEREDOC
# the VPC subnet
subnet_id = "${var.public_subnet_id_2}"
# the security group
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
# the public SSH key
key_name = "${aws_key_pair.mykeypair.key_name}"
}
resource "aws_instance" "runner1" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
instance_type = "t2.micro"
root_block_device {
volume_type = "gp2"
volume_size = "20"
delete_on_termination = "true"
}
tags {
Name = "${var.runner_name_1}"
Department = "organization"
}
iam_instance_profile = "${aws_iam_instance_profile.tr_s3_instance_profile.id}"
user_data = <<HEREDOC
#!/bin/bash
sudo yum update -y
echo $(curl -s http://169.254.169.254/latest/meta-data/local-hostname) | xargs sudo hostname
HEREDOC
# the VPC subnet
subnet_id = "${var.private_subnet_id_2}"
# the security group
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
# the public SSH key
key_name = "${aws_key_pair.mykeypair.key_name}"
}
resource "aws_instance" "runner2" {
ami = "${lookup(var.AMIS, var.AWS_REGION)}"
instance_type = "t2.micro"
root_block_device {
volume_type = "gp2"
volume_size = "20"
delete_on_termination = "true"
}
tags {
Name = "${var.runner_name_2}"
Department = "organization"
}
iam_instance_profile = "${aws_iam_instance_profile.tr_s3_instance_profile.id}"
user_data = <<HEREDOC
#!/bin/bash
sudo yum update -y
echo $(curl -s http://169.254.169.254/latest/meta-data/local-hostname) | xargs sudo hostname
HEREDOC
# the VPC subnet
subnet_id = "${var.private_subnet_id_1}"
# the security group
vpc_security_group_ids = ["${var.vpc_security_group_ids}"]
# the public SSH key
key_name = "${aws_key_pair.mykeypair.key_name}"
}