Skip to content

Commit 21ec7d6

Browse files
author
Shohre Mansouri
committed
Add more samples
1 parent a6a7dce commit 21ec7d6

4 files changed

+706
-0
lines changed

Sample2/moduletwo-start.tf

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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 = "deployer-key"
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 = "ap-southeast-2"
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+
enable_dns_hostnames = "true"
45+
46+
}
47+
48+
resource "aws_internet_gateway" "igw" {
49+
vpc_id = "${aws_vpc.vpc.id}"
50+
51+
}
52+
53+
resource "aws_subnet" "subnet1" {
54+
cidr_block = "${var.subnet1_address_space}"
55+
vpc_id = "${aws_vpc.vpc.id}"
56+
map_public_ip_on_launch = "true"
57+
availability_zone = "${data.aws_availability_zones.available.names[0]}"
58+
59+
}
60+
61+
resource "aws_subnet" "subnet2" {
62+
cidr_block = "${var.subnet2_address_space}"
63+
vpc_id = "${aws_vpc.vpc.id}"
64+
map_public_ip_on_launch = "true"
65+
availability_zone = "${data.aws_availability_zones.available.names[1]}"
66+
67+
}
68+
69+
# ROUTING #
70+
resource "aws_route_table" "rtb" {
71+
vpc_id = "${aws_vpc.vpc.id}"
72+
73+
route {
74+
cidr_block = "0.0.0.0/0"
75+
gateway_id = "${aws_internet_gateway.igw.id}"
76+
}
77+
}
78+
79+
resource "aws_route_table_association" "rta-subnet1" {
80+
subnet_id = "${aws_subnet.subnet1.id}"
81+
route_table_id = "${aws_route_table.rtb.id}"
82+
}
83+
84+
resource "aws_route_table_association" "rta-subnet2" {
85+
subnet_id = "${aws_subnet.subnet2.id}"
86+
route_table_id = "${aws_route_table.rtb.id}"
87+
}
88+
89+
# SECURITY GROUPS #
90+
# Nginx security group
91+
resource "aws_security_group" "nginx-sg" {
92+
name = "nginx_sg"
93+
vpc_id = "${aws_vpc.vpc.id}"
94+
95+
# SSH access from anywhere
96+
ingress {
97+
from_port = 22
98+
to_port = 22
99+
protocol = "tcp"
100+
cidr_blocks = ["0.0.0.0/0"]
101+
}
102+
103+
# HTTP access from anywhere
104+
ingress {
105+
from_port = 80
106+
to_port = 80
107+
protocol = "tcp"
108+
cidr_blocks = ["0.0.0.0/0"]
109+
}
110+
111+
# outbound internet access
112+
egress {
113+
from_port = 0
114+
to_port = 0
115+
protocol = "-1"
116+
cidr_blocks = ["0.0.0.0/0"]
117+
}
118+
}
119+
120+
# INSTANCES #
121+
resource "aws_instance" "nginx1" {
122+
ami = "ami-e428d986"
123+
instance_type = "t2.micro"
124+
subnet_id = "${aws_subnet.subnet1.id}"
125+
vpc_security_group_ids = ["${aws_security_group.nginx-sg.id}"]
126+
key_name = "${var.key_name}"
127+
128+
connection {
129+
user = "ec2-user"
130+
private_key = "${file(var.private_key_path)}"
131+
}
132+
133+
provisioner "remote-exec" {
134+
inline = [
135+
"sudo yum install nginx -y",
136+
"sudo service nginx start",
137+
"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"
138+
]
139+
}
140+
}
141+
142+
##################################################################################
143+
# OUTPUT
144+
##################################################################################
145+
146+
output "aws_instance_public_dns" {
147+
value = "${aws_instance.nginx1.public_dns}"
148+
}

Sample2/moduletwo-update.tf.ignore

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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

Comments
 (0)