Automation in EC2
Amazon EC2 allows you to automate many routine tasks to improve efficiency and reduce manual operations and maintenance costs. Some tasks you can automate in EC2 are:
• Instance launching - You can write scripts or use AWS services like CloudFormation to automatically launch new EC2 instances based on templates. This ensures consistency and repeatability.
• Instance termination - You can set up auto scaling policies or write scripts to automatically terminate idle or unused instances to save costs.
• Backup and snapshotting - You can automate the creation of EBS volume snapshots on a schedule to ensure your data is regularly backed up.
• Security updates - You can automate the installation of OS and software security updates on your EC2 instances to keep them patched and secure.
• Scaling - You can set up auto-scaling groups that automatically scale your EC2 fleet up or down based on metrics or schedules. This helps handle fluctuations in demand.
• Configuration management - You can use tools like Chef, Puppet or Ansible to automate and standardize the configuration of your EC2 instances.
• Monitoring - You can configure CloudWatch alarms to trigger automated actions when certain thresholds are met, like terminating unhealthy instances.
Automating routine tasks in EC2 helps improve reliability, security and cost efficiency. It reduces the need for manual operations and human error. Automation also enables a more dynamic and scalable infrastructure that can quickly respond to changes in demand.
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.
Task 1: Creating a Launch Template, Launching Instances, and Setting Up an Auto Scaling Group in AWS EC2
- 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.
Step 1: Create a Launch Template
Navigate to the EC2 Dashboard: Go to your AWS Management Console, then under the "Compute" section, click on "EC2".
Create a Launch Template: In the EC2 Dashboard, click on "Launch Templates" in the left navigation pane, and then click the "Create launch template" button.
Configure Template Details:
Provide a name and description for the launch template.
For "AMI ID," select the Ubuntu AMI.
For "Instance type," select t2.micro.
Under "Advanced Details," provide your user data script that installs Jenkins and Docker (you mentioned using Day 39 User data script).
In the “Advanced Details” section, paste the user data script for installing Jenkins and Docker in the “User data” field.
#!/bin/bash
sudo apt update
sudo apt install openjdk-11-jre -y
curl -fsSL https://pkg.jenkins.io/debian-stable/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-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins -y
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo apt-get update
sudo apt-get install docker.io -y
sudo systemctl start docker
Choose “Create launch template”. below you can see template is created.
Create 3 Instances using Launch Template, there must be an option that shows number of instances to be launched.
Step 2: Launch Instances using the Launch Template
Navigate to Launch Instances: In the EC2 Dashboard, click on "Instances" in the left navigation pane, and then click the "Launch Instances" button.
Select Launch Template: Choose the "My launch template" tab and select the launch template you created in the previous step.
Configure Instance Details: Configure settings like instance count, network settings, IAM role, etc.
Add Storage: Configure the storage settings as needed.
Configure Security Group: Configure security group settings as needed.
Review and Launch: Review your settings and click the "Launch" button.
You can see three instances created from template.
Task 2: Create an Auto Scaling Group
Creating an Auto Scaling Group will automatically manage the number of instances running based on certain conditions like CPU utilization, network traffic, etc.
Navigate to Auto Scaling Groups: In the EC2 Dashboard, click on "Auto Scaling Groups" in the left navigation pane, and then click the "Create Auto Scaling group" button.
Configure Group Details:
Choose the launch template you created earlier.
Configure the group size settings, such as desired, minimum, and maximum instances.
Configure Scaling Policies: Set up scaling policies based on conditions like CPU utilization.
Configure Notifications and Tags (Optional): Configure notifications and add tags if needed.
Review and Create: Review your settings and click the "Create Auto Scaling group" button.
For “Network”, choose the VPC and subnet you want the instances to launch in.
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.
- Review the details and click on "Create Auto Scaling group" to create the group.
Autoscaling group is created.
"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.
Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/) for the latest updates and discussions.