In the previous article of this terraform CDK series, we have created an ECS task-definition and service to run Nginx server on ECS.
In this article, we are going to attach an ALB to our ECS service.
An ALB (Application Load Balancer) helps in distributing incoming application traffic across multiple targets – in this case multiple ECS tasks. And this increases the availability of your application.
While creating an Application Load Balancer, we need to focus on two major components:
- Listeners: Before you start using your Application Load Balancer, you must add one or more listeners. A listener is a process that checks for connection requests, using the protocol and port that you configure. The rules that you define for a listener determine how the load balancer routes requests to its registered targets.
- Target Groups: Each target group is used to route requests to one or more registered targets. When you create each listener rule, you specify a target group and conditions. When a rule condition is met, traffic is forwarded to the corresponding target group. You can create different target groups for different types of requests.
Before starting with the creation of ALB components, First create a security group that is specific to the load balancer (like a firewall to the load balancer).
Import the security group provider:
Create ALB security group:
We want to specify the inbound and outbound traffic for our ALB. Hence, need to attach a security group to the ALB. Here, we are creating an ALB specific security group to allow traffic from the internet. i. e. from 0.0.0.0/0
cidr block.
Create ECS Security group:
One important change we need to make is – we need to create a security group for ECS and attach it to the EC2 instance we have created earlier. The security group will be specific for the ECS and allow traffic from the ALB only. It's like only ALB is allowed to access our ECS instances. (refer this to create and attach EC2 instance to the ECS).
As we are using dynamic-port mapping for our ECS containers, the fromPort and toPort attributes from the ingress should provide the range of ports allowed for the host. Our container port is 80 but the host port can be anything between the range 32768 to 65535.
Create target group:
We'll be creating a target group for our application load balancer.
Import target group provider:
targetType
: Type of target that you must specify when registering targets with this target group. See doc for supported values. The default isinstance
.vpcId
: (Optional, Forces new resource) Identifier for the VPC in which to create the target group. Required whentarget_type
isinstance
,ip
oralb
. Does not apply whentarget_type
islambda
.name
: (Optional, Forces new resource) Name of the target group. If omitted, Terraform will assign a random, unique name.protocol
: Protocol to use to connect with the target. Defaults toHTTP
. Not applicable whentarget_type
islambda
.port
: (May be required, Forces new resource) Port on which targets receive traffic, unless overridden when registering a specific target. Required whentarget_type
isinstance
,ip
oralb
. Does not apply whentarget_type
islambda
.protocolVersion
: (Optional, Forces new resource) Only applicable whenprotocol
isHTTP
orHTTPS
. The protocol version. Specify GRPC to send requests to targets using gRPC. Specify HTTP2 to send requests to targets using HTTP/2. The default is HTTP1, which sends requests to targets using HTTP/1.1healthCheck
: to check target health. Refer this for more health check parameters.
Create application load balancer:
Use Alb resource to create the load balancer.
loadBalancerType
: (Optional) The type of load balancer to create. Possible values areapplication
,gateway
, ornetwork
. The default value isapplication
.name
: Name of the load balancer.internal
: (Optional) If true, the LB will be internal.ipAddressType
: (Optional) The type of IP addresses used by the subnets for your load balancer. The possible values areipv4
anddualstack
subnets
: (Optional) A list of subnet IDs to attach to the LB. Subnets cannot be updated for Load Balancers of typenetwork
. Changing this value for load balancers of typenetwork
will force a recreation of the resource.securityGroups
: (Optional) A list of security group IDs to assign to the LB. Only valid for Load Balancers of typeapplication
.
ipv4
as the ip_address_type. You can only change to dualstack
ip_address_type if the selected subnets are IPv6 enabled.subnets
or subnet_mapping
is required.Create ALB listener and attach it to the ALB:
We are done with creating target group and the ALB. Let's now create and attach a listener to the ALB. We are attaching a HTTP listener with the target group created.
That's it. Deploy the changes using cdktf deploy
and Nginx's welcome page will be accessible on ALB dns name. You can find the ALB DNS name in AWS load balancers section.