Skip to content

OloruntobiOlurombi/VPC

Repository files navigation

Create EC2 instance with load balancer and persistence layer using IAC (Terraform)

image

Table of Content

  1. Create a VPC which should have a public and private subnet
  2. Create a role with s3 access.
  3. Launch an ec2 instance with the role created in step 1, inside the private subnet of VPC, and install apache through bootstrapping. (You need to have your NAT gateway attached to your private subnet)
  4. Create a load balancer in public subnet.
  5. Add the ec2 instance, under the load balancer
  6. Create an auto scaling group with minimum size of 1 and maximum size of 3 with load balancer created in step 3.
  7. Add the created instances under the auto scaling group.
  8. Write a life cycle policy with the following parameters: a. scale in : CPU utilization > 80% b. scale out : CPU Utilization < 60%
  9. Create a persistence layer of your choice and ensure that the ec2 instance can access it

Prerequisites:

  1. Basic knowledge of aws is required
  2. Basic knowledge of terraform is required
  3. AWS CLI (Install AWS CLI)
  4. Terraform (Install Terraform)

Project Structure

image

Let get Started!!!!

Step 1

  • aws configure

We have to create a new profile using aws configure command. This require access key and secret key to make profile and this profile will be used in terraform provider for authentication.

$ aws configure --profile yourProfilename

image

Step 2

  • Providers (terraform and aws) and profile

main.tf

image

Step 3

  • VPC (creating a VPC)

main.tf

image

Step 4

  • Subnets (Private & Public)

main.tf

image

Step 5

  • Internal Gateway for VPC (Create a internal gateway for VPC)

main.tf

image

Step 6

  • Elastic IP (Create a Elastic IP)

main.tf

image

Step 7

  • Route Table and Association for Public and Private Subnets

main.tf

image

image

Step 8

  • Create an S3 bucket and make it Private

main.tf

image

Step 9

  • Create a policy that will give access to the S3 bucket

allowaccess.json

image

main.tf

image

Step 10

  • Create an IAM role

ec2-assume-policy.json

image

main.tf

image

Step 11

  • Policy Attachment (Attach the policy to the role)

main.tf

image

Step 12

  • Create Security Group

main.tf

image

image

Step 13

  • Create apache launch configuration

ec2.tf

image

Step 14

  • Create Apache web instance EC2

ec2.tf

image

Step 15

  • Create IAM instance profile

main.tf

image

Step 16

  • Create an application load balancer security group

main.tf

image

Step 17

  • Create a new application load balancer in public subnet

main.tf

image

Step 18

  • Create a new target group for the application load balancer

main.tf

image

Step 19

  • Create a listener

main.tf

image

Step 20

  • Creating a target group attachment for the target group

main.tf

image

Step 21

  • Create a new ALB Target Group Attachment

main.tf

image

Step 22

  • Create an auto scaling group

main.tf

image

image

Step 23

  • Write a life cycle policy to scale up and down

main.tf

image

Step 24

  • Write a trigger using a CloudWatch alarm

main.tf

image

image

Step 25

  • Create Elastic Block Storage for our second EC2 instance

main.tf

image

Step 26

  • Create a volume attachment for our second EC2 instance

main.tf

image

Step 27

Please run the following Terraform Code:

  1. terraform init
  2. terraform plan
  3. terraform apply

Step 28

  • Login into your AWS Console and click on the autoscaling EC2. Then copy the Public IPv4 address to your web browser:

image

The End

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages