Day 40 - AWS EC2 Automation

Day 40 - AWS EC2 Automation

Automation in EC2:

Amazon EC2 or Amazon Elastic Compute Cloud can give you secure, reliable, high-performance, and cost-effective computing infrastructure to meet demanding business needs.

Also, if you know a few things, you can automate many things.

Read from here

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.

Read more from here

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.

Read from here

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.

Task1:

  • 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.

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

  • You can go one step ahead and create an auto-scaling group, sounds tough?

Check this out

Steps:

  1. Log in to the Amazon Ec2 console and navigate to EC2

  2. From the left navigation menu, click on Launch Templates and on create launch template

  3. In the "Launch template name" field, enter a name for the template, such as "JenkinsandDockersetup"

  4. In the "Description" field, enter a brief description of the template.

  5. Under quick Start select the type of machine you wanted to create i.e Amazon Linux

    Amazon Machine Image (AMI) as "Amazon Linux 2 LTS AMI (HVM)

    "Instance type as "t2.micro."

    select the key pair you want to use for SSH access to the instance.

  6. Under "Network settings," select the VPC, Subnet and security group where you want to launch the instance.

    Enable auto-assign PublicIP

  7. Add volumes under Storage (volumes)

  8. Under "Advanced details," in User data paste the Day 39 script for installing Jenkins and Docker. This script will automatically install Jenkins and Docker on the instance when it launches.

  9. Click on the "Create launch template" button.

    Jenkinsanddockersetup name launch template is created successfully.

    Launching Instances Using the Launch Template

    Now that you have created a launch template, you can use it to launch instances quickly and easily.

    1. From the EC2 console, click on "Instances" in the left navigation menu.

    2. Click on the "Launch instances" button.

    3. Select the "Launch instance from templates"

    4. Select the launch template you created earlier and give the number of instances you wanted

    5. Click on the "Launch instances"

  1. And that's it! Amazon EC2 will now launch the instances using the launch template you created earlier.

  2. Now connect to the instances and check if the docker and Jenkins is installed.

Creating an Auto Scaling Group Using a Launch Template

An auto-scaling group allows you to automatically adjust the number of instances running based on the demand for your application. This can help you ensure that you have enough capacity to handle spikes in traffic while minimizing costs during periods of low demand.

  1. Open the Amazon EC2 console.

  2. Click on the "Auto Scaling Groups" link in the left-hand menu.

  3. Click on the "Create Auto Scaling Group" button.

  1. It is a name and select the launch template that we previously created

  1. Provide the vpc and subnet details

  2. For the load balancer it is optional

    6. Provide the desired auto-scaling numbers

  3. For "Scaling policies", you can choose to set up scaling policies based on various metrics such as CPU utilization, network in/out, and others. Choose the policy target tracking policy.

    8. Review all the details and click on create an auto-scaling group


Creating a launch template from exiting EC2

  1. Create an EC2 instance

  2. Select the instance from where you want to launch the template

  1. Click on Create template from instance and start configuring the template

  2. Just give it a name, Ubuntu version, SG and VPC all will be taken from the instance from which we launched this template

  1. Under Advanced details > User data write the commands that you want to install in the ec2 while launching it

Click on Create launch template

  1. Open the template that you just created and go to Actions > Launch instance from template

Just review and click on Launch instance.

An instance is successfully launched

Connect to the instance and check if the Git is installed or not