Skip to content

Commit ee75bb2

Browse files
kammanakammana
authored andcommitted
terraform project code
0 parents  commit ee75bb2

File tree

8 files changed

+157
-0
lines changed

8 files changed

+157
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.terraform*
2+
*.tfstate*

elb.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Create a new load balancer
2+
resource "aws_elb" "javahome-elb" {
3+
name = "javahome-elb"
4+
# availability_zones = ["${var.azs}"]
5+
subnets = ["${aws_subnet.public.*.id}"]
6+
security_groups = ["${aws_security_group.webservers.id}"]
7+
listener {
8+
instance_port = 80
9+
instance_protocol = "http"
10+
lb_port = 80
11+
lb_protocol = "http"
12+
}
13+
14+
health_check {
15+
healthy_threshold = 2
16+
unhealthy_threshold = 2
17+
timeout = 3
18+
target = "HTTP:80/index.html"
19+
interval = 10
20+
}
21+
22+
instances = ["${aws_instance.webservers.*.id}"]
23+
cross_zone_load_balancing = true
24+
idle_timeout = 100
25+
connection_draining = true
26+
connection_draining_timeout = 300
27+
28+
tags {
29+
Name = "javahome-terraform-elb"
30+
}
31+
}
32+
33+
output "elb-dns-name"{
34+
value = "${aws_elb.javahome-elb.dns_name}"
35+
}

install_httpd.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#! /bin/bash
2+
3+
yum install httpd -y
4+
echo "<h1>Deployed by terraform </h1>" > /var/www/html/index.html
5+
chkconfig httpd on
6+
service httpd start

instances.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "aws_instance" "webservers" {
2+
count = "${length(var.subnets_cidr)}"
3+
ami = "${var.webservers_ami}"
4+
instance_type = "${var.instance_type}"
5+
security_groups = ["${aws_security_group.webservers.id}"]
6+
subnet_id = "${element(aws_subnet.public.*.id,count.index)}"
7+
user_data = "${file("install_httpd.sh")}"
8+
9+
tags {
10+
Name = "Server-${count.index}"
11+
}
12+
13+
}

provider.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
region = "${var.aws_region}"
3+
}

security-groups.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "aws_security_group" "webservers" {
2+
name = "allow_http"
3+
description = "Allow http inbound traffic"
4+
vpc_id = "${aws_vpc.javahome_vpc.id}"
5+
6+
ingress {
7+
from_port = 80
8+
to_port = 80
9+
protocol = "tcp"
10+
cidr_blocks = ["0.0.0.0/0"]
11+
}
12+
13+
egress {
14+
from_port = 0
15+
to_port = 0
16+
protocol = "-1"
17+
cidr_blocks = ["0.0.0.0/0"]
18+
}
19+
}

variables.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
variable "aws_region" {
2+
default = "us-west-2"
3+
}
4+
5+
variable "vpc_cidr" {
6+
default = "10.20.0.0/16"
7+
}
8+
9+
variable "subnets_cidr" {
10+
type = "list"
11+
default = ["10.20.1.0/24", "10.20.2.0/24"]
12+
}
13+
14+
variable "azs" {
15+
type = "list"
16+
default = ["us-west-2a", "us-west-2b"]
17+
}
18+
19+
variable "webservers_ami" {
20+
default = "ami-e689729e"
21+
}
22+
23+
variable "instance_type" {
24+
default = "t2.micro"
25+
}

vpc.tf

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# VPC for our applications
2+
3+
resource "aws_vpc" "javahome_vpc" {
4+
cidr_block = "${var.vpc_cidr}"
5+
6+
tags {
7+
Name = "JavaHomeVPC"
8+
}
9+
}
10+
11+
# Create Internet Gateway and attach it to javahome_vpc
12+
13+
resource "aws_internet_gateway" "javahome_igw" {
14+
vpc_id = "${aws_vpc.javahome_vpc.id}"
15+
16+
tags {
17+
Name = "main"
18+
}
19+
}
20+
21+
# Build subnets for our VPCs
22+
resource "aws_subnet" "public" {
23+
count = "${length(var.subnets_cidr)}"
24+
vpc_id = "${aws_vpc.javahome_vpc.id}"
25+
availability_zone = "${element(var.azs,count.index)}"
26+
cidr_block = "${element(var.subnets_cidr,count.index)}"
27+
map_public_ip_on_launch = true
28+
tags {
29+
Name = "Subnet-${count.index +1}"
30+
}
31+
}
32+
33+
# Create Route table, attache Internet Gateway and associate with public subnets
34+
35+
36+
resource "aws_route_table" "public_rt" {
37+
vpc_id = "${aws_vpc.javahome_vpc.id}"
38+
39+
route {
40+
cidr_block = "0.0.0.0/0"
41+
gateway_id = "${aws_internet_gateway.javahome_igw.id}"
42+
}
43+
tags {
44+
Name = "PublicRT"
45+
}
46+
}
47+
48+
# Attach route table with public subnets
49+
50+
resource "aws_route_table_association" "a" {
51+
count = "${length(var.subnets_cidr)}"
52+
subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
53+
route_table_id = "${aws_route_table.public_rt.id}"
54+
}

0 commit comments

Comments
 (0)