|
| 1 | +################################################################################## |
| 2 | +# VARIABLES |
| 3 | +################################################################################## |
| 4 | + |
| 5 | +variable "aws_access_key" {} |
| 6 | +variable "aws_secret_key" {} |
| 7 | +variable "private_key_path" {} |
| 8 | +variable "key_name" { |
| 9 | + default = "PluralsightKeys" |
| 10 | +} |
| 11 | +variable "network_address_space" { |
| 12 | + default = "10.1.0.0/16" |
| 13 | +} |
| 14 | +variable "subnet1_address_space" { |
| 15 | + default = "10.1.0.0/24" |
| 16 | +} |
| 17 | +variable "subnet2_address_space" { |
| 18 | + default = "10.1.1.0/24" |
| 19 | +} |
| 20 | + |
| 21 | +################################################################################## |
| 22 | +# PROVIDERS |
| 23 | +################################################################################## |
| 24 | + |
| 25 | +provider "aws" { |
| 26 | + access_key = "${var.aws_access_key}" |
| 27 | + secret_key = "${var.aws_secret_key}" |
| 28 | + region = "us-east-1" |
| 29 | +} |
| 30 | + |
| 31 | +################################################################################## |
| 32 | +# DATA |
| 33 | +################################################################################## |
| 34 | + |
| 35 | +data "aws_availability_zones" "available" {} |
| 36 | + |
| 37 | +################################################################################## |
| 38 | +# RESOURCES |
| 39 | +################################################################################## |
| 40 | + |
| 41 | +# NETWORKING # |
| 42 | +resource "aws_vpc" "vpc" { |
| 43 | + cidr_block = "${var.network_address_space}" |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +resource "aws_internet_gateway" "igw" { |
| 48 | + vpc_id = "${aws_vpc.vpc.id}" |
| 49 | + |
| 50 | +} |
| 51 | + |
| 52 | +resource "aws_subnet" "subnet1" { |
| 53 | + cidr_block = "${var.subnet1_address_space}" |
| 54 | + vpc_id = "${aws_vpc.vpc.id}" |
| 55 | + map_public_ip_on_launch = "true" |
| 56 | + availability_zone = "${data.aws_availability_zones.available.names[0]}" |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +resource "aws_subnet" "subnet2" { |
| 61 | + cidr_block = "${var.subnet2_address_space}" |
| 62 | + vpc_id = "${aws_vpc.vpc.id}" |
| 63 | + map_public_ip_on_launch = "true" |
| 64 | + availability_zone = "${data.aws_availability_zones.available.names[1]}" |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | +# ROUTING # |
| 69 | +resource "aws_route_table" "rtb" { |
| 70 | + vpc_id = "${aws_vpc.vpc.id}" |
| 71 | + |
| 72 | + route { |
| 73 | + cidr_block = "0.0.0.0/0" |
| 74 | + gateway_id = "${aws_internet_gateway.igw.id}" |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +resource "aws_route_table_association" "rta-subnet1" { |
| 79 | + subnet_id = "${aws_subnet.subnet1.id}" |
| 80 | + route_table_id = "${aws_route_table.rtb.id}" |
| 81 | +} |
| 82 | + |
| 83 | +resource "aws_route_table_association" "rta-subnet2" { |
| 84 | + subnet_id = "${aws_subnet.subnet2.id}" |
| 85 | + route_table_id = "${aws_route_table.rtb.id}" |
| 86 | +} |
| 87 | + |
| 88 | +# SECURITY GROUPS # |
| 89 | +resource "aws_security_group" "elb-sg" { |
| 90 | + name = "nginx_elb_sg" |
| 91 | + vpc_id = "${aws_vpc.vpc.id}" |
| 92 | + |
| 93 | + #Allow HTTP from anywhere |
| 94 | + ingress { |
| 95 | + from_port = 80 |
| 96 | + to_port = 80 |
| 97 | + protocol = "tcp" |
| 98 | + cidr_blocks = ["0.0.0.0/0"] |
| 99 | + } |
| 100 | + |
| 101 | + #allow all outbound |
| 102 | + egress { |
| 103 | + from_port = 0 |
| 104 | + to_port = 0 |
| 105 | + protocol = "-1" |
| 106 | + cidr_blocks = ["0.0.0.0/0"] |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +# Nginx security group |
| 111 | +resource "aws_security_group" "nginx-sg" { |
| 112 | + name = "nginx_sg" |
| 113 | + vpc_id = "${aws_vpc.vpc.id}" |
| 114 | + |
| 115 | + # SSH access from anywhere |
| 116 | + ingress { |
| 117 | + from_port = 22 |
| 118 | + to_port = 22 |
| 119 | + protocol = "tcp" |
| 120 | + cidr_blocks = ["0.0.0.0/0"] |
| 121 | + } |
| 122 | + |
| 123 | + # HTTP access from the VPC |
| 124 | + ingress { |
| 125 | + from_port = 80 |
| 126 | + to_port = 80 |
| 127 | + protocol = "tcp" |
| 128 | + cidr_blocks = ["${var.network_address_space}"] |
| 129 | + } |
| 130 | + |
| 131 | + # outbound internet access |
| 132 | + egress { |
| 133 | + from_port = 0 |
| 134 | + to_port = 0 |
| 135 | + protocol = "-1" |
| 136 | + cidr_blocks = ["0.0.0.0/0"] |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +# LOAD BALANCER # |
| 141 | +resource "aws_elb" "web" { |
| 142 | + name = "nginx-elb" |
| 143 | + |
| 144 | + subnets = ["${aws_subnet.subnet1.id}", "${aws_subnet.subnet2.id}"] |
| 145 | + security_groups = ["${aws_security_group.elb-sg.id}"] |
| 146 | + instances = ["${aws_instance.nginx1.id}", "${aws_instance.nginx2.id}"] |
| 147 | + |
| 148 | + listener { |
| 149 | + instance_port = 80 |
| 150 | + instance_protocol = "http" |
| 151 | + lb_port = 80 |
| 152 | + lb_protocol = "http" |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +# INSTANCES # |
| 157 | +resource "aws_instance" "nginx1" { |
| 158 | + ami = "ami-c58c1dd3" |
| 159 | + instance_type = "t2.micro" |
| 160 | + subnet_id = "${aws_subnet.subnet1.id}" |
| 161 | + vpc_security_group_ids = ["${aws_security_group.nginx-sg.id}"] |
| 162 | + key_name = "${var.key_name}" |
| 163 | + |
| 164 | + connection { |
| 165 | + user = "ec2-user" |
| 166 | + private_key = "${file(var.private_key_path)}" |
| 167 | + } |
| 168 | + |
| 169 | + provisioner "remote-exec" { |
| 170 | + inline = [ |
| 171 | + "sudo yum install nginx -y", |
| 172 | + "sudo service nginx start", |
| 173 | + "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" |
| 174 | + ] |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +resource "aws_instance" "nginx2" { |
| 179 | + ami = "ami-c58c1dd3" |
| 180 | + instance_type = "t2.micro" |
| 181 | + subnet_id = "${aws_subnet.subnet2.id}" |
| 182 | + vpc_security_group_ids = ["${aws_security_group.nginx-sg.id}"] |
| 183 | + key_name = "${var.key_name}" |
| 184 | + |
| 185 | + connection { |
| 186 | + user = "ec2-user" |
| 187 | + private_key = "${file(var.private_key_path)}" |
| 188 | + } |
| 189 | + |
| 190 | + provisioner "remote-exec" { |
| 191 | + inline = [ |
| 192 | + "sudo yum install nginx -y", |
| 193 | + "sudo service nginx start", |
| 194 | + "echo '<html><head><title>Green Team Server</title></head><body style=\"background-color:#77A032\"><p style=\"text-align: center;\"><span style=\"color:#FFFFFF;\"><span style=\"font-size:28px;\">Green Team</span></span></p></body></html>' | sudo tee /usr/share/nginx/html/index.html" |
| 195 | + ] |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +################################################################################## |
| 200 | +# OUTPUT |
| 201 | +################################################################################## |
| 202 | + |
| 203 | +output "aws_elb_public_dns" { |
| 204 | + value = "${aws_elb.web.dns_name}" |
| 205 | +} |
0 commit comments