There is  various uses for AWS Application Load Balancer one is to routing by path, in this lab we going to learn how to make a simple use case, distribute the traffic using the path.

For the user case lets assume you want to divide the load content between two paths, work and images, so we need to create two target groups for each path.

Requirements

  • AWS account

Hands-On

  1. In the AWS Management Console go to EC2

2. Go to instances

3. Click on Launch instances

4. Select Amazon Linux 2 AMI - 64 bits

5. Select t2.micro and click on Next

6. In the next window make sure you have the Auto-assign Public IP enabled, select 1 instance, select the sub-network leave everything else on defaults and click Next

7. In the Add Storage step you can leave it as default, and click on Next

8. In Add tags, leave as default and click Next

9. In the next window, create a new security group and give it a name, you will have as default the port 22 open to connect via ssh, click on Add Rule and add the port 80 for the nginx server (type HTTP), and click Review and Launch

10. Review the information and click on Launch

11. In the next window, select Create a new key pair, give it a name, download and click on Launch instances

12. You will see the Launch status, if everything is correct, you will have a confirmation, click on View Instances

13. Repeat the process of launching a instance, but with a different sub-network.

14. After you add the second instance go to instances, select the first instance, copy the private IPv4 Address for later,  and click on Connect

15. On the EC2 Instance Connect, copy the IP address for later and click on Connect

16. This will open a new window with an emulated terminal in the instance, now install nginx

sudo su -
amazon-linux-extras install -y nginx1

17. Check the status of nginx

systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: di
sabled)
   Active: inactive (dead)

Dec 14 21:04:22 ip-172-31-29-155.us-east-2.compute.internal systemd[1]: Unit nginx...
Hint: Some lines were ellipsized, use -l to show in full.

18. Start the nginx server

systemctl start nginx

19. Open in a new browser tab the public IP to check is working

20. Now lets customize the landing page, we going to direct the traffic of work to this server, we need to create a work path and an index.html, then use exit to close the connection, and close the tab or window.

cd /usr/share/nginx/html
mkdir work
cd work
touch index.html
echo "This is the work server" > index.html
exit
exit

21. Refresh and you will see the custom message

22. Repeat the steps with the second instance, click on instances

23. Select the second instance and click on Connect

24. On the EC2 Instance Connect tab, copy the public IP address for later, click on Connect, this will open a emulated terminal on a new browser window.

25. Install nginx

sudo su -
amazon-linux-extras install -y nginx1

26. Check the status of the nginx server

systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: di
sabled)
   Active: inactive (dead)

Dec 14 21:21:11 ip-172-31-24-113.us-east-2.compute.internal systemd[1]: Unit nginx...
Hint: Some lines were ellipsized, use -l to show in full.

27. Start the nginx server

systemctl start nginx

28. Open a new tab o browser window and open the public IP address to check the nginx installation

29. Now lets customize the landing page, we going to direct the traffic of images to this server, we need to create a images path and an index.html, then use exit to close the connection, and close the tab or window.

cd /usr/share/nginx/html
mkdir images
cd images
touch index.html
echo "This is the images server" > index.html
exit
exit

30. Refresh the browser to see the second message

31. Go to EC2, Target Groups

32. Click on Create target group

33. Select IP address as type, give it the name of work and click on Next

34. Add the private address of the work server on the IP and click on Create target group

35. Click on Create target group

36. Select IP address as type, give it the name of images and click on Next

37. Add the private address of the images server on the IP and click on Create target group

38. Go to Load Balancers

39. Click on Create Load Balancer

40. Select the Application Load Balancer

41. Give it a name

42. Select the AZ the instance are using and click Next

43. You can ignore the warning as for this lab we are not going to use the port 443, click Next

44. In the next screen create a new security group with 80 port open and click Next

45. In the next screen select an Existing target group as Target group, and use one of the target groups we created, I'm going to use work for now, and click Next

46. Click Next

47. In the review, click Create

48. You will see the creation status, if everything is OK, click on Close

49. In the Load Balancer page, select the load balancer we just create, and select the Listeners tab

50. Click on View/edit rules

51. Select the Edit tab

52. And edit the default rule, in this rule we going to ask for a path if the user don't give any.

53. Click on the trash icon to remove the action

54. Add an action, select Return fixed response

55. As Return response code, write 200, and in the Response body put "put a path, either work or images" and click on the check mark.

56. Click on Update

57. Go back

58. In the load balancer page, select the load balancer, and in the tab Description, copy the DNS name

59. If you put the dns name in the browser you will see the custom message

60. Now lets add the work and images paths, Go again to the listeners tab, selecting the load balancer and select View/edit rules

61. Select the + sign to add a new rule

62. Click on Insert Rule

63. Click on Add condition and select Path..

64. Select /work/ as condition and click on the check to save

65. Click on Add action and select Forward to...

66. Select work as target group

67. Click on the Check to save on click on the button Save on the top right

68. Add a new rule on top

69. And add the same configuration, this time use /images/ as path and the images target group

70. If you go to the load balancer DNS again you will see our custom message

71. and if you add at the end the path /work or /images you will be see each server message

Clean-Up

  1. On EC2, Load Balancers, select the load balancer and click Actions, Delete

2. Confirm

3. Go to Target groups, select the target group and click on Actions, Delete and confirm

4. Go to Security groups and select the security groups we create with this lab and delete them, selecting them and on Actions clicking on Delete security group

5. Select the instances on EC2, and click on Instance state and click on Terminate instance and confirm