terraform-aws-modules/terraform-aws-alb

repository·master·Indexed 19 days ago

https://github.com/terraform-aws-modules/terraform-aws-alb

A Terraform module for provisioning and managing AWS Application Load Balancers (ALB) and Network Load Balancers (NLB). It includes support for listeners, target groups, security groups, Cognito authentication, and mutual authentication (mTLS) via the lb_trust_store module for managing CA certificate bundles and revocation lists.

Tokens
17.8K
Snippets
42
Records
60
Agent score
66%

What's inside terraform-aws-alb

  1. Use the module wrapper pattern for multiple resources

    master

    The wrappers module implements a single module wrapper pattern. This allows you to manage multiple instances of the ALB/NLB module within a single configuration block. This is particularly useful in environments like Terragrunt where native Terraform for_each might not be feasible, or when you want to manage multiple resources without duplicating configuration files.

    This wrapper does not add new functionality; it simply passes arguments through to the underlying root module. It uses two primary input maps:

    1. defaults: A map of arguments applied to every instance created in the items map.
    2. items: A map where each key represents a unique instance and the value is a map of arguments specific to that instance (which can override or supplement defaults).
  2. Use the lb_trust_store wrapper for multiple module instances

    master

    The lb_trust_store wrapper is a pattern used to manage multiple instances of the modules/lb_trust_store module within a single configuration block. This is particularly useful in environments like Terragrunt where native Terraform for_each might not be feasible for managing multiple resource copies without duplicating configuration files.

    This wrapper does not add new functionality; it simply provides a way to pass a map of items and a map of defaults to the underlying module.

  3. Migrate Terraform state for Listener Rules (v8.x to v9.x)

    master

    In v9.x, Listener Rules are migrated from indexed collections to map-based resources. You must use terraform state mv to move each rule from its v8.x index to its new v9.x key (which often follows a "listener_key/rule_key" pattern).

    # Example: Migrating HTTP rules
    terraform state mv 'module.alb.aws_lb_listener_rule.http_tcp_listener_rule[0]' 'module.alb.aws_lb_listener_rule.this["http-https-redirect/fixed-response"]'
    
    # Example: Migrating HTTPS rules
    terraform state mv 'module.alb.aws_lb_listener_rule.https_listener_rule[0]' 'module.alb.aws_lb_listener_rule.this["https/cognito"]'
  4. Migrate Listener and Listener Rule configuration from v8.x to v9.x

    master

    In v9.x, the way listeners and rules are defined has been consolidated and restructured for better organization.

    • Consolidation: Instead of separate http_tcp_listeners, https_listeners, and https_listener_rules lists, use a single listeners map.
    • Nesting Rules: Listener rules are now defined directly inside the listener object under a rules key, rather than in a separate top-level list.
    • Target Group Referencing: Instead of using target_group_index (an integer), use target_group_key (a string) to reference target groups defined in the target_groups map.
    • Condition Syntax: Condition keys have changed from plural to singular (e.g., path_patterns becomes path_pattern, query_strings becomes query_string, http_headers becomes http_header).
    # v9.x Listener and Rule pattern
    listeners = {
      my-listener = {
        port     = 443
        protocol = "HTTPS"
        
        # Default action for the listener
        forward = {
          target_group_key = "my-target-group"
        }
    
        # Rules nested within the listener
        rules = {
          my-rule = {
            priority = 10
            actions = [{
              type = "forward"
              target_group_key = "my-target-group"
            }]
            conditions = [{
              path_pattern = { values = ["/api/*"] }
            }]
          }
        }
      }
    }
  5. Migrate Terraform state for Additional SSL Certificates (v8.x to v9.x)

    master

    Additional SSL certificates must be migrated from indexed collections to map-based resources in v9.x using terraform state mv.

    # Example: Moving an additional certificate
    terraform state mv 'module.alb.aws_lb_listener_certificate.https_listener[0]' 'module.alb.aws_lb_listener_certificate.this["https/0"]'
  6. Migrate listener definitions from v8.x to v9.x

    master

    In v9.x, the method for creating listeners was simplified. Instead of multiple arguments, use a single listeners variable which accepts a map of listener definitions (using for_each).

    Key changes:

    • The target_group_index used in v8.x to associate listeners with target groups is replaced by target_group_key in v9.x.
    • The target_group_key must match the key of the target group definition in your target_groups map.
    # v9.x Listener Example
    # listeners = {
    #   listener_key = {
    #     port     = 443
    #     protocol = "HTTPS"
    #     target_group_key = "tg1" # Must match key in target_groups map
    #   }
    # }
  7. Configure the wrapper module with Terraform

    master

    To use the wrapper directly in Terraform, call the module and provide the defaults and items arguments. Each entry in items can contain any argument supported by the root ALB/NLB module.

    module "wrapper" {
      source = "terraform-aws-modules/alb/aws//wrappers"
    
      defaults = {
        create = true
        tags = {
          Terraform   = "true"
          Environment = "dev"
        }
      }
    
      items = {
        my-item = {
          # any argument supported by the root module
        }
        my-second-item = {
          # any argument supported by the root module
        }
      }
    }
  8. Configure the wrapper module with Terragrunt

    master

    To use the wrapper with Terragrunt, set the source in your terraform block and define defaults and items within the inputs block. Use defaults for shared configuration and items to define the individual resource instances.

    Note: You can use the tfr:/// source format or a direct Git source.

    terraform {
      source = "tfr:///terraform-aws-modules/alb/aws//wrappers"
      # Alternative source:
      # source = "git::git@github.com:terraform-aws-modules/terraform-aws-alb.git//wrappers?ref=master"
    }
    
    inputs = {
      defaults = {
        create = true
        tags = {
          Terraform   = "true"
          Environment = "dev"
        }
      }
    
      items = {
        my-item = {
          # any argument supported by the root module
        }
        my-second-item = {
          # any argument supported by the root module
        }
      }
    }
  9. Upgrade from v9.x to v10.x

    master

    When upgrading from version 9.x to 10.x, note the following breaking changes and requirements:

    Minimum Requirements

    • Terraform: v1.5.7 or higher.
    • AWS Provider: v6.5 or higher.

    Breaking Changes in Configuration

    • Listener Rule Actions: The rule.actions.type key has been removed. Actions are now defined by their type name directly. For example, instead of type = "fixed-response", use the fixed_response key.
    • Query String Conditions: The query_string parameter has changed from a map(string) to a list(map(string)). You must now provide a list of objects containing key and value keys.
    • Variable Types: Variable definitions have moved from any to detailed object types, which may affect how you pass complex objects.

    Other Changes

    • New Feature: A region parameter is now available to specify an AWS region different from the provider's default region.
    • Security Group Rules: Rules now use a default naming scheme of <security-group-name>-<map-key> unless a specific name is provided.
    • SSL Policy: The aws_lb_listener.ssl_policy now defaults to ELBSecurityPolicy-TLS13-1-3-2021-06.
     module "alb" {
       source  = "terraform-aws-modules/alb/aws"
    -  version = "9.17.0"
    +  version = "10.0.0"
    
       listeners = {
         ex-http-https-redirect = {
           port     = 80
           protocol = "HTTP"
           redirect = {
             port        = "443"
             protocol    = "HTTPS"
             status_code = "HTTP_301"
           }
    
           rules = {
             ex-fixed-response = {
               priority = 3
               actions = [{
                 # Same for all action types, not just `fixed_response`
    -            type           = "fixed-response"
    +            fixed_response = {
                   content_type = "text/plain"
                   status_code  = 200
                   message_body = "This is a fixed response"
    +            }
               }]
    
               conditions = [{
    -            query_string = {
    +            query_string = [{
                  key   = "weighted"
                  value = "true"
    -            }
    +            }]
               }]
             }
           }
         }
       }
     }
  10. Configure the lb_trust_store wrapper with Terragrunt

    master

    To use this wrapper with Terragrunt, define the source in the terraform block and provide defaults and items in the inputs block. The items map allows you to define multiple instances, where each key represents a unique identifier for an instance and the value is a map of arguments supported by the underlying module.

    terraform {
      source = "tfr:///terraform-aws-modules/alb/aws//wrappers/lb_trust_store"
      # Alternative source:
      # source = "git::git@github.com:terraform-aws-modules/terraform-aws-alb.git//wrappers/lb_trust_store?ref=master"
    }
    
    inputs = {
      defaults = {
        create = true
        tags = {
          Terraform   = "true"
          Environment = "dev"
        }
      }
    
      items = {
        my-item = {
          # can be any argument supported by the module
        }
        my-second-item = {
          # can be any argument supported by the module
        }
      }
    }