# EC2 instance Autoscaling

In this Lab you will get the idea of the following:

* Created an Amazon EC2 launch template
    
* Created an Amazon EC2 Auto Scaling launch configuration
    
* Configured scaling policies and created auto Scaling group to scale in and scale out the number of servers based on variable load
    

### You need to first create these for this lab:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687491105998/221e2fd3-9812-4661-8ce0-6608e5ee8b4c.jpeg align="center")

* VPC with subnet 10.0.0.0/16
    
    * For Availability Zone 1
        
        * public subnet: 10.0.0.0/24
            
        * private subnet: 10.0.1.0/24
            
    * For Availability Zone 2
        
        * public subnet: 10.0.2.0/24
            
        * private subnet: 10.0.3.0/24
            
* Security Group
    
    * Inbound rule: HTTP and SSH
        

\[ Visit my previous blogs for Creating VPC and Security group if you had problem \]

## Let's begin:

1. In EC2 -&gt; Load Balancing session select Load Balancers and Create load balancer Select Application load balancer
    

* Basic configuration
    
    * Load balancer name: `WebELB`
        
    * Select VPC: `myVPC`
        
    * Mappings: `Select at least two AG so select both public subnets of different AG`
        
* Security groups
    
    * Security group: Select that you created to access HTTP
        
* Listener and routing
    
    * Protocol: HTTP Port: 80
        
    * Create target group as:
        
        * Target group name: `web-app`
            
        * VPC: `myVPC`
            
            Next
            
        * Register the targets session leave it as default and
            
            **<mark>Create target group</mark>**
            

\-Go back to the load balancer tab, refresh, and select the target group that you created

1. Creating a launch template
    

* In the EC2 left bar there is an instances section in which Launch Templates also select and create launch templates as:
    
    * Launch template name: `webtemp`
        
    * Template version description: `For autoscaling use`
        
    * Select `Provide guidance to help me set up a template that I can use with EC2 Auto Scaling`
        
    * Application and OS Images: select `My AMIs` and leave the default option
        
    * Instance type: `t3.micro` or other free tiers if exits
        
    * Key pait(login)-&gt; Key pair name: `don't include in the launch template`
        
    * Network Settings-&gt; Subnet: `Don't include in launch template` Firewall(security group): Select the existing security group and select that you created for HTTP access
        
    * Leave all defaults and **<mark>Create launch template</mark>**
        

1. Creating an Auto Scaling group
    

* Choose the Launch template that we created and From `Action` dropdown select Create Auto Scaling group
    
    * Name -&gt;
        
        * Auto Scaling group name: `Web Auto Scaling Group`
            
        * All Default and `Next`
            
    * Network -&gt;
        
        * VPC: `myVPC`
            
        * Availability Zones and subnets: `Select both private subnets`
            
        * Choose `Next`
            
    * Load balancing -&gt;
        
        * Select Attach to an existing load balancer
            
            * Existing load balancer target groups: Select `web-app` that you created
                
        * Health checks:- Select `Turn on Elastic Load Balancing health checks`
            
        * Choose `Next`
            
    * Configure group Size and scaling policies - optional
        
        * \-&gt; Group size - optional
            
            * Desired capacity: `2`
                
            * Minimum capacity: `2`
                
            * Maximum capacity: `4`
                
        * \-&gt; Scaling policies - optional
            
            * Select Target tracking scaling policy
                
                * Metric type: choose `Average CPU utilization`
                    
                * Target value: `50`
                    
                * Choose `Next`
                    
    * Add notifications - optional
        
        * Choose `Next`
            
    * Add tags - optional
        
        * Choose Add tag Key: Name Value-optional: `WebApp`
            
    * Review and **<mark>Create Auto Scaling group</mark>**
        

1. Verify autoscaling
    

* Select instances and you see there are 2 `WebApp` named instances are automatically created
    
* Select Target Groups and select that you created above and observe health status -&gt; healthy
    

1. Test auto-scaling configuration
    

* When the Load of CPU utilization reaches 50%+ after a few minutes you should see your auto-scaling group add a new instance. This occurs because Amazon CloudWatch detected that the average CPU utilization of your Auto Scaling group exceeds 50% and your scale-up policy has been invoked in response. The maximum of 4 instances of name WebApp created according to policy. You can add more if needed.
    

## Have a great day!!!
