# #90daysofdevops #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.

## 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](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-templates.html)

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

### 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 number of instances to be launched ,can you find it? :)
    
* You can go one step ahead and create an auto-scaling group, sounds tough?
    

## **Task 1: Create a template**

1. Navigate to Launch template section in AWS and create a template.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681614313881/e31fb0e7-0c97-4350-aabf-01b9e45216ca.png?auto=compress,format&format=webp align="left")
    
2. Provide the AMI as Amazon Linux 2 as per requirement.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681614342872/f5c42fdb-61eb-48d3-9d89-ff4bdda02499.png?auto=compress,format&format=webp align="left")
    
3. Provide the shape of the EC2 instance as per the required CPU and Memory.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681614854904/e6331c3e-d933-44a7-ac14-3201bef2994b.png?auto=compress,format&format=webp align="left")
    
4. Select the subnet and security group.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681614448641/f193c8cf-3498-483f-86dc-a199be218eb8.png?auto=compress,format&format=webp align="left")
    
5. In the additional section, you can provide a shell script to install docker and Jenkins as pre-requisite to the server.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681614486078/82106ac5-9eaa-4e43-a4d9-56265c3a6b03.png?auto=compress,format&format=webp align="left")
    
6. Now click to create template and then navigate to Action section to launch instance out of created template.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681614657084/5ba53e7e-b4ac-4d59-b3b1-5aac5e871a71.png?auto=compress,format&format=webp align="left")
    
7. Provide the desired number of instance as shown.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681614750502/74401283-c446-4012-86a1-1b8a92838824.png?auto=compress,format&format=webp align="left")
    
8. Navigate to the instance section in the EC2 dashboard and the instances will be now created. Here we have created 3 instances.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681615023661/e97ea64e-00b7-476b-a862-be9ebf9892d5.png?auto=compress,format&format=webp align="left")
    

## **Task 2: Create an Auto-Scaling group.**

1. Navigate to the Auto scaling group section and give a name to the group that will be created. Select the template that we have created in the above task.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681615291529/f4b66837-d9c2-433c-985e-e9855bddc24a.png?auto=compress,format&format=webp align="left")
    
2. Provide the subnet you want to keep your servers at.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681615669521/5f92129d-5f3e-4163-99ba-1ebacb1b5085.png?auto=compress,format&format=webp align="left")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681625657636/5680d6fb-aeb6-44bd-a02b-9b319b991216.png?auto=compress,format&format=webp align="left")
    
3. Provide the desired capacity and maximum capacity of the group to create servers.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681626009721/2a888a44-9a76-499b-8086-8cb502e0257d.png?auto=compress,format&format=webp align="left")
    
4. Review and now create the group.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681626282613/e628d07e-cc05-4141-8805-d3f17e0df128.png?auto=compress,format&format=webp align="left")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681626408124/a5941d26-0565-4376-b54c-0a9a77598270.png?auto=compress,format&format=webp align="left")
    
5. Now, navigate to the instance section and you can see 6 servers.
    
    Out of 6, 3 servers were created for above task 1 and the rest 3 are spun up by the Auto-Scaling group as the desired field we gave is 3.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1681626653826/a7136c53-80aa-452c-92ba-3205734d5918.png?auto=compress,format&format=webp align="left")
