Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
86ba1e3
update aws ami data resource filter in main.tf
ranjithk9812-code Jun 23, 2026
17ad81e
added AWS instance
ranjithk9812-code Jun 23, 2026
00357bc
AWS instances updated
ranjithk9812-code Jun 23, 2026
57b6a16
Added instance and location
ranjithk9812-code Jun 23, 2026
5a3eafd
Added
ranjithk9812-code Jun 23, 2026
1f06e2c
added the instance type
ranjithk9812-code Jun 23, 2026
64eab00
initial commit
ranjithk9812-code Jun 24, 2026
2f05519
updated instace type
ranjithk9812-code Jun 24, 2026
b83aa24
change instace type
ranjithk9812-code Jun 24, 2026
4367076
Updated
ranjithk9812-code Jun 24, 2026
93b458b
changed the variable
ranjithk9812-code Jun 24, 2026
92acf93
updated the outputs
ranjithk9812-code Jun 24, 2026
83af5f7
same update the output file
ranjithk9812-code Jun 24, 2026
90bf0ca
created securoty group
ranjithk9812-code Jun 24, 2026
56df90c
fix the error
ranjithk9812-code Jun 24, 2026
4632913
use module for security group
ranjithk9812-code Jun 24, 2026
0e53415
update the modules
ranjithk9812-code Jun 24, 2026
1ed89cd
removed the security group
ranjithk9812-code Jun 24, 2026
2355d94
Added the module VPC
ranjithk9812-code Jun 25, 2026
35431ef
error fixed
ranjithk9812-code Jun 25, 2026
e3aafe5
load balancer
ranjithk9812-code Jun 29, 2026
ffb6a5d
resloved
ranjithk9812-code Jun 29, 2026
957e2a7
fix EC2 security
ranjithk9812-code Jun 29, 2026
3be533a
Allow traffic to EC2
ranjithk9812-code Jun 29, 2026
1eac315
autoscaling
ranjithk9812-code Jun 29, 2026
6ba2368
refractor to use variables
ranjithk9812-code Jun 29, 2026
f625a4e
deploy dev env
ranjithk9812-code Jun 30, 2026
01690ec
updated
ranjithk9812-code Jun 30, 2026
69408b1
redeployed
ranjithk9812-code Jun 30, 2026
0667bf8
updated
ranjithk9812-code Jun 30, 2026
4fa240a
updated
ranjithk9812-code Jun 30, 2026
4e99fea
uodated
ranjithk9812-code Jun 30, 2026
f7447dc
updated
ranjithk9812-code Jun 30, 2026
8a0df56
cleanup
ranjithk9812-code Jun 30, 2026
2388647
deploy qa
ranjithk9812-code Jun 30, 2026
779024e
typo mistakes
ranjithk9812-code Jun 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions enviroments/dev/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module "dev" {
source = "../../modules"
}

5 changes: 3 additions & 2 deletions providers.tf → enviroments/dev/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}

provider "aws" {
region = "us-west-2"
}
region = "us-west-2"
}
13 changes: 13 additions & 0 deletions enviroments/qa/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module "qa" {
source = "../../modules/web"

environment = {
name = qa
network_prefix = "10.1"

}

min_size = 1
max_size = 1
}

3 changes: 3 additions & 0 deletions enviroments/qa/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "environment_url" {
value = module.qa.environment_url
}
12 changes: 12 additions & 0 deletions enviroments/qa/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}

provider "aws" {
region = "us-west-2"
}
24 changes: 0 additions & 24 deletions main.tf

This file was deleted.

193 changes: 193 additions & 0 deletions modules/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
data "aws_ami" "app_ami" {
most_recent = true
owners = var.ami_filter.owners

filter {
name = "name"
values = [var.ami_filter.name]
}

filter {
name = "architecture"
values = ["x86_64"]
}

filter {
name = "root-device-type"
values = ["ebs"]
}

filter {
name = "virtualization-type"
values = ["hvm"]
}
}

module "web_vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "~> 6.0"

name = "${var.environment.name}-web-vpc"
cidr = "${var.environment.network_prefix}.0.0/16"

azs = ["us-west-2a", "us-west-2b", "us-west-2c"]

private_subnets = [
"${var.environment.network_prefix}.1.0/24",
"${var.environment.network_prefix}.2.0/24",
"${var.environment.network_prefix}.3.0/24"
]

public_subnets = [
"${var.environment.network_prefix}.101.0/24",
"${var.environment.network_prefix}.102.0/24",
"${var.environment.network_prefix}.103.0/24"
]

map_public_ip_on_launch = true

enable_nat_gateway = false
enable_vpn_gateway = false

tags = {
Terraform = "true"
Environment = var.environment.name
}
}

module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 9.0"

name = "${var.environment.name}-web-alb"
load_balancer_type = "application"

vpc_id = module.web_vpc.vpc_id
subnets = module.web_vpc.public_subnets

security_group_ingress_rules = {
all_http = {
from_port = 80
to_port = 80
ip_protocol = "tcp"
description = "Allow HTTP web traffic"
cidr_ipv4 = "0.0.0.0/0"
}
}

security_group_egress_rules = {
all = {
ip_protocol = "-1"
cidr_ipv4 = "0.0.0.0/0"
}
}

listeners = {
http = {
port = 80
protocol = "HTTP"

forward = {
target_group_key = "web-instance"
}
}
}

target_groups = {
web-instance = {
name_prefix = "web"
protocol = "HTTP"
port = 80
target_type = "instance"
create_attachment = false

health_check = {
enabled = true
path = "/"
port = "traffic-port"
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 5
interval = 30
matcher = "200"
}
}
}

tags = {
Environment = var.environment.name
Project = "Terraform-Learning"
}
}

resource "aws_security_group" "web_instance_sg" {
name_prefix = "web-instance-sg-"
description = "Allow HTTP traffic from ALB to EC2"
vpc_id = module.web_vpc.vpc_id

ingress {
description = "Allow HTTP from ALB"
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = [module.alb.security_group_id]
}

egress {
description = "Allow all outbound traffic"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

tags = {
Name = "${var.environment.name}-web-instance-sg"
}
}

module "autoscaling" {
source = "terraform-aws-modules/autoscaling/aws"
version = "9.2.1"

name = "${var.environment.name}-web-asg"

min_size = var.min_size
max_size = var.max_size
desired_capacity = var.desired_capacity

vpc_zone_identifier = module.web_vpc.public_subnets

health_check_type = "ELB"
health_check_grace_period = 300

launch_template_name = "web-asg-template"
launch_template_description = "Launch template for web ASG"
update_default_version = true

image_id = data.aws_ami.app_ami.id
instance_type = var.instance_type
security_groups = [aws_security_group.web_instance_sg.id]

user_data = base64encode(<<-EOF
#!/bin/bash
dnf update -y
dnf install -y httpd
systemctl start httpd
systemctl enable httpd
echo "Hello from Terraform Auto Scaling behind ALB" > /var/www/html/index.html
EOF
)

traffic_source_attachments = {
web_alb = {
traffic_source_identifier = module.alb.target_groups["web-instance"].arn
traffic_source_type = "elbv2"
}
}

tags = {
Environment = var.environment.name
Project = "Terraform-Learning"
}
}
11 changes: 11 additions & 0 deletions modules/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
output "environment_url" {
value = module.alb.dns_name
}

output "vpc_id" {
value = module.web_vpc.vpc_id
}

output "autoscaling_group_name" {
value = module.autoscaling.autoscaling_group_name
}
49 changes: 49 additions & 0 deletions modules/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
variable "instance_type" {
description = "Type of EC2 instance to provision"
type = string
default = "t3.micro"
}

variable "ami_filter" {
description = "Name filter and owner for AMI"
type = object({
name = string
owners = list(string)
})

default = {
name = "al2023-ami-2023.*-x86_64"
owners = ["amazon"]
}
}

variable "environment" {
description = "Deployment environment details"
type = object({
name = string
network_prefix = string
})

default = {
name = "dev"
network_prefix = "10.0"
}
}

variable "min_size" {
description = "Minimum number of instances in the ASG"
type = number
default = 1
}

variable "max_size" {
description = "Maximum number of instances in the ASG"
type = number
default = 2
}

variable "desired_capacity" {
description = "Desired number of instances in the ASG"
type = number
default = 1
}
7 changes: 0 additions & 7 deletions outputs.tf

This file was deleted.

4 changes: 0 additions & 4 deletions variables.tf

This file was deleted.