# Day40 of #90Daysofdevops

Amazon Web Services (AWS) provides a range of powerful tools to help you manage and scale your cloud infrastructure efficiently. In this guide, we’ll explore how to **automate EC2 instance setup** using **Launch Templates**, understand different **instance types**, and work with **Auto-Scaling Groups** to automatically adjust your instance capacity based on demand.

---

## **What is EC2?**

Amazon EC2 (Elastic Compute Cloud) allows you to run virtual machines, known as instances, on the AWS cloud. You can launch as many instances as you need and scale up or down depending on the demand.

**Key Concepts:**

* **Instance**: A virtual server on AWS.
    
* **AMI (Amazon Machine Image)**: A pre-configured template to launch instances with specific settings (like OS, applications, and configurations).
    
* **Security Groups**: A set of firewall rules that control access to your instances.
    

## **Why Automate EC2 Instance Setup?**

Managing EC2 instances manually can become repetitive and error-prone, especially as the number of instances increases. Using automation tools like **Launch Templates** and **Auto-Scaling Groups** can help streamline the process, reduce human errors, and improve operational efficiency.

## **Understanding EC2 Instance Types**

AWS offers different instance types for specific use cases. The right instance type for your application depends on factors like CPU requirements, memory, and storage needs.

Here are some common EC2 instance types:

* **General Purpose (e.g., t2, t3)**: Balanced performance for most workloads.
    
* **Compute Optimized (e.g., c5, c6g)**: Ideal for compute-intensive tasks like high-performance web servers or batch processing.
    
* **Memory Optimized (e.g., r5, r6g)**: Best for memory-intensive applications like large databases or in-memory caches.
    
* **Storage Optimized (e.g., i3, d2)**: High-performance storage for data-heavy applications that require lots of local storage.
    

## **Amazon Machine Images (AMIs)**

An **Amazon Machine Image (AMI)** is a pre-configured image that contains the software configuration required to launch an EC2 instance. It includes the operating system, application server, and any applications you need.

* **AWS Marketplace**: You can select an AMI from the AWS Marketplace, which offers various ready-made software solutions.
    
* **Community AMIs**: These are shared by other AWS users.
    
* **Custom AMIs**: If you've set up a specific instance configuration, you can create your own custom AMI for future use.
    

---

# **Launch Templates in AWS EC2**

A **Launch Template** is a reusable configuration that defines the settings needed to launch EC2 instances. It helps you standardize the configuration of your EC2 instances, so every time you launch an instance from a template, it is set up with the same configurations.

With a Launch Template, you can define:

* **AMI** (the image you use to launch the instance)
    
* **Instance type** (e.g., t2.micro, t3.large)
    
* **Security groups** (firewall rules)
    
* **Key pair** (for SSH access)
    
* **User data scripts** (automated configuration tasks like installing software)
    

By using Launch Templates, you avoid repetitive manual configurations and ensure consistency across your infrastructure.

---

### **Steps to Create a Launch Template**

1. **Log in to the AWS Management Console:**
    
    * Go to the [**AWS Management Console**](https://aws.amazon.com/console/).
        
    * Sign in with your AWS credentials.
        
2. **Navigate to EC2 Dashboard:**
    
    * From the AWS Management Console, click on **Services** at the top and then select **EC2** from the "Compute" section.
        
3. **Create a Launch Template:**
    
    * In the EC2 Dashboard, find the "Launch Templates" option in the left sidebar under the **Instances** section.
        
    * Click **Launch Templates**, and then click **Create launch template**.
        
4. **Configure the Launch Template:**
    
    * **Template Name**: Enter a descriptive name for your template. Example: "Jenkins-Docker-Template".
        
    * **Template Version Description**: Add a description for the version (e.g., "Initial version for Jenkins and Docker setup").
        
    * **AMI ID**: Select **Amazon Linux 2** as the operating system.
        
    * **Instance Type**: Choose **t2.micro**, which is suitable for small, cost-effective applications.
        
    * **Key Pair**: Select an existing key pair or create a new one for SSH access to the instance.
        
    * **Security Groups**: Choose a security group that allows access to SSH (port 22) and HTTP (port 80) so you can access your instance and web services.
        
    * **User Data**: This is the most important part! Paste the following script to automatically install **Jenkins** and **Docker** on your instance. This script will run when the instance is launched:
        
        **Copy**
        
        **Copy**
        
        ```bash
          #!/bin/bash
          # Update the package index
          sudo yum update -y
        
          # Install Docker
          sudo amazon-linux-extras install docker -y
          sudo service docker start
          sudo usermod -a -G docker ec2-user
        
          # Install Jenkins
          wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
          sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
          sudo yum install jenkins java-1.8.0-openjdk-devel -y
          sudo systemctl start jenkins
          sudo systemctl enable jenkins
        ```
        

This script:

* Updates the instance's packages.
    
* Installs Docker and Jenkins, and starts their services.
    

5. **Create the Template:**
    
    * Once all the settings are configured, click **Create launch template**. Your template is now ready!
        

---

### **Using the Launch Template to Create Instances**

Now that you’ve created a Launch Template, you can use it to easily launch new EC2 instances with the same configuration.

1. **Launch Instances:**
    
    * Go back to the EC2 Dashboard and click **Launch instance**.
        
    * Choose the **Launch instance from template** option.
        
    * Select the launch template you created (e.g., "Jenkins-Docker-Template").
        
    * Specify how many instances you want to launch (e.g., 3 instances).
        
2. **Verify the Instances:**
    
    * After launching the instances, go to the **Instances** section of the EC2 Dashboard.
        
    * Note down the **public IP addresses** of the running instances.
        
    * Open a web browser and type in the **public IP address** of any instance (e.g., `http://<your-instance-public-ip>:8080`).
        
    * You should see the Jenkins setup page if the user data script worked correctly.
        

---

# **Steps to Create an Auto-Scaling Group**

1. **Create an Auto-Scaling Group:**
    
    * In the EC2 Dashboard, click **Auto Scaling Groups** from the left sidebar under **Auto Scaling**.
        
    * Click **Create Auto Scaling group**.
        
2. **Configure the Auto-Scaling Group:**
    
    * **Group Name**: Give your group a descriptive name (e.g., "Jenkins-Docker-AutoScaling").
        
    * **Launch Template**: Choose the launch template you created earlier (e.g., "Jenkins-Docker-Template").
        
    * **VPC and Subnets**: Select the **VPC** and **subnets** where your instances should be launched.
        
    * **Scaling Policies**: Set the desired number of instances, minimum number of instances, and maximum number of instances. For example:
        
        * **Desired capacity**: 2 instances (the number of instances you want running under normal load).
            
        * **Minimum capacity**: 1 instance (this is the minimum number of instances that should always be running).
            
        * **Maximum capacity**: 5 instances (this is the maximum number of instances that can be running).
            

You can also set scaling policies based on metrics like **CPU utilization** or **network traffic**. For example, if CPU utilization exceeds 70% for a specified time, you can trigger the scaling policy to add another instance.

3. **Complete the Setup:**
    
    * Review all settings and click **Create Auto Scaling group**.
        
4. **Auto-Scaling in Action:**
    
    * Once the auto-scaling group is set up, it will automatically add or remove EC2 instances based on your defined scaling policies. You can monitor this behavior from the **Auto Scaling Groups** section in the EC2 dashboard.
        

---

With these tools, you can automate your AWS EC2 infrastructure, reduce errors, and scale your resources as needed. This provides a solid foundation for more advanced AWS cloud management tasks.

Happy Learning! 🌟
