Day 40 Task: AWS EC2 Automation โ
This is #90DaysofDevops challenge under the guidance of Shubham Londhe sir.
Introduction
Amazon EC2, or Amazon Elastic Compute Cloud, provides a robust computing infrastructure for businesses. In this blog post, we'll explore the world of automation within EC2, focusing on launch templates, instance types, AMIs, and practical tasks to enhance your workflow.
Automation in EC2
Amazon EC2 offers secure, reliable, high-performance, and cost-effective computing infrastructure. With the right knowledge, you can unlock the potential for automation, streamlining various processes and tasks.
Launch Template in AWS EC2
Launch templates in AWS EC2 simplify the process of starting instances by saving configuration information. This includes essential details like AMI ID, instance type, and network settings. By utilizing launch templates, you eliminate the need to input these parameters every time you launch a new instance. Learn more about creating and using launch templates in the official documentation.
Instance Types
EC2 provides a diverse range of instance types, each optimized for specific use cases. These instances offer varying combinations of CPU, memory, storage, and networking capacity. This flexibility allows you to choose the perfect mix of resources for your applications. For a detailed understanding, refer to the official documentation.
Amazon Machine Image (AMI)
An Amazon Machine Image (AMI) is a pre-configured image supported and maintained by AWS. It contains all the necessary information to start an instance. When launching instances with identical configurations, AMIs streamline the process. Dive deeper into AMIs in the official documentation.
Task 1:
Step 1: Access the EC2 Dashboard
Navigate to the AWS Management Console and access the EC2 Dashboard.
Step 2: Launch Template Creation
In the EC2 Dashboard, select "Launch Templates" from the left-hand menu.
Click on the "Create launch template" button.
Step 3: Configure Launch Template
Choose AMI and Instance Type:
Set the AMI to "Amazon Linux 2".
Choose the "t2.micro" instance type.
Add User Data Script:
In the "Advanced details" section, locate "User data."
Insert the Day 39 User data script for Jenkins and Docker setup.
#!/bin/bash # Update the package repositories and installed packages sudo yum update -y # Install Java Development Kit (JDK) 11 sudo yum install -y java-11-amazon-corretto-devel # Add the Jenkins repository to yum sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo # Import the Jenkins repository GPG key sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key # Install Jenkins sudo yum install -y jenkins # Start Jenkins service and enable it to start on boot sudo systemctl enable jenkins sudo systemctl start jenkins # Install Docker sudo yum install -y docker # Start Docker service and enable it to start on boot sudo systemctl enable docker sudo systemctl start docker
Provide Template Details:
Give your launch template a name and description for easy identification.
Save Launch Parameters:
- Save additional launch parameters like network settings if necessary.
Create Launch Template:
Click on "Create launch template" to save your configuration.
Step 4: Launch Instances Using Launch Template
Return to the EC2 Dashboard.
Click on "Launch Templates" in the left-hand menu.
Select the launch template you created in Step 3.
Choose the "Launch Instance" option.
Pick the launch template you created in Step 3.
Adjust the number of instances to be launched as needed.
๐ Congratulations! We have successfully created instances from the launch template.
Step 5: Confirm Auto-Scaling Group (Optional)
If you wish to create an Auto-Scaling Group:
In the EC2 Dashboard, select "Auto Scaling Groups."
Click on "Create Auto Scaling group."
Follow the prompts to set up your Auto Scaling group, specifying desired configurations.
Choose your launch template created in Step 3 to configure the auto-scaling group's instance specifications.
Define scaling policies based on metrics such as CPU utilization, network traffic, or custom metrics to automatically adjust the number of instances in response to workload changes.
Configure health checks to monitor the status of your instances and replace unhealthy instances automatically.
Set up notifications to receive alerts when scaling events occur or when instances fail health checks, ensuring timely response to any issues.
Review and confirm the auto-scaling group configuration before finalizing the setup.
Now, observe that 4 instances are automatically launched by the auto-scaling group as we have set the desired capacity to 4.
- If any instance is terminated, the auto-scaling group will automatically scale to replace it.
- You will notice that after terminating an instance, the auto-scaling group creates 2 new instances automatically.
- Moreover, our Jenkins and docker is functioning correctly. You can verify by taking any IP and accessing it through the browser, as during the creation of the instance template, we added a shell script in the user data.
- Hurray! Our auto-scaling group is creating instances automatically based on the set policy to replace any terminated instances promptly.
Conclusion:
You have successfully created a launch template, launched instances using the template, and optionally explored creating an Auto-Scaling Group. This approach enhances efficiency and scalability in your AWS environment.
Thank you for taking the time to read! ๐
Subscribe to my newsletter
Read articles from Akash Dhengale directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Akash Dhengale
Akash Dhengale
DevOps Enthusiast || BCA Grad || MCA Pursuing | AWS, Docker, Kubernetes || Automation Specialist || Git & GitHub || Terraform || Seeking Entry-Level Opportunities.