Day40: AWS EC2 Automation ☁☁

Day40: AWS EC2 Automation ☁☁

💨AWS EC2 (Elastic Compute Cloud):

Amazon Web Service EC2 is a web service which is provided by the AWS cloud which is secure, resizable, and scalable. These virtual machines are pre-configured with the operating systems and some of the required software. Instead of managing the infrastructure AWS will do that so you can just launch and terminate the EC2 instance whenever you want.

You can deploy your applications in EC2 servers without any worrying about the underlying infrastructure. You configure the EC2-Instance in a very secure manner by using the VPC, Subnets, and Security groups. You can scale the configuration of the EC2 instance based on the demand of the application by attaching the autoscaling group to the EC2 instance. The other advantage of AWS EC2 is that you need to pay only for how much you use it is like the pay-as-you-go model.


The main uses of AWS EC2 include:

  1. Scalability and Elasticity: This scalability helps ensure that your applications can handle increased traffic or workload spikes efficiently, without the need for upfront investments in servers or long-term commitments.

  2. Cost Optimization: With EC2, you pay only for the compute resources you use. This "pay-as-you-go" pricing model allows you to optimize costs by matching your infrastructure capacity to your actual needs.

  3. Variety of Workloads: It can be used for running web applications, hosting databases, performing data processing, running batch jobs, deploying containerized applications, and much more.

    EC2 instances can be customized and configured to meet specific requirements, making it versatile for different types of applications and workloads.

  4. Integration with AWS Services: EC2 seamlessly integrates with other AWS services, allowing you to build comprehensive and scalable solutions.

    For example, you can store data in Amazon S3, use Amazon RDS for managed databases, leverage AWS Lambda for serverless computing, or utilize Amazon VPC for networking and security. EC2 instances can interact with these services, providing a powerful ecosystem for building applications and infrastructure.

  5. Security and Compliance: AWS provides a secure and compliant infrastructure, and EC2 is designed with security in mind. You have control over network access, firewall settings, and security groups to protect your instances.

    AWS also offers various security features, such as IAM for access management, VPC for network isolation, and encryption options for data protection.

  6. Management and Monitoring: AWS provides tools and services for managing and monitoring EC2 instances. You can use AWS Management Console, AWS CLI, or SDKs to launch, configure, and manage instances.

    Additionally, services like Amazon CloudWatch allow you to monitor the performance and health of your instances, set up alerts, and automate actions based on predefined metrics.

    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:

EC2 provides a wide range of instance types, each designed to cater to different workload requirements. Instance types vary in terms of computing power, memory, storage, networking capabilities, and specialized hardware (such as GPUs for accelerated computing). You can choose the appropriate instance type based on your specific needs, optimizing performance and cost-efficiency for your workload.

Types of AWS EC2 Instances:

  1. General Purpose Instances

  2. Compute Optimized Instances

  3. Memory-Optimized Instances

  4. Storage Optimized Instances

  5. Accelerated Computing Instances

Instance types

📌🔎 To know more about types of AWS EC2 instances

AMIs (Amazon Machine Images):

When launching an EC2 instance, you start with an Amazon Machine Image (AMI). An AMI is a pre-configured template that contains an operating system, software packages, and configuration settings.

AWS provides a variety of pre-built AMIs, including popular operating systems like Linux and Windows. You can also create custom AMIs to capture specific configurations and application installations for reuse.

Use an AMI:

You can create your own customized AMI from an existing ec2-instance with the help of snapshots. After creating a snapshot you create a new ec2-instance by using the existing one know the new instance is the copy of an existing instance with all the required data in it. you can use that instance to create a new instance as shown in the following figure.


Automation of Amazon EC2:

Automation is a key aspect of managing and operating Amazon EC2 instances efficiently. AWS provides several tools and services that enable you to automate various aspects of EC2, including instance provisioning, configuration management, scaling, and monitoring.

Here are some of the automation capabilities in Amazon EC2

  1. AWS Systems Manager:

    ⚙Systems Manager provides a suite of tools for automating management tasks across EC2 instances and other AWS resources.

    ⚙ You can use Systems Manager Automation to create workflows that automate common administrative tasks, such as patching, software installation, and configuration updates across a fleet of EC2 instances.

  2. AWS Auto Scaling:

    ⚙Auto Scaling enables you to automatically adjust the number of EC2 instances based on predefined scaling policies. You can define scaling triggers based on metrics like CPU utilization, network traffic, or application response time.

    ⚙Auto Scaling can scale out (add instances) to handle increased demand and scale in (remove instances) during periods of low demand. It helps optimize resource utilization, maintain performance, and ensure high availability.

  3. AWS Lambda:

    ⚙Lambda is a serverless compute service that allows you to run code without provisioning or managing servers. You can trigger Lambda functions in response to events, such as changes in EC2 instance state or CloudWatch events.

    ⚙With Lambda, you can automate tasks like instance provisioning, backups, and configuration updates. For example, you can use Lambda to automatically start or stop EC2 instances based on a schedule or specific conditions.

    1. Infrastructure-as-Code Tools:

⚙Infrastructure-as-Code (IaC) tools, such as Terraform, AWS Cloud Development Kit (CDK), and AWS CloudFormation, enable you to define your infrastructure and configuration as code.

⚙These tools allow you to describe your desired infrastructure state in a declarative manner and automatically provision and manage resources like EC2 instances.

⚙IaC tools provide a powerful way to automate the deployment and management of your EC2 instances with version-controlled, repeatable configurations


Create a launch template with Amazon Linux 2 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.)

Creating a Launch Template:

  1. Log into AWS Console: Go to the AWS website and sign in with your AWS account credentials.

  2. Go on EC2 Instances. Click on "Launch Templates".

    1. Give Name to Launch Template and Version.

  1. Select Image, and give all the info like private key and user-data script to install Jenkins in our system. Click on Launch Template.

    1. create or choose a security group.

    2. In the "Advanced Details" section, paste the user data script for installing Jenkins and Docker in the "User data" field

      1. Choose "Create launch template". below you can see the template has been created.

Congratulations!! Your first Launch Template was Created Successfully.

  1. Also, we can create a template using EC2 Instance, Select the EC2 instance, click action, click Image and Templates, and click create a template from the instance.

  2. Create 3 Instances using Launch Template

    • To launch three instances using the launch template
  1. In the Amazon EC2 console, choose "Launch an instance from templates" in the left navigation pane.

  2. Select the launch template that you just created.

  3. Choose "Launch instances" to launch the instances and select '3' to create instances from a template.

Creating Auto-Scaling Group:

Step 1: Prepare Your Launch Template or Configuration:

Before creating an Auto-Scaling Group, ensure you have a launch template or configuration that defines the instance type, AMI, and other settings you want for your instances.

You can follow the steps mentioned earlier to create a launch template.

Step 2: Access the Auto Scaling Dashboard:

  1. Sign in to your AWS account and Go to the AWS Management Console.

  2. In the AWS Management Console, navigate to the EC2 Dashboard.

  3. Under the "Auto Scaling" section in the left sidebar, click on "Auto Scaling Groups."

    Step3: In the "Create Auto Scaling Group" page, enter a name for the auto-scaling group.

    • For "Launch Template", choose the launch template we created earlier.

      • For "Load balancing", choose any option as per your requirement.

  • In the "Group Size" page, enter the desired capacity for the auto-scaling group, such as 2.

Step 4: Configure Notifications (Optional):

You can set up email or SMS notifications to alert you when the Auto-Scaling Group scales up or down or when there are other events.

Step 5: Review and Create the Auto-Scaling Group:

  1. Review all the configuration settings you've made.

  2. Click the "Create Auto Scaling group" button to create the group.


    Automation capabilities in Amazon EC2 empower you to streamline and optimize the management and operation of your infrastructure. By automating tasks such as provisioning, configuration, scaling, monitoring, and maintenance, you can improve efficiency, reduce manual effort, ensure consistency, and enhance the overall reliability and agility of your EC2-based applications and workloads.

    Thank you for 📖reading my blog, 👍Like it and share it 🔄 with your friends.

    Happy learning😊😊