{% set config = merged_configs.get('aws_lb_listener', {}) %}
{% set comments = compliance_comments.get('aws_lb_listener', []) %}
# ------------------------------------------------------------------------------
# Load Balancer HTTPS Listener
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "aws_lb_listener" "https" {
  load_balancer_arn = aws_lb.this.arn
  port              = {{ config.get('port', 443) }}
  protocol          = "{{ config.get('protocol', 'HTTPS') }}"
  ssl_policy        = "{{ config.get('ssl_policy', 'ELBSecurityPolicy-TLS13-1-2-2021-06') }}"
  certificate_arn   = {{ config.get('certificate_arn', 'null') }}

  default_action {
    type = "{{ config.get('default_action_type', 'fixed-response') }}"

    {% if config.get('default_action_type', 'fixed-response') == 'fixed-response' %}
    fixed_response {
      content_type = "text/plain"
      message_body = "Not Found"
      status_code  = "404"
    }
    {% endif %}

    {% if config.get('default_action_type') == 'forward' %}
    target_group_arn = {{ config.get('default_target_group_arn', 'null') }}
    {% endif %}
  }

  tags = {
    Name      = "{{ project_id }}-alb-https"
    ManagedBy = "complyform"
  }
}
