AWS EC2 Automation and AutoScaling

AWS EC2 Automation and AutoScaling

ยท

9 min read

Welcome back, DevOps and AWS enthusiasts! ๐ŸŒŸ

In my previous blog, "AWS and IAM Basics," we explored the fundamental aspects of Amazon Web Services (AWS) and Identity and Access Management (IAM).

If you haven't had a chance to read it, you can find it on my Hashnode Page.

Continuing our journey into AWS, let's delve into the Automation and Auto-Scaling capabilities of Amazon EC2, a key component in the AWS ecosystem.

What is AWS EC2?

Amazon Elastic Compute Cloud (EC2) is like your virtual computer in the cloud. It's a web service that lets you flexibly adjust your computing capacity. With EC2, you can run these virtual servers called instances right in the AWS cloud. The cool part is that the EC2 instances can easily grow or shrink based on your computing needs. It's like having a cloud-based power switch for your servers!

They provide you with control over the operating system, also over networking, storage, and various other aspects of your virtual server. This gives you a flexible and cost-effective solution to smoothly deploy and handle your applications in the cloud.

Features of EC2

  • Virtual Instances: Create and launch virtual servers (instances) with customizable configurations, operating systems, and types.

  • Elasticity: Easily scale instances based on demand, avoiding overprovisioning and ensuring optimal computing resources.

  • Instance Types: Diverse options including general-purpose, compute-optimized, memory-optimized, storage-optimized, GPU instances, and more.

  • Customizable: Full control over operating system, networking, security settings, and installed software for tailored environments.

  • Security: Utilize security groups, network access control lists (ACLs), and key pairs for controlled traffic and secure remote access.

  • Storage Options: Varied choices such as instance store and Amazon EBS for persistent and scalable block storage.

  • Load Balancing: Elastic Load Balancing (ELB) supports distributing incoming traffic across multiple instances for improved availability.

  • Auto Scaling: Set up automated adjustments to instance numbers based on predefined conditions for consistent application performance.

  • Monitoring and Logging: Amazon CloudWatch provides insights into performance metrics, utilization, and resource consumption. Enable logs for troubleshooting.

  • Networking: Instances are launchable in a Virtual Private Cloud (VPC) for enhanced network control, including configurable subnets, route tables, and network gateways.

  • Global Reach: Multiple regions and availability zones worldwide allow strategic deployment, reducing latency and improving performance.

  • Pay-as-You-Go Pricing: Cost-effective billing model where you are charged only for the resources you use, optimizing budget and infrastructure spending.

Amazon EC2 | AWS Security Blog

Pricing of EC2 Instances

AWS EC2 pricing is based on various factors, including instance types, regions, and usage. It's essential to understand the pricing model to optimize costs. Different regions may have varying prices due to factors like demand and infrastructure costs.

Instance Types

Amazon EC2 offers a diverse array of instance types optimized for various purposes. The unique combinations of CPU, memory, storage, and networking capacity within these instance types provide the flexibility to select the ideal resource mix for your applications. Each instance type includes multiple sizes, allowing you to fine-tune your resources according to the requirements of the workload you intend to run.

Instance TypeCPUMemory (GiB)Storage (GB)
t2.micro11EBS only
t2.small12EBS only
t2.medium24EBS only
t2.large28EBS only
t2.xlarge416EBS only
t2.2xlarge832EBS only
m5.large28EBS only
m5.xlarge416EBS only
m5.2xlarge832EBS only

Instance types in Amazon EC2 are categorized based on the specific purposes they are optimized for. These purpose-driven distinctions allow users to choose instances that align with their intended use cases, ensuring optimal performance for various workloads.

Instance TypeInstance SizesDescription
General Purposenano, micro, small, medium, large, xlarge, 2xlargeBalanced compute, memory, and network resources
Compute Optimizedlarge, xlarge, 2xlarge, 4xlarge, 8xlarge, 16xlarge, 32xlargeHigh-performance compute instances optimized for CPU-intensive workloads
Memory Optimizedlarge, xlarge, 2xlarge, 4xlarge, 8xlarge, 12xlarge, 16xlargeMemory-optimized instances for applications requiring high RAM capacity
Storage Optimizedlarge, xlarge, 2xlarge, 4xlarge, 8xlarge, 16xlarge, 32xlargeHigh-performance instances with local NVMe SSD storage optimized for storage-intensive workloads
GPU Instancesp2.xlarge, p2.8xlarge, p2.16xlarge, p3.2xlarge, p3.8xlarge, p3.16xlarge, p4d.24xlarge, g3s.xlarge, g3.4xlarge, g3.8xlarge, g3.16xlargeInstances with powerful GPUs for parallel processing and high-performance computing
FPGA Instancesf1.2xlarge, f1.4xlarge, f1.16xlargeInstances with FPGAs for hardware acceleration of specific tasks

Instance States

Understanding the different instance states, such as pending, running, stopping, and terminated, is crucial for effective instance management and troubleshooting.


The instance lifecycle.

Instance stateDescriptionInstance usage billing
pendingThe instance is preparing to enter the running state. An instance enters the pending state when it is launched or when it is started after being in the stopped state.Not billed
runningThe instance is running and ready for use.Billed
stoppingThe instance is preparing to be stopped.Not billed
stoppedThe instance is shut down and cannot be used. The instance can be started at any time.Not billed
shutting-downThe instance is preparing to be terminated.Not billed
terminatedThe instance has been permanently deleted and cannot be started.Not billed

EC2 Automation

EC2 Automation involves automating tasks related to Amazon Elastic Compute Cloud (EC2) instances on AWS. This includes processes like instance creation, configuration management, and scaling using tools such as AWS Systems Manager and AWS CloudFormation. It enhances operational efficiency and consistency in managing EC2 resources within a cloud environment.

Launch Templates

Creating a launch template allows you to consolidate the configuration information necessary for launching an instance. With launch templates, you store launch parameters, eliminating the need to specify them every time you initiate an instance.

For instance, a launch template may encompass critical details such as the AMI ID, Instance Type, and Network Settings commonly employed for instance launches. When initiating an instance through the Amazon EC2 console, an AWS SDK, or a command line tool, you have the option to designate the specific launch template to apply.

Advantages of Launch Templates

  • Versioning: Effortlessly oversee multiple versions of launch templates, ensuring streamlined management and easy tracking of changes.

  • User Data Support: Enable customization by passing user data to instances, facilitating tailored configurations based on specific requirements.

  • Flexibility: Seamlessly integrate with Auto Scaling Groups to achieve dynamic scaling, providing adaptability and responsiveness to fluctuating workloads.

EC2 AutoScaling

Auto Scaling automates the adjustment of the EC2 instance count in a group, ensuring optimal performance and meeting demand. This feature guarantees that your preferred number of instances is consistently running, even in the event of failure for one or more instances.


            An illustration of a basic Auto Scaling group.

AMI (Amazon Machine Image)

An Amazon Machine Image (AMI) is a pre-configured template that holds essential information for launching an instance in Amazon EC2. It serves as the foundational element for constructing virtual servers in the cloud.

AMIs come in three types: Amazon-provided AMIs, AWS Marketplace AMIs, and Custom AMIs.

Key components of an AMI include the Root Volume, Instance Configuration, Permissions, Block Device Mappings, and Launch Permissions. Together, these elements define the blueprint for creating and deploying instances in Amazon EC2.

EC2 Automation vs. AutoScaling

While EC2 automation with launch templates focuses on efficient provisioning, Auto Scaling ensures optimal performance and availability by dynamically adjusting the number of instances.

Task 01

Create a launch template with Amazon Linux 2 AMI and t2.micro instance type with Jenkins and Docker setup.

  1. Go to the AWS login page and select Root user.

  2. Enter "EC2" in the search box.

  3. Navigate to the left-hand side and select "Launch Templates." From there, choose "Create launch template."

  4. On the "Create a launch template" page, input a name for the launch template.

  5. When selecting the "Amazon Machine Image (AMI)," choose "Amazon Linux 2."

  6. Under "Instance type," select "t2.micro."

  7. In the "Advanced Details" section, paste the user data script designed for installing Jenkins and Docker into the designated "User data" field.

    Feel free to duplicate the updated shell script provided below.

     #!/bin/bash
     sudo yum update โ€“y
     sudo yum install docker
     sudo systemctl start docker
     sudo systemctl enable docker
     sudo usermod -aG docker $USER
     sudo wget -O /etc/yum.repos.d/jenkins.repo \ pkg.jenkins.io/redhat-stable/jenkins.repo
     sudo rpm --import pkg.jenkins.io/redhat-stable/jenkins.io-202..
     sudo yum upgrade
     sudo dnf install java-11-amazon-corretto -y
     sudo yum install jenkins -y
     sudo systemctl enable jenkins
    
  8. Select "Create launch template," and you'll observe the template has been successfully generated below.

Task 02

Create 3 Instances using Launch Template, there must be an option that shows the number of instances to be launched, can you find it?

  1. Launch the 3 instances using the provided launch template.

    Navigate to the "Launch instance from templates" option in the left navigation pane within the Amazon EC2 console.

  2. Choose the launch template you've recently generated.

  3. In the "Number of instances" field on the right side, indicate the desired quantity of instances you wish to launch. Customize other configuration settings as needed, including VPC, subnet, security group, and more.

    Choose "Launch instances" to launch the instances.

  4. Check that 3 instances have been generated from the template.

Let's take a step further by establishing an auto-scaling group.

  1. Navigate to "Auto Scaling Groups" in the left navigation pane, and then select "Create Auto Scaling Group."

  2. On the "Create Auto Scaling Group" page, provide a name for the auto-scaling group.

    Under "Launch Template," select the launch template we created earlier.

  3. Under "Network," choose the VPC and subnet where you want the instances to be launched.

  4. Regarding "Load balancing," pick an option based on your specific requirements.

  5. In the "Group Size" section, input the desired capacity for the auto-scaling group, for instance, set it to 2.

  6. Under "Scaling policies," choose to configure scaling policies based on diverse metrics such as CPU utilization, network in/out, and others. Choose the target tracking policy for the policy type.

  7. Review.

  8. Select "Create Auto Scaling Group" to initiate the creation of the auto-scaling group.

  9. The auto-scaling group has been successfully created.

  10. After some time, the auto-scaling group will initiate the launch of instances according to the specified launch template and configuration. Here, you can observe the addition of two new instances launched by the auto-scaling group.

Conclusion

Embracing EC2 automation and Auto Scaling is pivotal for achieving efficiency, reliability, and cost-effectiveness in your AWS infrastructure. As you continue your AWS journey, integrating these features will contribute to a more resilient and adaptable cloud environment.

If you found this blog helpful or have questions, feel free to connect with me on LinkedIn.

Check my other blogs on DevOps here.

Let's build a network of like-minded professionals exploring the vast world of cloud computing together!

ย