Image taken from stackoverflow.com
Now we have all the tools to create a robust server. In this exercise we will create a server that can auto scale, be load balanced and can handle instance failures according to requirements.
To do this create a load balancer as we did in last hands on but don't add any instance to it or if you are using the previous exercise load balancer terminate those instances
Go to Auto Scaling Groups under auto scaling in your EC2 instance dashboard. Click on Create Auto Scaling Group.
Fill the auto scaling group name and select a launch template of you have one if you don't have one select create new launch template. This launch template is a template which will be used to create our new EC2 instances for auto scaling.
- Select a name and description for our template
- Select the AMI for which you want this template. if you don't know select Amazon Linux 2 AMI (HVM), SSD Volume Type for the purpose of this tutorial this should be the first option
- Select the instance type as t2.micro (free tier eligible)
- A key pair name for ssh login. You can select one we created while creating an ec2 instance.
- Let network settings be VPC lets not get into what this is for the purpose of this tutorial
- Select a security group. Select the same one that we select while creating our EC2 instances
- Then there is storage, tags, network interfaces no need to touch them.
- Open Additional details and in user data paste the following script and click on create new launch template
#!/bin/bash # Use this for your user data (script from top to bottom) # install httpd (Linux 2 version) yum update -y yum install -y httpd systemctl start httpd systemctl enable httpd echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www /html/index.html
Now you will see configure settings screen which you can select that all your instances can adhere to launch template or you can mix and match them with spot or other instances I think that will be complex for now. So lets adhere to launch template
Next is load balancing. Select
Attach to an existing load balancer. Select a target group you created while creating the load balancer. Select ELB health checks it checks if the underlying structure is working correctly. If you want to enable cloud watch logs. Select additional settings monitoring else click next. I don't need cloudwatch logs so I left it unchecked.
Now to deploy an actual application you just have to expose the API over port 80 in deployment for HTTP or over port 443 for HTTPS in your instance and you have to setup SSL. Suggest a small opensource application if you want to see it deployed other wise I'm working on a small python Django project it is a work in progress we will deploy than in 20-30 days until that we have to wait. Until that this is as far as we go.
In the next tutorial we will see how to setup EC2 instances AMI so that we have pre configured EC2 instances on our disposal.