In this post we will create an AWS application load balancer using CloudFormation.
The EC2 servers
In the previous post, we have presented a creation of VPC and EC2 based web server. To create an application load balancer we need to use at least two different availability zones. I have modified the previous template to create two EC2 machines in two availability zones.
Parameters:
LatestAmiId:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
Resources:
VPC:
Type: AWS::EC2::VPC
Properties:
CidrBlock: 10.0.0.0/16
Tags:
- Key: Name
Value: po-vpc
Subnet1:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.1.0/24
AvailabilityZone: us-east-1a
Tags:
- Key: Name
Value: po-subnet1
Subnet2:
Type: AWS::EC2::Subnet
Properties:
VpcId: !Ref VPC
CidrBlock: 10.0.2.0/24
AvailabilityZone: us-east-1b
Tags:
- Key: Name
Value: po-subnet2
InternetGateway:
Type: AWS::EC2::InternetGateway
DependsOn: VPC
Properties:
Tags:
- Key: Name
Value: po-route-table
AttachGateway:
Type: AWS::EC2::VPCGatewayAttachment
Properties:
VpcId: !Ref VPC
InternetGatewayId: !Ref InternetGateway
RouteTable1:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: po-route-table1
RouteTable2:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: po-route-table2
RouteTableAssociate1:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: Subnet1
RouteTableId:
Ref: RouteTable1
RouteTableAssociate2:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId:
Ref: Subnet2
RouteTableId:
Ref: RouteTable2
PublicRoute1:
Type: AWS::EC2::Route
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref RouteTable1
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
PublicRoute2:
Type: AWS::EC2::Route
DependsOn: AttachGateway
Properties:
RouteTableId: !Ref RouteTable2
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref InternetGateway
SecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
VpcId: !Ref VPC
GroupDescription: the security group
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0
Tags:
- Key: Name
Value: po-security-group
Server1:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
SubnetId: !Ref Subnet1
KeyName: PO
SecurityGroupIds:
- !Ref SecurityGroup
Tags:
- Key: Name
Value: po-server1
Server2:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: !Ref LatestAmiId
SubnetId: !Ref Subnet2
KeyName: PO
SecurityGroupIds:
- !Ref SecurityGroup
Tags:
- Key: Name
Value: po-server2
ElasticIP1:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
InstanceId: !Ref Server1
Tags:
- Key: Name
Value: po-elastic-ip1
ElasticIP2:
Type: AWS::EC2::EIP
Properties:
Domain: vpc
InstanceId: !Ref Server2
Tags:
- Key: Name
Value: po-elastic-ip2
The Application Load Balancer
Now we have 2 EC2 servers running in two different availability zones, and we can configure the application load balancer.
ApplicationLoadBalancer:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
DependsOn: AttachGateway
Properties:
IpAddressType: ipv4
Scheme: internet-facing
SecurityGroups:
- !Ref SecurityGroup
Subnets:
- !Ref Subnet1
- !Ref Subnet2
Type: application
Tags:
- Key: Name
Value: po-alb
The application load balancer is connected to the two subnets, each one in a different availability zone. Next we configure the targets for the application load balancer to be the two EC2 servers.
TargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
Port: 80
Protocol: HTTP
TargetType: instance
VpcId: !Ref VPC
Targets:
- Id: !Ref Server1
- Id: !Ref Server2
Tags:
- Key: Name
Value: po-alb-tg
The last step is to configure a listener that the application load balancer will serve request on its port.
ApplicationLoadBalancerListener:
Type: AWS::ElasticLoadBalancingV2::Listener
Properties:
LoadBalancerArn: !Ref ApplicationLoadBalancer
Port: 80
Protocol: HTTP
DefaultActions:
- TargetGroupArn: !Ref TargetGroup
Type: forward
Final Note
We have presented how to configure an application load balancer on AWS using the AWS CloudFormation. In the next post, we will configure a CloudFront CDN for it.
No comments:
Post a Comment