-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo2-start.tf
149 lines (122 loc) · 4.08 KB
/
demo2-start.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
##################################################################################
# VARIABLES
##################################################################################
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "private_key_path" {}
variable "key_name" {
default = "demo_test1"
}
variable "network_address_space" {
default = "10.1.0.0/16"
}
variable "subnet1_address_space" {
default = "10.1.0.0/24"
}
variable "subnet2_address_space" {
default = "10.1.1.0/24"
}
##################################################################################
# PROVIDERS
##################################################################################
provider "aws" {
access_key = "${var.aws_access_key}"
secret_key = "${var.aws_secret_key}"
region = "us-east-1"
}
##################################################################################
# DATA
##################################################################################
data "aws_availability_zones" "available" {}
##################################################################################
# RESOURCES
##################################################################################
# NETWORKING #
resource "aws_vpc" "vpc" {
cidr_block = "${var.network_address_space}"
enable_dns_hostnames = "true"
}
resource "aws_internet_gateway" "igw" {
vpc_id = "${aws_vpc.vpc.id}"
}
resource "aws_subnet" "subnet1" {
cidr_block = "${var.subnet1_address_space}"
vpc_id = "${aws_vpc.vpc.id}"
map_public_ip_on_launch = "true"
availability_zone = "${data.aws_availability_zones.available.names[0]}"
}
resource "aws_subnet" "subnet2" {
cidr_block = "${var.subnet2_address_space}"
vpc_id = "${aws_vpc.vpc.id}"
map_public_ip_on_launch = "true"
availability_zone = "${data.aws_availability_zones.available.names[1]}"
}
# ROUTING #
resource "aws_route_table" "rtb" {
vpc_id = "${aws_vpc.vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.igw.id}"
}
}
resource "aws_route_table_association" "rta-subnet1" {
subnet_id = "${aws_subnet.subnet1.id}"
route_table_id = "${aws_route_table.rtb.id}"
}
resource "aws_route_table_association" "rta-subnet2" {
subnet_id = "${aws_subnet.subnet2.id}"
route_table_id = "${aws_route_table.rtb.id}"
}
# SECURITY GROUPS #
# Nginx security group
resource "aws_security_group" "nginx-sg" {
name = "nginx_sg"
vpc_id = "${aws_vpc.vpc.id}"
# SSH access from anywhere
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# HTTP access from anywhere
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# outbound internet access
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# INSTANCES #
resource "aws_instance" "nginx1" {
ami = "ami-c58c1dd3"
instance_type = "t2.micro"
subnet_id = "${aws_subnet.subnet1.id}"
vpc_security_group_ids = ["${aws_security_group.nginx-sg.id}"]
key_name = "${var.key_name}"
connection {
user = "ec2-user"
host = self.associate_public_ip_address
private_key = "${file(var.private_key_path)}"
}
provisioner "remote-exec" {
inline = [
"sudo yum install nginx -y",
"sudo service nginx start",
"echo '<html><head><title>Blue Team Server</title></head><body style=\"background-color:#1F778D\"><p style=\"text-align: center;\"><span style=\"color:#FFFFFF;\"><span style=\"font-size:28px;\">Blue Team</span></span></p></body></html>' | sudo tee /usr/share/nginx/html/index.html"
]
}
}
##################################################################################
# OUTPUT
##################################################################################
output "aws_instance_public_dns" {
value = "${aws_instance.nginx1.public_dns}"
}