Day 40 AWS EC2 Automation ☁

Day 40 AWS EC2 Automation ☁

AWS EC2 Automation

Automating AWS EC2 (Elastic Compute Cloud) instances can be done using various tools and services provided by AWS. Here are some common approaches and tools for EC2 automation:

  1. AWS Management Console:

    • AWS provides a web-based console that allows you to manually launch, configure, and manage EC2 instances. While not strictly automation, you can create and save configurations, making it easier to launch instances with predefined settings.
  2. AWS Command Line Interface (CLI):

    • The AWS CLI is a command-line tool that allows you to interact with AWS services, including EC2, using commands. You can write scripts using the AWS CLI to automate tasks like launching instances, creating key pairs, and configuring security groups.

        Example AWS CLI command to launch an EC2 instance
        aws ec2 run-instances --image-id ami-12345678 --instance-type t2.micro --key-name MyKeyPair
      
  3. AWS SDKs:

    • AWS Software Development Kits (SDKs) are available for various programming languages, including Python, Java, and Node.js. Using SDKs, you can programmatically interact with AWS services, including EC2, to automate tasks in your preferred programming language.
  4. AWS CloudFormation:

    • AWS CloudFormation enables you to define and provision AWS infrastructure as code using a declarative template. You can create templates for EC2 instances, security groups, and other resources, making it easy to replicate and manage infrastructure consistently.
  5. AWS Elastic Beanstalk:

    • If you are deploying applications, AWS Elastic Beanstalk can simplify the process. It automates the deployment and scaling of web applications, including the underlying EC2 instances, load balancing, and more. It supports multiple programming languages.
  6. AWS Systems Manager (SSM):

    • AWS Systems Manager allows you to automate various operational tasks on EC2 instances. It provides features like Run Command, which allows you to run scripts remotely on instances without the need for SSH access.
  7. AWS Lambda:

    • You can use AWS Lambda to automate tasks in response to events or on a schedule. For example, you can use Lambda functions to start or stop EC2 instances based on certain conditions, reducing costs by shutting down instances during non-business hours.
  8. Auto Scaling:

    • AWS Auto Scaling allows you to automatically adjust the number of EC2 instances in a group based on demand or a schedule. This helps in ensuring the right amount of capacity at any given time.

Choose the automation approach based on your specific use case, requirements, and preferred tools. Combining different services and tools often provides a comprehensive solution for EC2 automation.

Automation in EC2:

Amazon EC2 or Amazon Elastic Compute Cloud can give you secure, reliable, high-performance, and cost-effective computing infrastructure to meet demanding business needs.

Launch template in AWS EC2:

  • You can make a launch template with the configuration information you need to start an instance. You can save launch parameters in launch templates so you don't have to type them in every time you start a new instance.

  • For example, a launch template can have the AMI ID, instance type, and network settings that you usually use to launch instances.

  • You can tell the Amazon EC2 console to use a certain launch template when you start an instance.

Instance Types:

Amazon EC2 has a large number of instance types that are optimised for different uses. The different combinations of CPU, memory, storage and networking capacity in instance types give you the freedom to choose the right mix of resources for your apps. Each instance type comes with one or more instance sizes, so you can adjust your resources to meet the needs of the workload you want to run.

AMI:

An Amazon Machine Image (AMI) is an image that AWS supports and keeps up to date. It contains the information needed to start an instance. When you launch an instance, you must choose an AMI. When you need multiple instances with the same configuration, you can launch them from a single AMI.

How to create an Amazon Machine Image (AMI) 2 ways!!! | by Johncle | Medium

Task1:

  • Create a launch template with Ubuntu AMI and t2.micro instance type with Jenkins and Docker setup (You can use the Day 39 User data script for installing the required tools.

    Sure, I can guide you through creating a launch template with Amazon Linux 2 AMI and t2.micro instance type, setting up Jenkins and Docker.

    Step 1: Create a Launch Template:

    1. Open the AWS Management Console.

    2. Navigate to EC2:

      • Click on "Services" in the upper left corner.

      • Under "Compute," select "EC2."

    3. Create a Launch Template:

      • In the EC2 Dashboard, on the left navigation pane, click on "Launch Templates."

  • Click the "Create launch template" button.

    1. Configure your Launch Template:
  • Choose the appropriate Amazon Machine Image (AMI), for instance, Ubuntu.

  • Choose the instance type as t2.micro.

  • In the "User data" section, you can provide scripts for Jenkins and Docker setup.

      #!/bin/bash

          # For update
          sudo apt update -y
          sudo apt install openjdk-11-jre-headless -y
          java -version 2>/home/ubuntu/jenkins_output.txt

          # For install Jenkins
          curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
            /usr/share/keyrings/jenkins-keyring.asc > /dev/null
          echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
            https://pkg.jenkins.io/debian binary/ | sudo tee \
            /etc/apt/sources.list.d/jenkins.list > /dev/null
          sudo apt-get update -y
          sudo apt-get install jenkins -y

          sudo systemctl enable jenkins
          sudo systemctl start jenkins
          sudo systemctl status jenkins >>/home/ubuntu/jenkins_output.txt

          # For install Docker
          sudo apt install docker.io -y
          sudo systemctl enable docker
          docker --version  2>/home/ubuntu/docker_output.txt
          sudo systemctl start docker >>/home/ubuntu/docker_output.txt
          sudo usermod -aG docker $USER
          sudo gpasswd -a $USER docker
  1. Save the Launch Template:
  • Click on "Launch instance from template."

Launch Instances using the Launch Template:

  1. Navigate to Instances:

    • In the EC2 Dashboard, on the left navigation pane, click on "Instances."
  2. Launch Instances:

    • Click the "Launch Instance from Template" button.

    • Select your created launch template.

    • You should find an option to specify the number of instances to launch. It might be labeled as "Number of instances" or something similar, depending on the AWS console interface at the time of use.

  • Proceed with the instance launch.

Create an Auto Scaling Group:

  1. Navigate to Auto Scaling Groups:

    • In the EC2 Dashboard, on the left navigation pane, click on "Auto Scaling Groups."
  2. Create an Auto Scaling Group:

    • Click the "Create Auto Scaling group" button.
  3. Configure Auto Scaling Group:

    • Choose your created launch template.

    • Configure the desired minimum, maximum, and desired capacity for the group.

    • Configure scaling policies based on your requirements. For instance, you can set up scaling policies based on CPU utilization to automatically scale in or out.

  4. Complete the Auto Scaling Group Setup:

    • Follow the prompts to complete the setup, including configuring notifications, tags, etc.

    • Click on "Create Auto Scaling group" to create the group.

By setting up an Auto Scaling group, your instances will automatically scale in and out based on the specified policies, ensuring that your application can handle varying levels of traffic without manual intervention.

That's it! You've just completed the task. 🎉

Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Happy Learning!