diff --git a/.github/workflows/terraform-deploy.yml b/.github/workflows/terraform-deploy.yml index 53b510f..3468c5c 100644 --- a/.github/workflows/terraform-deploy.yml +++ b/.github/workflows/terraform-deploy.yml @@ -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 @@ -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: @@ -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 diff --git a/.gitignore b/.gitignore index 1e0a9e6..bcd945c 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,6 @@ override.tf.json # Ignore CLI configuration files .terraformrc -terraform.rc \ No newline at end of file +terraform.rc + +*.lock.hcl \ No newline at end of file diff --git a/README.md b/README.md index 9b014e7..f0d3167 100644 --- a/README.md +++ b/README.md @@ -1 +1,34 @@ -# AWS_LB_WITH_TF_PIPELINE \ No newline at end of file +# 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) diff --git a/docs/EC2_instances.png b/docs/EC2_instances.png new file mode 100644 index 0000000..01a2858 Binary files /dev/null and b/docs/EC2_instances.png differ diff --git a/docs/LOadBalancer.png b/docs/LOadBalancer.png new file mode 100644 index 0000000..08253d8 Binary files /dev/null and b/docs/LOadBalancer.png differ diff --git a/docs/NginxOnLB.png b/docs/NginxOnLB.png new file mode 100644 index 0000000..688080b Binary files /dev/null and b/docs/NginxOnLB.png differ diff --git a/docs/Terraform_successful_apply.png b/docs/Terraform_successful_apply.png new file mode 100644 index 0000000..ef2d9b7 Binary files /dev/null and b/docs/Terraform_successful_apply.png differ diff --git a/docs/soliution_diagram.drawio.png b/docs/soliution_diagram.drawio.png new file mode 100644 index 0000000..b93533e Binary files /dev/null and b/docs/soliution_diagram.drawio.png differ diff --git a/elb.tf b/elb.tf index e52444a..097d413 100644 --- a/elb.tf +++ b/elb.tf @@ -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] @@ -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] diff --git a/network.tf b/network.tf index 1a329cd..9cc44ee 100644 --- a/network.tf +++ b/network.tf @@ -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 = { @@ -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}" diff --git a/output.tf b/output.tf new file mode 100644 index 0000000..514c484 --- /dev/null +++ b/output.tf @@ -0,0 +1,3 @@ +output "dns" { + value = aws_lb.mv_lb.dns_name +} \ No newline at end of file diff --git a/user_data.sh b/user_data.sh index c44b453..35b9343 100644 --- a/user_data.sh +++ b/user_data.sh @@ -1,5 +1,5 @@ #!/bin/bash #sudo yum update -y sudo yum install -y nginx -echo "