Skip to content

Commit

Permalink
Final test version pushing with readme
Browse files Browse the repository at this point in the history
  • Loading branch information
007mahapra committed Mar 4, 2024
1 parent 940e9e1 commit 7306b11
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 16 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/terraform-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ jobs:
terraform_version: 1.5.7

- name: Configure AWS credentials
run: |
mkdir -p ~/.aws
echo "[default]" > ~/.aws/credentials
echo "aws_access_key_id=${{ secrets.AWS_ACCESS_KEY_ID }}" >> ~/.aws/credentials
echo "aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> ~/.aws/credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: "ap-southeast-1"

- name: Initialize Terraform
run: terraform init
Expand All @@ -38,6 +38,7 @@ jobs:
run: terraform plan

- name: Run terraform plan
#run: terraform apply -auto-approve # Mahaveer - Don't want to run from Github Action automatically as I have tested it manually , submitting just for assesment
run: terraform apply

destroy:
Expand All @@ -46,3 +47,4 @@ jobs:
steps:
- name: Manual Step - Destroy Infrastructure
run: terraform destroy
#run: terraform destroy -auto-approve # Mahaveer - Don't want to run from Github Action automatically as I have tested it manually , submitting just for assesment
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ override.tf.json

# Ignore CLI configuration files
.terraformrc
terraform.rc
terraform.rc

*.lock.hcl
35 changes: 34 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
# AWS_LB_WITH_TF_PIPELINE
# Project
Load Balancer in AWS

# Solution Diagram
![plot](./docs/soliution_diagram.drawio.png)



# Tested on
- Terraform version : 1.5.7
- aws provider version : 5.39.0
- AWS Free Tier

# What it does
1. Creates VPC , Private & Public Subnets, Creates ALB with Autoscaling
2. Runs a pipeline in GitHub Actions whenever code is pushed to main branch


# Output
1. Tested locally and it can run automatically on GitHub Actions , didn't apply as it'll incur cost

![plot](./docs/Terraform_successful_apply.png)

2. Nginx Web Server running through LB

![plot](./docs/NginxOnLB.png)

3. Loadbalancer in AWS

![plot](./docs/LOadBalancer.png)

4. Desired EC2 instances running in AWS

![plot](./docs/EC2_instances.png)
Binary file added docs/EC2_instances.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/LOadBalancer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/NginxOnLB.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/Terraform_successful_apply.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/soliution_diagram.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions elb.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_lb" "mv_lb" {
name = "sharmi-lb-asg"
name = "mahaveer-lb-asg"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.mv_sg_for_elb.id]
Expand Down Expand Up @@ -61,9 +61,9 @@ resource "aws_launch_template" "mv_ec2_launch_templ" {

resource "aws_autoscaling_group" "mv_asg" {
# no of instances
desired_capacity = 1
max_size = 1
min_size = 1
desired_capacity = var.desired_capacity
max_size = var.max_size
min_size = var.min_size

# source
target_group_arns = [aws_lb_target_group.mv_alb_tg.arn]
Expand Down
4 changes: 2 additions & 2 deletions network.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ resource "aws_vpc" "my_vpc" {
resource "aws_subnet" "pub_subnets" {
for_each = { for idx, zone in var.availability_zone : idx => zone }
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.${each.key}.0/24" # Adjust CIDR block as needed
cidr_block = "10.0.${each.key}.0/24" # Distributing IPs
map_public_ip_on_launch = true
availability_zone = each.value
tags = {
Expand All @@ -20,7 +20,7 @@ resource "aws_subnet" "pub_subnets" {
resource "aws_subnet" "pvt_subnets" {
for_each = { for idx, zone in var.availability_zone : idx => zone }
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.${each.key + 2}.0/24" # Adjust CIDR block as needed
cidr_block = "10.0.${each.key + 2}.0/24" # Distributing IPs
availability_zone = each.value
tags = {
"Name" = "PRIVATE_SUBNET_${each.value}"
Expand Down
3 changes: 3 additions & 0 deletions output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "dns" {
value = aws_lb.mv_lb.dns_name
}
4 changes: 2 additions & 2 deletions user_data.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
#sudo yum update -y
sudo yum install -y nginx
echo "<h1>Hello from $(hostname -f)</h1>" > /var/www/html/index.html
service nginx start
service nginx start
echo "<h1>Hello from Mahaveer's Nginx Web Server running at $(hostname -f)</h1>" > /var/www/html/index.html
20 changes: 19 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,22 @@ variable "sg_ports_for_internet" {
type = list(number)
default = [80, 443] # 80 -> http, 443 -> https
description = "List of allowed ports accessible through LB."
}
}

variable "desired_capacity" {
type = number
default = 2
description = "Desired target number of instances the group aims to keep available"
}

variable "max_size" {
type = number
default = 2
description = "Maximum number of instances the Auto Scaling Group can ever launch."
}

variable "min_size" {
type = number
default = 1
description = "Minimum number of instances the Auto Scaling Group "
}

0 comments on commit 7306b11

Please sign in to comment.