Table of Contents
AWS ALB Setup
Prerequisite
If you don’t have one, create a default vpc
1 |
aws ec2 create-default-vpc |
Get the aws cli if you don’t already have it
https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html
Configure your aws cli
https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html
Test to make sure you have version 2 of the aws cli installed
1 |
aws elbv2 help |
Get the AMI id of the free tier-eligible AMI
List your VPCs
1 |
aws ec2 describe-vpcs |
Create an environment variable for your VPC
1 |
export VPC=<default_vpc_id> |
Use a key pair from your existing key pairs
MyALBKeyPair.pem
Create Resources
Create your security group
1 |
aws ec2 create-security-group --group-name MyALBSecurityGroup --description "My ALB security group" |
Set environment variable for your security group
1 |
export SGID=<security_group_id> |
Get your local IP Address
1 |
curl https://checkip.amazonaws.com |
Set an environment variable for your IP address
1 |
export IPADD=<your_ip_address> |
Add ssh and HTTP rules to your inbound rules
1 |
aws ec2 authorize-security-group-ingress --group-id $SGID --protocol tcp --port 22 --cidr $IPADD/32<br>aws ec2 authorize-security-group-ingress --group-id $SGID --protocol tcp --port 80 --cidr 0.0.0.0/0 |
Create your subnets
1 |
aws ec2 create-subnet --vpc-id $VPC --availability-zone-id use1-az1 --cidr-block 172.31.128.0/20<br>aws ec2 create-subnet --vpc-id $VPC --availability-zone-id use1-az2 --cidr-block 172.31.192.0/20 |
Create environment variables for your subnets
1 2 |
export AZ1SUB=<az1_subnet_id> export AZ2SUB=s<az2_subnet_id> |
Create your EC2 instances using the AMI id of ami-03c7d01cf4dedc891 (the free tier eligible AMI), two in each subnet; one for the video server and the other for the web server.
userdata-video-server-1.txt
1 2 3 4 5 6 7 8 |
#!/bin/bash sudo su yum install httpd -y cd /var/www/html mkdir vid echo "<html><body><h1><span style="color:#6D8764">Hello from EC2 video server instance #1</span></h1><p>This server acts as an Apache httpd server for video content</p></body></html>" > /var/www/html/vid/index.html echo "<html><body><h1><span style="color:#6D8764">Serving video data from EC2 application instance #1</span></h1><p>This server acts as an Apache httpd server for video content</p></body></html>" > /var/www/html/vid/video.html service httpd start |
1 |
aws ec2 run-instances --image-id ami-03c7d01cf4dedc891 \<br>--instance-type t2.micro --count 1 --subnet-id $AZ1SUB \<br>--key-name MyALBKeyPair20230426 --security-group-ids $SGID \<br>--associate-public-ip-address --user-data file://userdata-video-server-1.txt |
userdata-video-server-2.txt
1 2 3 4 5 6 7 8 |
#!/bin/bash sudo su yum install httpd -y cd /var/www/html mkdir vid echo "<html><body><h1><span style="color:#E51400">Hello from EC2 video server instance #2</span></h1><p>This server acts as an Apache httpd server for video content</p></body></html>" > /var/www/html/vid/index.html echo "<html><body><h1><span style="color:#E51400">Serving application data from EC2 application instance #2</span></h1><p>This server acts as an Apache httpd server for video content</p></body></html>" > /var/www/html/vid/video.html service httpd start |
1 |
aws ec2 run-instances --image-id ami-03c7d01cf4dedc891 \<br>--instance-type t2.micro --count 1 --subnet-id $AZ2SUB \<br>--key-name MyALBKeyPair20230426 --security-group-ids $SGID \<br>--associate-public-ip-address --user-data file://userdata-video-server-2.txt |
userdata-web-server-1.txt
1 2 3 4 5 6 |
#!/bin/bash sudo su yum install httpd -y echo "<html><body><h1><span style="color:#FFB570">Hello from EC2 web instance #1</span></h1><p>This server acts as an Apache nginx server</p></body></html>" > /var/www/html/index.html echo "<html><body><h1><span style="color:#FFB570">Serving web interactions from EC2 web instance #1</span></h1><p>This server acts as an Apache nginx server</p></body></html>" > /var/www/html/application.html service httpd start |
1 |
aws ec2 run-instances --image-id ami-03c7d01cf4dedc891 \<br>--instance-type t2.micro --count 1 --subnet-id $AZ1SUB \<br>--key-name MyALBKeyPair20230426 --security-group-ids $SGID \<br>--associate-public-ip-address --user-data file://userdata-web-server-1.txt |
userdata-web-server-2.txt
!/bin/bash
sudo su
yum install httpd -y
echo “
Hello from EC2 web instance #2
This server acts as an Apache nginx server” > /var/www/html/index.html
echo “
Serving web interactions from EC2 web instance #2
1 2 3 4 5 6 |
#!/bin/bash sudo su yum install httpd -y echo "<html><body><h1><span style="color:#3333FF">Hello from EC2 web instance #2</span></h1><p>This server acts as an Apache nginx server</p></body></html>" > /var/www/html/index.html echo "<html><body><h1><span style="color:#3333FF">Serving web interactions from EC2 web instance #2</span></h1><p>This server acts as an Apache nginx server</p></body></html>" > /var/www/html/application.html service httpd start |
1 |
aws ec2 run-instances --image-id ami-03c7d01cf4dedc891 \<br>--instance-type t2.micro --count 1 --subnet-id $AZ2SUB \<br>--key-name MyALBKeyPair20230426 --security-group-ids $SGID \<br>--associate-public-ip-address --user-data file://userdata-web-server-2.txt |
Create environment variables for your EC2 instances
1 2 3 4 |
export VIDSERV1=<video_server_instance_id_1> export VIDSERV2=<video_server_instance_id_2> export WEBSERV1=<web_server_instance_id_1> export WEBSERV2=<web_server_instance_id_2> |
Tag your instances with names
1 |
aws ec2 create-tags --resources $VIDSERV1 --tags Key="Name",Value="Video Server #1"<br>aws ec2 create-tags --resources $VIDSERV2 --tags Key="Name",Value="Video Server #2"<br>aws ec2 create-tags --resources $WEBSERV1 --tags Key="Name",Value="Web Server #1"<br>aws ec2 create-tags --resources $WEBSERV2 --tags Key="Name",Value="Web Server #2" |
Create your Video Load Balancer
1 |
aws elbv2 create-load-balancer --name MyALB --subnets $AZ1SUB $AZ2SUB --security-groups $SGID |
Create environment variables for your ALB ARN and DNS name
1 |
export ALBARN=arn:aws:elasticloadbalancing:us-east-1:############:loadbalancer/app/MyALB/a6f121e8d8e04021<br>export ALBDNS=MyALB-949638453.us-east-1.elb.amazonaws.com |
Create your Target Groups
1 |
aws elbv2 create-target-group --name VideoTargets --protocol HTTP --port 80 --vpc-id $VPC<br>aws elbv2 create-target-group --name WebTargets --protocol HTTP --port 80 --vpc-id $VPC |
Create environment variables for your target group ARNs
1 |
export VIDTGARN=arn:aws:elasticloadbalancing:us-east-1:############:targetgroup/VideoTargets/ecf67383b371d68e<br>export WEBTGARN=arn:aws:elasticloadbalancing:us-east-1:############:targetgroup/WebTargets/c62e5325012e7d51 |
Register your EC2 instances with your Target Groups
1 |
aws elbv2 register-targets --target-group-arn $VIDTGARN --targets Id=$VIDSERV1 Id=$VIDSERV2<br>aws elbv2 register-targets --target-group-arn $WEBTGARN --targets Id=$WEBSERV1 Id=$WEBSERV2 |
Create a listener for your ALB and give it a default Target Group of the web target group
1 |
aws elbv2 create-listener --load-balancer-arn $ALBARN --protocol HTTP \<br>--port 80 --default-actions Type=forward,TargetGroupArn=$WEBTGARN |
Create an environment variable for your listener ARN
1 |
export LISTARN=arn:aws:elasticloadbalancing:us-east-1:############:listener/app/MyALB/a6f121e8d8e04021/b233b9957d512d9a |
Verify the health of your targets in each Target Group
1 |
aws elbv2 describe-target-health --target-group-arn $VIDTGARN<br>aws elbv2 describe-target-health --target-group-arn $WEBTGARN |
Add path-based routing
conditions-pattern.json
1 2 3 4 5 6 7 8 9 |
[ { "Field": "path-pattern", "PathPatternConfig": { "Values": ["/vid/*"] } } ] |
1 |
aws elbv2 create-rule \<br>--listener-arn $LISTARN \<br>--priority 5 \<br>--conditions file://conditions-pattern.json \<br>--actions Type=forward,TargetGroupArn=$VIDTGARN |
Get your listener arns
1 |
aws elbv2 describe-rules --listener-arn $LISTARN |
Create environment variables for your rule ARNs
1 |
export VIDRULEARN=arn:aws:elasticloadbalancing:us-east-1:############:listener-rule/app/MyALB/a6f121e8d8e04021/b233b9957d512d9a/5ac15ce0c4755a35<br>export WEBRULEARN=arn:aws:elasticloadbalancing:us-east-1:############:listener-rule/app/MyALB/a6f121e8d8e04021/b233b9957d512d9a/d40b830a3ddef854 |
Test your setup
1 |
http://myalb-1394077796.us-east-1.elb.amazonaws.com/ |
http://myalb-1394077796.us-east-1.elb.amazonaws.com/vid/
Delete Resources
Delete your listener rules
1 |
aws elbv2 delete-rule --rule-arn $VIDRULEARN<br>aws elbv2 delete-rule --rule-arn $WEBRULEARN |
Delete your listener
1 |
aws elbv2 delete-listener --listener-arn $LISTARN |
Delete your Target Groups
1 |
aws elbv2 delete-target-group --target-group-arn $VIDTGARN<br>aws elbv2 delete-target-group --target-group-arn $WEBTGARN |
Delete your ALB
1 |
aws elbv2 delete-load-balancer --load-balancer-arn $ALBARN |
Terminate your EC2 instances
1 |
aws ec2 terminate-instances --instance-ids $VIDSERV1 $VIDSERV2 $WEBSERV1 $WEBSERV2 |
Delete your subnets
1 |
aws ec2 delete-subnet --subnet-id $AZ1SUB<br>aws ec2 delete-subnet --subnet-id $AZ2SUB |
Delete your security group
1 |
aws ec2 delete-security-group --group-id $SGID |
References
https://serverfault.com/questions/509136/how-do-i-set-a-vpc-in-aws-to-default-vpc-true