Skip to content

Commit 03c502f

Browse files
committed
Adding terraform files
0 parents  commit 03c502f

File tree

13 files changed

+2711
-0
lines changed

13 files changed

+2711
-0
lines changed

alb.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ALB(application Load Balancer Part)
2+
resource "aws_lb" "terra_alb" {
3+
name = "terra-alb"
4+
internal = false
5+
load_balancer_type = "application"
6+
security_groups = [aws_security_group.terra_alb_http.id]
7+
subnets = [aws_subnet.autoscaling_ws_sub_1.id, aws_subnet.autoscaling_ws_sub_2.id]
8+
9+
tags = {
10+
Name = "Terra ALB"
11+
}
12+
}
13+
14+
# ALB listener for forwarding http traffic
15+
resource "aws_lb_listener" "terra_alb_listener" {
16+
load_balancer_arn = local.alb_arn
17+
port = "80"
18+
protocol = "HTTP"
19+
20+
default_action {
21+
type = "forward"
22+
target_group_arn = aws_lb_target_group.terra_alb_target_grp.arn
23+
}
24+
}

alb_sg.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# AWS security group for Application Load Balancer(ALB)
2+
resource "aws_security_group" "terra_alb_http" {
3+
name = "terra_alb_http"
4+
vpc_id = local.terra_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+
20+
tags = {
21+
Name = "Only HTTP inbound & ALL protocols outbound"
22+
}
23+
}
24+
25+
# AWS target group for VPC & ALB
26+
resource "aws_lb_target_group" "terra_alb_target_grp" {
27+
name = "terra-alb-target-grp"
28+
port = 80
29+
protocol = "HTTP"
30+
vpc_id = local.terra_vpc_id
31+
}

ami.tf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# AMI part
2+
# EC2 instance with Apache2 webserver & website for AMI
3+
resource "aws_instance" "terra_ec2_for_ami" {
4+
ami = "ami-08df646e18b182346" # ap-south-1
5+
instance_type = "t2.micro"
6+
7+
subnet_id = aws_subnet.autoscaling_ws_sub_1.id
8+
security_groups = [aws_security_group.terra_sg.id]
9+
10+
associate_public_ip_address = true
11+
key_name = "AWS_AMI" # key-pair for ssh login && installing webserver with website
12+
user_data = <<EOF
13+
#!/bin/bash
14+
yum update -y
15+
yum install -y httpd
16+
systemctl start httpd
17+
systemctl enable httpd
18+
sudo yum install -y curl
19+
sudo curl -L -O https://github.com/codeplusmath/autoscaling-website/archive/refs/heads/master.zip
20+
sudo unzip master.zip
21+
sudo cp -r ./autoscaling-website-master/* /var/www/html/
22+
EOF
23+
24+
tags = {
25+
Name = "Terra_ec2"
26+
}
27+
}
28+
29+
# Creating AMI from above instance
30+
resource "aws_ami_from_instance" "terra_ami" {
31+
name = "terra_ami"
32+
source_instance_id = aws_instance.terra_ec2_for_ami.id
33+
34+
tags = {
35+
Name = "Terra_AMI"
36+
}
37+
}
38+
39+
40+
# Launch configuration using Custom AMI
41+
resource "aws_launch_template" "terra_lt" {
42+
image_id = aws_ami_from_instance.terra_ami.id
43+
name = "terra_lt"
44+
instance_type = "t2.micro"
45+
key_name = "AWS_AMI"
46+
47+
user_data = "${base64encode(<<EOF
48+
#!/bin/bash
49+
echo "$(hostname -f)" > /var/www/html/ip.txt
50+
EOF
51+
)}"
52+
53+
network_interfaces {
54+
associate_public_ip_address = true
55+
security_groups = [aws_security_group.terra_sg.id]
56+
}
57+
58+
tags = {
59+
Name = "terra_lt"
60+
}
61+
}

asg.tf

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# ASG(Auto Scaling Group) Part
2+
resource "aws_autoscaling_group" "terra_asg" {
3+
name = "terra_asg"
4+
target_group_arns = [aws_lb_target_group.terra_alb_target_grp.arn]
5+
6+
health_check_type = "ELB"
7+
health_check_grace_period = 30
8+
9+
vpc_zone_identifier = [aws_subnet.autoscaling_ws_sub_1.id, aws_subnet.autoscaling_ws_sub_2.id]
10+
11+
12+
desired_capacity = 2
13+
max_size = 4
14+
min_size = 1
15+
16+
enabled_metrics = [
17+
"GroupMinSize",
18+
"GroupMaxSize",
19+
"GroupDesiredCapacity",
20+
"GroupInServiceInstances",
21+
"GroupTotalInstances"
22+
]
23+
metrics_granularity = "1Minute"
24+
25+
launch_template {
26+
id = local.launch_template_id
27+
version = "$Latest"
28+
}
29+
30+
tag {
31+
key = "Name"
32+
value = "Terra_asg"
33+
propagate_at_launch = true
34+
}
35+
}
36+
37+
# AutoScaling Policies
38+
# Scale Up Policy
39+
resource "aws_autoscaling_policy" "terra_scaleup" {
40+
name = "terra_scaleup"
41+
scaling_adjustment = 1
42+
adjustment_type = "ChangeInCapacity"
43+
cooldown = 30
44+
autoscaling_group_name = aws_autoscaling_group.terra_asg.name
45+
}
46+
47+
# Cloudwatch Alarm
48+
resource "aws_cloudwatch_metric_alarm" "terra_scaleup_alarm" {
49+
alarm_name = "terra_scaleup_alarm"
50+
comparison_operator = "GreaterThanOrEqualToThreshold"
51+
evaluation_periods = "1"
52+
metric_name = "RequestCountPerTarget"
53+
54+
namespace = "AWS/ApplicationELB"
55+
period = "60"
56+
statistic = "Sum"
57+
threshold = "100"
58+
59+
dimensions = {
60+
TargetGroup = aws_lb_target_group.terra_alb_target_grp.arn_suffix
61+
LoadBalancer = aws_lb.terra_alb.arn_suffix
62+
}
63+
64+
alarm_description = "Trigger when recieve more that 100 requests in 60 sec for alb"
65+
alarm_actions = [aws_autoscaling_policy.terra_scaleup.arn]
66+
}
67+
68+
# Scale Down Policy
69+
resource "aws_autoscaling_policy" "terra_scaledown" {
70+
name = "terra_scaledown"
71+
scaling_adjustment = -1
72+
adjustment_type = "ChangeInCapacity"
73+
cooldown = 30
74+
autoscaling_group_name = aws_autoscaling_group.terra_asg.name
75+
}
76+
77+
# CloudWatch Alarm
78+
resource "aws_cloudwatch_metric_alarm" "terra_scaledown_alarm" {
79+
alarm_name = "terra_scaledown_alarm"
80+
comparison_operator = "LessThanOrEqualToThreshold"
81+
evaluation_periods = "1"
82+
metric_name = "RequestCountPerTarget"
83+
84+
namespace = "AWS/ApplicationELB"
85+
period = "60"
86+
statistic = "Sum"
87+
threshold = "100"
88+
89+
dimensions = {
90+
TargetGroup = aws_lb_target_group.terra_alb_target_grp.arn_suffix
91+
LoadBalancer = aws_lb.terra_alb.arn_suffix
92+
}
93+
94+
alarm_description = "Trigger when recieve less that 100 requests in 60 sec for alb"
95+
alarm_actions = [aws_autoscaling_policy.terra_scaledown.arn]
96+
}

main.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# AWS Virtual Private Cloud (VPC)
2+
resource "aws_vpc" "terra_autoscalingwebserver_vpc" {
3+
cidr_block = var.VPC_CIDR_BLOCK # associating CIDR block (11.0.0.0/24)
4+
5+
tags = {
6+
Name = "Terra Auto Scaling Webserver"
7+
}
8+
}
9+
10+
# AWS subnet 1 in ap-south-1a region
11+
resource "aws_subnet" "autoscaling_ws_sub_1" {
12+
vpc_id = aws_vpc.terra_autoscalingwebserver_vpc.id # creating subnet in custom VPC
13+
cidr_block = var.SUBNET_CIDR_BLOCK_1 # CIDR Block - 11.0.0.0/25 (0-127 ip addresses available)
14+
availability_zone = "ap-south-1a"
15+
16+
tags = {
17+
Name = "AutoScalingWebserver subnet 1"
18+
}
19+
}
20+
21+
# AWS subnet 2 in ap-south-1b region
22+
resource "aws_subnet" "autoscaling_ws_sub_2" {
23+
vpc_id = aws_vpc.terra_autoscalingwebserver_vpc.id # creating subnet in custom VPC
24+
cidr_block = var.SUBNET_CIDR_BLOCK_2 # CIDR Block - 11.0.0.128/25 (128-255 ip addresses available)
25+
availability_zone = "ap-south-1b"
26+
27+
tags = {
28+
Name = "AutoScalingWebserver subnet 2"
29+
}
30+
}
31+
32+
# Internet Gateway
33+
resource "aws_internet_gateway" "autoscaling_igw" {
34+
vpc_id = aws_vpc.terra_autoscalingwebserver_vpc.id
35+
36+
tags = {
37+
Name = "Internet Gateway"
38+
}
39+
}

out.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# VPC id
2+
output "terraform_vpc_id" {
3+
value = local.terra_vpc_id
4+
}
5+
6+
# ALB DNS
7+
output "terra_alb_dns" {
8+
value = local.alb_dns
9+
}

provider.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "4.22.0"
6+
}
7+
}
8+
}
9+
10+
11+
provider "aws" {
12+
region = var.AWS_REGION
13+
access_key = var.AWS_ACCESS_KEY
14+
secret_key = var.AWS_SECRET_KEY
15+
}

routetable.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Route Table Internet Gateway
2+
resource "aws_route_table" "terra_VPC_routetble" {
3+
vpc_id = aws_vpc.terra_autoscalingwebserver_vpc.id
4+
5+
route{
6+
cidr_block = "0.0.0.0/0"
7+
gateway_id = aws_internet_gateway.autoscaling_igw.id
8+
}
9+
10+
tags = {
11+
Name = "Custom Public Subnet in terra VPC"
12+
}
13+
}
14+
15+
# Associating routes with VPC subnets
16+
resource "aws_route_table_association" "public_vpc_subnet_1a" {
17+
subnet_id = aws_subnet.autoscaling_ws_sub_1.id
18+
route_table_id = aws_route_table.terra_VPC_routetble.id
19+
}
20+
21+
resource "aws_route_table_association" "public_vpc_subnet_1b" {
22+
subnet_id = aws_subnet.autoscaling_ws_sub_2.id
23+
route_table_id = aws_route_table.terra_VPC_routetble.id
24+
}

sg.tf

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# AWS security groups for VPC & ASG
2+
resource "aws_security_group" "terra_sg" {
3+
name = "terra_sg"
4+
description = "Allow http connection"
5+
vpc_id = aws_vpc.terra_autoscalingwebserver_vpc.id
6+
7+
ingress {
8+
description = "HTTP connection inbound"
9+
from_port = 80
10+
11+
to_port = 80
12+
protocol = "tcp"
13+
cidr_blocks = ["0.0.0.0/0"]
14+
}
15+
16+
ingress {
17+
description = "SSH connection inbound"
18+
from_port = 22
19+
20+
to_port = 22
21+
protocol = "tcp"
22+
cidr_blocks = ["0.0.0.0/0"]
23+
}
24+
25+
26+
egress {
27+
from_port = 0
28+
to_port = 0
29+
protocol = "-1"
30+
cidr_blocks = ["0.0.0.0/0"]
31+
}
32+
33+
tags = {
34+
Name = "terra_sg"
35+
}
36+
}

0 commit comments

Comments
 (0)