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

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

Open the Amazon EC2 console.

In the left navigation pane, choose "Launch Templates".

Choose "Create launch template".

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805332541/79e87bd1-abc8-4d26-8763-aec013e18361.png align="left")

In the "Create a launch template" page, enter a name for the launch template

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805349536/37ce2a48-d916-4221-be9b-08ccbe07345f.png align="left")

For "Amazon Machine Image (AMI)", choose "Amazon Linux 2"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805372193/3da38851-e107-457f-9db6-ad136c3ddb86.png align="left")

For "Instance type", choose "t2.micro"

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805418445/7e112830-6c65-41fc-8200-00c83741e816.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805438696/1e5941a3-4a41-4ea5-a7c2-06f242346dc8.png align="left")

In the "Advanced Details" section, paste the user data script for installing Jenkins and Docker in the "User data" field.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805457403/db07a7ad-60a7-478d-9ff8-06237302735e.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805474079/f83700cd-96b2-45a8-8b37-58644942185f.png align="left")

Choose "Create launch template". below you can see template is created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805507815/c323a411-e57e-4ca4-b613-53d970eec2d2.png align="left")

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

**To launch three instances using the launch template**

In the Amazon EC2 console, choose "Launch instance from templates" in the left navigation pane.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805525355/f8ca8bb6-566b-42f2-81ec-413c5eefe420.png align="left")

Select the launch template that you just created.

Specify the number of instances you want to launch in the "Number of instances" field on right side. Choose the other configuration settings as desired, such as VPC, subnet, security group, and so on.

Choose "Launch instances" to launch the instances.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805587395/7f6ad87c-b8c1-483a-a198-14e5f6779f3d.png align="left")

You can see three instances created from template.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805603516/f7dd2f1e-c68b-4fd8-9d6b-7227c3bb47cb.png align="left")

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

In the left navigation pane, choose "Auto Scaling Groups".

Choose "Create Auto Scaling Group".

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805660569/b3fffca0-5015-4a60-b51c-785a55a9d06d.png align="left")

In the "Create Auto Scaling Group" page, enter a name for the auto-scaling group.

For "Launch Template", choose the launch template we created earlier.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805741496/729103b5-86fe-4420-b340-dd18595268e3.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805768546/5717b4b9-a009-423b-a028-351347e0e019.png align="left")

For "Network", choose the VPC and subnet you want the instances to launch in.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805782083/c05c8a80-4832-4c40-9074-8b47b923d637.png align="left")

For "Load balancing", choose any option as per your requirement.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805793666/c15d099e-1728-4cd6-8749-12dce879ef1d.png align="left")

In the "Group Size" page, enter the desired capacity for the auto-scaling group, such as 2.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805804770/1f367503-d5ed-418d-bd61-26b8f4929dd6.png align="left")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805816302/0d9f6b47-c9da-443a-a22b-972351b93dae.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805831859/3ed34f0a-8e01-400a-8494-c5b5164ef75c.png align="left")

Choose "Create Auto Scaling Group" to create the auto-scaling group.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805863600/03002eef-6b12-41c3-b63d-f41332440cce.png align="left")

Autoscaling group is created.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805881012/f0a2b449-3598-4808-8b44-598d0fc52b10.png align="left")

After a few moments, the auto-scaling group will launch the desired number of instances based on the launch template and the configuration you specified. below two new instances launched by auto-scaling group.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1711805888620/707e2df6-db50-4c03-be08-76d1d3640b72.png align="left")

Thank you for reading!
