Day 2 : Elastic Compute Cloud (EC2)

Day 2 : Elastic Compute Cloud (EC2)

ยท

6 min read

Amazon EC2 allows us to create virtual machines, or instances, that run on the AWS Cloud .

How to create , manage & connect to an EC2 instance ?

  • search for EC2 in the search box

  • Now select The Option Virtual Servers in Cloud to view EC2 Dashboard.

  • In the EC2 Dashboard we will set to see a section called Launch Instance

  • To Launch an instance press Launch instance:

    • In order to connect to this server remotely Create new key pair.

      • fill in the required details:

    • Save the .pem or .ppk file that gets generated.

  • Once configuring the server according to Our needs Click on Launch Instance.

  • Now to connect to the Server click on the Instance ID and then click on connect.

  • Now Follow the steps that are mentioned:

  • Now to connect the instance remotely:

    1. Locate your private key file (usually in Downloads folder)

    2. ensure your key in not publicly viewable:

       cd Downlloads
       ls -l
       total 1
       -rw-r--r--@  1 root  group     1674 Dec  3 19:41 Test-server-RSA-Privet-key.pem
       # to change the visibility
       chmod 400 Test-server-RSA-Privet-key.pem
       ls -l
       total 1
       -r--------@  1 root  group     1674 Dec  3 19:41 Test-server-RSA-Privet-key.pem
      
  • Now to connect to the instance remotely run the following command in the terminal:

      ssh -i "Test-server-RSA-Privet-key.pem" ubuntu@ec2-3-81-19-167.compute-1.amazonaws.com
    

Launch Templates:

  • In order to avoid repeating the above tasks we can create a Template from instance.

  • Configure the template according to your needs:

  • No to use this template:

    • navigate to launch templates then select the template and toggle Actions button to view launch instance from template.

  • The instance will be preconfigured and to launch a new instance just click on create instance :

User Data Script (EC2) :

  • In order to configure the EC2 instance with updates, other services ect we use user data script:

  • scroll to the bottom of launch instance from template page and you will get to see advance options , toggle it and again scroll down to see the following result:

    this user data will apply for the current instances begin created using the instance template and will not persist .

    we might need to add the user data if we intend to use this instance again.

    To avoid this we can modify the instance template

  • Modify the Instance Template:

    scroll down and toggle Advanced Option and then again scroll down :

  • Suppose we want to update the Instance and also want to install tools like docker , nginx then we can write a user data which is actually a bash file

      #!/bin/bash
      sudo apt-get update -y
      sudo apt-get install docker.io -y
      sudo apt-get install git-all -y
      sudo apt update
      sudo apt install fontconfig openjdk-17-jre -y
      java -version
      openjdk version "17.0.8" 2023-07-18
      OpenJDK Runtime Environment (build 17.0.8+7-Debian-1deb12u1)
      OpenJDK 64-Bit Server VM (build 17.0.8+7-Debian-1deb12u1, mixed mode, sharing) 
      sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
        https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
      echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
        https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
        /etc/apt/sources.list.d/jenkins.list > /dev/null
      sudo apt-get update
      sudo apt-get install jenkins -y
    
      sudo usermod -aG docker ubuntu
      sudo usermod -aG docker jenkins
      sudo apt-get upgrate -y
    

Autoscaling & Load Balancing:

  • Autoscaling and load balancing are crucial components in a system's architecture to ensure optimal performance, availability, and cost efficiency

Scenario: E-commerce Website on Amazon EC2

  • Imagine you have an e-commerce website hosted on Amazon EC2 instances.

Normal Traffic:

  • During regular hours, your website experiences moderate traffic.

  • You have a fixed number of EC2 instances handling the incoming requests.

  • Everything works smoothly, and users can browse and make purchases without any noticeable delays

Traffic Surge:

  • Suddenly, a marketing campaign or a flash sale results in a significant increase in traffic.

  • The current number of EC2 instances may struggle to handle the sudden surge, leading to slower response times or, in worst cases, service outages.

Need For Autoscaling :

  • Autoscaling allows your infrastructure to automatically respond to changes in demand.

  • When the traffic increases, autoscaling can spin up additional EC2 instances to handle the load.

  • During periods of low traffic, autoscaling can reduce the number of instances, ensuring that you're not paying for resources you don't need.

    Uneven Distribution ๐Ÿค”:

    • Assuming autoscaling is in place, new instances are spun up to accommodate the increased traffic.

    • However, without load balancing, all incoming requests might be directed to a single instance (or a few instances), leading to uneven distribution of the load.

Need For Load Balancing:

  • Load balancing distributes incoming network traffic across multiple EC2 instances, ensuring that no single instance bears the entire burden.

  • This improves responsiveness and prevents any individual instance from becoming a bottleneck.

Traffic Decrease:

  • After the surge, the traffic returns to normal levels.

  • Autoscaling can now scale down the number of instances to save on costs, ensuring that you're not over-provisioned during quieter periods.

Implementing Auto Scaling groups and Load Balancing :

  • An Auto Scaling group is a collection of Amazon EC2 instances that are treated as a logical unit.

  • Make Sure that you Don't include subnet in launch template because it is Not applicable for EC2 Auto Scaling.

  • Also Shutdown behavior, Stop - Hibernate behavior like Stop and Terminate are Not applicable for EC2 Auto Scaling.

  • Navigate to EC2 and In the left panel scroll to the bottom until you find the Auto Scaling toggle button.

  • Now to create an Auto Scaling group Click 'Create Auto Scaling group' :

  • Now give a name to the Auto Scaling group and select the launch template with the proper template version if you have multiple versions available:

  • In the next step we need to Choose instance launch options:

    • Select the subnets that you want your instance to run on.

Configure advanced options:

  • Attaching a Load balancing:

    • Configure group size and scaling:

      Set this according to the needs.

    • Add Notification if you want:

    • Add Tags

    • Review:

  • When we click create Auto Scaling group.

    • you can observe that whenever the CPU performance (i selected it) exceeds the threshold value then it will trigger the build of new EC2 instance .

Web Application Firewall & Shield (WAF):

  • Protects Against DDoS Attacks and Malicious Web Traffic.

  • Describe web ACL and associate it to AWS resources:

  • Add AWS resources:

  • Add managed rule groups

  • Add rules and rule groups:

  • Set rule priority:

  • Configure metrics:

  • Review and create web ACL:

    • As a result the WAF gets generated:


ย