forked from rcove/aws-tgw-r8040
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstances.tf
127 lines (109 loc) · 4.4 KB
/
instances.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
# Create ubuntu instances for the websites (user ubuntu)
data "aws_ami" "ubuntu_ami" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
#############################################
########### Spoke-1 Web Server ##############
#############################################
locals {
website = <<WEBSITE
sudo echo "" > index.html
sudo echo "Greetings!" >> index.html
sudo echo "----------" >> index.html
sudo echo "" >> index.html
sudo echo "This is a web server in Spoke-1!" >> index.html
sudo echo "" >> index.html
sudo nohup busybox httpd -f -p 80 &
sudo sleep 5
WEBSITE
}
resource "aws_instance" "spoke_1_instance" {
ami = data.aws_ami.ubuntu_ami.id
instance_type = var.linux_small_server_size
count = length(data.aws_availability_zones.azs.names)
availability_zone = element(data.aws_availability_zones.azs.names, count.index)
subnet_id = element(aws_subnet.spoke_1_external_subnet.*.id, count.index)
key_name = var.key_name
associate_public_ip_address = "false"
vpc_security_group_ids = [aws_security_group.spoke_1_security_group.id]
user_data = <<-EOF
#!/bin/bash
echo "${local.website}" >> website.sh
chmod +x website.sh
mv ./website.sh /home/ubuntu/
sudo echo "" > index.html
sudo echo "Greetings!" >> index.html
sudo echo "-----App1-----" >> index.html
sudo echo "" >> index.html
sudo echo "This is a web server in ${element(data.aws_availability_zones.azs.names, count.index)}!" >> index.html
sudo echo "" >> index.html
sudo mkdir app1
sudo echo "Web server directed by /app1" >> app1/index.html
sudo nohup busybox httpd -f -p 80 &
sudo sleep 5
EOF
tags = {
Name = "${var.project_name}-Spoke-1 Web Server ${count.index + 1}"
Server = "${var.project_name}-Website"
}
}
/*
#############################################
######## Spoke-1a app2 Web Server ##########
#############################################
resource "aws_instance" "spoke_1a_instance" {
ami = "${data.aws_ami.ubuntu_ami.id}"
instance_type = var.linux_small_server_size
count = "${length(data.aws_availability_zones.azs.names)}"
availability_zone = "${element(data.aws_availability_zones.azs.names, count.index)}"
subnet_id = "${element(aws_subnet.spoke_1a_external_subnet.*.id,count.index)}"
key_name = "${var.key_name}"
associate_public_ip_address = "false"
vpc_security_group_ids = ["${aws_security_group.spoke_1a_security_group.id}"]
user_data = <<-EOF
#!/bin/bash
echo "${local.website}" >> website.sh
chmod +x website.sh
mv ./website.sh /home/ubuntu/
sudo echo "" > index.html
sudo echo "Greetings!" >> index.html
sudo echo "-----app2-----" >> index.html
sudo echo "" >> index.html
sudo echo "This is a web server in ${element(data.aws_availability_zones.azs.names, count.index)}!" >> index.html
sudo echo "" >> index.html
sudo mkdir app2
sudo echo "Web server directed by /app2" >> app2/index.html
sudo nohup busybox httpd -f -p 80 &
sudo sleep 5
EOF
tags {
Name = "${var.project_name}-Spoke-1a Web Server ${count.index+1}"
Server = "${var.project_name}-Website"
}
}
*/
######################################
########### Spoke-2 client ###########
######################################
resource "aws_instance" "spoke_2_instance" {
ami = data.aws_ami.ubuntu_ami.id
instance_type = var.linux_small_server_size
subnet_id = aws_subnet.spoke_2_external_subnet.id
key_name = var.key_name
associate_public_ip_address = "false"
vpc_security_group_ids = [aws_security_group.spoke_2_security_group.id]
tags = {
Name = "${var.project_name}-Spoke-2 Linux"
Dev-Test = "false"
Prod-Test = "true"
}
}