AWS EC2 Automation ☁

☁️What is Amazon EC2?

Amazon Elastic Compute Cloud, often referred to as Amazon EC2, is like a virtual computer in the cloud. It allows you to rent these virtual computers, known as instances, on-demand. These instances can run various operating systems and software, providing you with the computing power you need without having to own or manage physical hardware.

☁️Why is Amazon EC2 Useful?

  1. Security: It runs on Amazon's secure infrastructure, helping to protect your data.

  2. Reliability: EC2 instances are highly reliable, with built-in features for fault tolerance.

  3. Performance: You can choose the instance type that suits your needs, from basic to high-performance options.

  4. Cost-Effectiveness: You only pay for what you use, making it cost-effective for businesses of all sizes.

☁️Automation in Amazon EC2:

Here are some key points about automation:]

  1. Launching Instances: Instead of manually setting up instances one by one, you can create scripts or use tools to automatically launch multiple instances at once. This is helpful for quickly scaling your computing resources.

  2. Configuration Management: You can automate the setup and configuration of your instances, ensuring they have the right software, settings, and security measures in place as soon as they are launched.

  3. Scaling: With automation, you can set rules to automatically add or remove instances based on your application's demand. For example, you can add more instances during peak hours and scale down when traffic decreases.

  4. Monitoring and Alerts: Automation allows you to set up monitoring for your instances and receive alerts when something goes wrong. This helps you respond to issues promptly.

  5. Backup and Restore: You can automate the process of creating backups of your instances and restoring them if needed. This ensures data safety and recovery.

  6. Patch Management: Automation helps in applying security patches and updates to your instances without manual intervention, reducing vulnerabilities.

  7. Load Balancing: Automation can manage the distribution of incoming traffic across multiple instances, ensuring even distribution and high availability.

☁️Launch Template in AWS EC2:

  • 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 in Amazon EC2

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 (Amazon Machine Image):

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: Setting Up Amazon EC2 Instances with Jenkins and Docker Using Launch Templates and Auto-Scaling (Optional)

1. Go to the AWS Management Console and navigate to the EC2 service.

2. Click on "Launch Instance" and choose an Amazon Ec2 instance.

3. Select an instance type, configure instance details, and add any required storage.

  1. Scroll down to the "Advanced Details" section. Enter the following script in the "User data" field to install Jenkins and Docker

      #!/bin/bash
    
       # Update the system packages
       sudo yum update –y
    
       # Download and configure the Jenkins repository
       sudo wget -O /etc/yum.repos.d/jenkins.repo \
           https://pkg.jenkins.io/redhat-stable/jenkins.repo
       sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
    
       # Upgrade the system packages
       sudo yum upgrade
    
       # Install Java 11 using Amazon Corretto
       sudo dnf install java-11-amazon-corretto -y
    
       # Install Jenkins
       sudo yum install jenkins -y
    
       # Enable and start the Jenkins service
       sudo systemctl enable jenkins
       sudo systemctl start jenkins
    
       # Install Docker
       sudo yum install -y docker
    
       # Enable and start the Docker service
       sudo systemctl enable docker
       sudo systemctl start docker
    
       # Check the status of Jenkins service
       sudo systemctl status jenkins
    
       # Check the status of Docker service
       sudo systemctl status docker
    

    5. Review the configuration, and launch the instance.

    6. Once the instance is running, copy the public IP address and configure Jenkins.

    7. In the "Network Settings", make sure the Security Group allows inbound traffic on port 8080 to access Jenkins.

Jenkins is installed and configured on machine, "Amazon AMI", t2-micro.

Jenkins is ready to be used.

  1. Once you ensure that everything looks good, select the instance and create a template out of it.

  2. Select the instance and go to Actions>Image and templates>Create template from instance.

    Give a name to the template. Add a description and version.

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

  • To create 3 instances using the launch template:
  1. Go back to the Amazon EC2 console.

  2. Navigate to "Instances" in the left navigation pane and click on "Launch template" dropdown menu. Select "Launch instance from template"

    1. Choose the launch template you created earlier from the list.

    2. In the "Number of instances" field, specify "3" as the number of instances to be launched.

    3. Review and configure other settings as needed.

Click on "Launch instances" to start the instances.

We can see 3 instances launched from the selected template:

You can go one step ahead and create an auto-scaling group 🤓

  1. Go back to the Amazon EC2 console.

  2. Navigate to "Auto Scaling Groups" in the left navigation pane and click on "Create Auto Scaling group".

  3. Configure the basic settings for the auto-scaling group, such as the launch template, desired capacity, and minimum and maximum instances.

  4. Set up scaling policies based on your requirements, such as scaling based on CPU utilization or other metrics.

  5. Configure other advanced options such as health checks, notifications, and tags.

  6. Review and confirm the settings.

    1. After you review all the steps, Click on "Create Auto Scaling group" to create the auto-scaling group.

You will receive an email for SNS (Simple Notification Service from AWS for confirmation) for subscription.