/terraform-aws-alb

This terraform module is used to create ALB on AWS.

Primary LanguageHCLMIT LicenseMIT

Terraform AWS ALB

This terraform module is used to create ALB on AWS.

Terraform Licence


We eat, drink, sleep and most importantly love DevOps. We are working towards strategies for standardizing architecture while ensuring security for the infrastructure. We are strong believer of the philosophy Bigger problems are always solved by breaking them into smaller manageable problems. Resonating with microservices architecture, it is considered best-practice to run database, cluster, storage in smaller connected yet manageable pieces within the infrastructure.

This module is basically combination of Terraform open source and includes automatation tests and examples. It also helps to create and improve your infrastructure with minimalistic code instead of maintaining the whole infrastructure code yourself.

We have fifty plus terraform modules. A few of them are comepleted and are available for open source usage while a few others are in progress.

Prerequisites

This module has a few dependencies:

Examples

IMPORTANT: Since the master branch used in source varies based on new modifications, we suggest that you use the release versions here.

Here are examples of how you can use this module in your inventory structure:

ALB Example

  module "alb" {
    source                     = "clouddrove/alb/aws"
    version                    = "0.14.0"
    name                       = "alb"
    internal                   = false
    load_balancer_type         = "application"
    instance_count             = module.ec2.instance_count
    security_groups            = [module.ssh.security_group_ids, module.http-https.security_group_ids]
    subnets                    = module.public_subnets.public_subnet_id
    enable_deletion_protection = false
    target_id                  = module.ec2.instance_id
    vpc_id                     = module.vpc.vpc_id
    https_enabled              = true
    http_enabled               = true
    https_port                 = 443
    listener_type              = "forward"
    listener_certificate_arn   = "arn:aws:acm:eu-west-1:924144197303:certificate/0418d2ba-91f7-4196-991b-28b5c60cd4cf"
    target_group_port          = 80
    target_groups  = [
      {
        backend_protocol     = "HTTP"
        backend_port         = 80
        target_type          = "instance"
        deregistration_delay = 300
        health_check = {
          enabled             = true
          interval            = 30
          path                = "/"
          port                = "traffic-port"
          healthy_threshold   = 3
          unhealthy_threshold = 3
          timeout             = 10
          protocol            = "HTTP"
          matcher             = "200-399"
        }
      }
    ]
  }

NLB Example

  module "alb" {
    source                     = "clouddrove/alb/aws"
    version                    = "0.14.0"
    name                       = "nlb"
    internal                   = false
    load_balancer_type         = "application"
    instance_count             = module.ec2.instance_count
    subnets                    = module.public_subnets.public_subnet_id
    enable_deletion_protection = false
    target_id                  = module.ec2.instance_id
    vpc_id                     = module.vpc.vpc_id
    http_tcp_listeners = [
      {
        port               = 80
        protocol           = "TCP"
        target_group_index = 0
      },
    ]

    https_listeners = [
      {
        port               = 443
        protocol           = "TLS"
        certificate_arn    = "arn:aws:acm:eu-west-1:924144197303:certificate/0418d2ba-91f7-4196-991b-28b5c60cd4cf"
        target_group_index = 1
      },
    ]

    target_groups = [
      {
        backend_protocol = "TCP"
        backend_port     = 80
        target_type      = "instance"
      },
      {
        backend_protocol = "TLS"
        backend_port     = 443
        target_type      = "instance"
      },
    ]
  }

CLB Example

  module "clb" {
  source                     = "clouddrove/alb/aws"
  version                    = "0.14.0"
  name                       = "clb"

  load_balancer_type = "classic"
  internal        = false
  target_id       = module.ec2.instance_id
  security_groups = [module.ssh.security_group_ids, module.http_https.security_group_ids]
  subnets         = module.public_subnets.public_subnet_id

  listeners = [
  {
  lb_port            = 22000
  lb_protocol        = "TCP"
  instance_port      = 22000
  instance_protocol  = "TCP"
  ssl_certificate_id = null
  },
  {
  lb_port            = 4444
  lb_protocol        = "TCP"
  instance_port      = 4444
  instance_protocol  = "TCP"
  ssl_certificate_id = null
  }
  ]

  health_check_target              = "TCP:4444"
  health_check_timeout             = 10
  health_check_interval            = 30
  health_check_unhealthy_threshold = 5
  health_check_healthy_threshold   = 5
}

Inputs

Name Description Type Default Required
access_logs Access logs Enable or Disable. bool false no
allocation_id The allocation ID of the Elastic IP address. string "" no
attributes Additional attributes (e.g. 1). list(any) [] no
availability_zones The AZ's to serve traffic in. list(map(string)) [] no
clb_enable If true, create clb. bool false no
connection_draining TBoolean to enable connection draining. Default: false. bool false no
connection_draining_timeout The time after which connection draining is aborted in seconds. number 300 no
delimiter Delimiter to be used between organization, environment, name and attributes. string "-" no
enable If true, create alb. bool false no
enable_connection_draining Whether or not to enable connection draining ("true" or "false"). bool false no
enable_cross_zone_load_balancing Indicates whether cross zone load balancing should be enabled in application load balancers. bool true no
enable_deletion_protection If true, deletion of the load balancer will be disabled via the AWS API. This will prevent Terraform from deleting the load balancer. Defaults to false. bool false no
enable_http2 Indicates whether HTTP/2 is enabled in application load balancers. bool true no
environment Environment (e.g. prod, dev, staging). string "test" no
health_check_healthy_threshold The number of successful health checks before an instance is put into service. number 10 no
health_check_interval The time between health check attempts in seconds. number 30 no
health_check_target The target to use for health checks. string "TCP:80" no
health_check_timeout The time after which a health check is considered failed in seconds. number 5 no
health_check_unhealthy_threshold The number of failed health checks before an instance is taken out of service. number 2 no
http_enabled A boolean flag to enable/disable HTTP listener. bool true no
http_listener_type The type of routing action. Valid values are forward, redirect, fixed-response, authenticate-cognito and authenticate-oidc. string "redirect" no
http_port The port on which the load balancer is listening. like 80 or 443. number 80 no
http_tcp_listeners A list of maps describing the HTTP listeners for this ALB. Required key/values: port, protocol. Optional key/values: target_group_index (defaults to 0) list(map(string)) [] no
https_enabled A boolean flag to enable/disable HTTPS listener. bool true no
https_listeners A list of maps describing the HTTPS listeners for this ALB. Required key/values: port, certificate_arn. Optional key/values: ssl_policy (defaults to ELBSecurityPolicy-2016-08), target_group_index (defaults to 0) list(map(string)) [] no
https_port The port on which the load balancer is listening. like 80 or 443. number 443 no
idle_timeout The time in seconds that the connection is allowed to be idle. number 60 no
instance_count The count of instances. number 0 no
internal If true, the LB will be internal. string "" no
ip_address_type The type of IP addresses used by the subnets for your load balancer. The possible values are ipv4 and dualstack. string "ipv4" no
label_order Label order, e.g. name,application. list(any)
[
"name",
"environment"
]
no
listener_certificate_arn The ARN of the SSL server certificate. Exactly one certificate is required if the protocol is HTTPS. string "" no
listener_protocol The protocol for connections from clients to the load balancer. Valid values are TCP, HTTP and HTTPS. Defaults to HTTP. string "HTTPS" no
listener_type The type of routing action. Valid values are forward, redirect, fixed-response, authenticate-cognito and authenticate-oidc. string "forward" no
listeners A list of listener configurations for the ELB.
list(object({
lb_port : number
lb_protocol : string
instance_port : number
instance_protocol : string
ssl_certificate_id : string
}))
[] no
load_balancer_create_timeout Timeout value when creating the ALB. string "10m" no
load_balancer_delete_timeout Timeout value when deleting the ALB. string "10m" no
load_balancer_type The type of load balancer to create. Possible values are application or network. The default value is application. string "" no
load_balancer_update_timeout Timeout value when updating the ALB. string "10m" no
log_bucket_name S3 bucket (externally created) for storing load balancer access logs. Required if logging_enabled is true. string "" no
managedby ManagedBy, eg 'CloudDrove'. string "hello@clouddrove.com" no
name Name (e.g. app or cluster). string "" no
repository Terraform current module repo string "https://registry.terraform.io/modules/clouddrove/terraform-aws-alb/aws/0.14.0" no
security_groups A list of security group IDs to assign to the LB. Only valid for Load Balancers of type application. list(any) [] no
status_code The HTTP redirect code. The redirect is either permanent (HTTP_301) or temporary (HTTP_302). string "HTTP_301" no
subnet_id The id of the subnet of which to attach to the load balancer. You can specify only one subnet per Availability Zone. string "" no
subnet_mapping A list of subnet mapping blocks describing subnets to attach to network load balancer list(map(string)) [] no
subnets A list of subnet IDs to attach to the LB. Subnets cannot be updated for Load Balancers of type network. Changing this value will for load balancers of type network will force a recreation of the resource. list(any) [] no
tags Additional tags (e.g. map(BusinessUnit,XYZ). map(any) {} no
target_group_port The port on which targets receive traffic, unless overridden when registering a specific target. string 80 no
target_groups A list of maps containing key/value pairs that define the target groups to be created. Order of these maps is important and the index of these are to be referenced in listener definitions. Required key/values: name, backend_protocol, backend_port. Optional key/values are in the target_groups_defaults variable. any [] no
target_id The ID of the target. This is the Instance ID for an instance, or the container ID for an ECS container. If the target type is ip, specify an IP address. list(any) n/a yes
target_type The type of target that you must specify when registering targets with this target group. string "" no
vpc_id The identifier of the VPC in which to create the target group. string "" no

Outputs

Name Description
arn The ARN of the ALB.
arn_suffix The ARN suffix of the ALB.
clb_arn The ARN of the CLB.
clb_name DNS name of CLB.
clb_zone_id The ID of the zone which ALB is provisioned.
dns_name DNS name of ALB.
http_listener_arn The ARN of the HTTP listener.
https_listener_arn The ARN of the HTTPS listener.
listener_arns A list of all the listener ARNs.
main_target_group_arn The main target group ARN.
name The ARN suffix of the ALB.
tags A mapping of tags to assign to the resource.
zone_id The ID of the zone which ALB is provisioned.

Testing

In this module testing is performed with terratest and it creates a small piece of infrastructure, matches the output like ARN, ID and Tags name etc and destroy infrastructure in your AWS account. This testing is written in GO, so you need a GO environment in your system.

You need to run the following command in the testing folder:

  go test -run Test

Feedback

If you come accross a bug or have any feedback, please log it in our issue tracker, or feel free to drop us an email at hello@clouddrove.com.

If you have found it worth your time, go ahead and give us a ★ on our GitHub!

About us

At CloudDrove, we offer expert guidance, implementation support and services to help organisations accelerate their journey to the cloud. Our services include docker and container orchestration, cloud migration and adoption, infrastructure automation, application modernisation and remediation, and performance engineering.

We are The Cloud Experts!


We ❤️ Open Source and you can check out our other modules to get help with your new Cloud ideas.