{% extends "aws/base_resource.tf.j2" %}
{% block resources %}
{% for clb in resources %}
{{ resource_spacer() }}
resource "aws_elb" "{{ clb.LoadBalancerName | tf_resource_name }}" {
  name               = "{{ clb.LoadBalancerName }}"
  internal           = {{ clb.Scheme == 'internal' | lower }}

  subnets = [
    {% for subnet in clb.Subnets %}
    "{{ subnet }}"{{ "," if not loop.last }}
    {% endfor %}
  ]

  {% if clb.SecurityGroups %}
  security_groups = [
    {% for sg in clb.SecurityGroups %}
    "{{ sg }}"{{ "," if not loop.last }}
    {% endfor %}
  ]
  {%  endif %}

  {% for listener in clb.ListenerDescriptions %}
  listener {
    instance_port     = {{ listener.Listener.InstancePort }}
    instance_protocol = "{{ listener.Listener.InstanceProtocol }}"
    lb_port           = {{ listener.Listener.LoadBalancerPort }}
    lb_protocol       = "{{ listener.Listener.LoadBalancerProtocol }}"
    {% if listener.Listener.SSLCertificateId %}
    ssl_certificate_id = "{{ listener.Listener.SSLCertificateId }}"
    {%  endif %}
  }
  {% endfor %}

  health_check {
    target              = "{{ clb.HealthCheck.Target }}"
    interval            = {{ clb.HealthCheck.Interval }}
    timeout             = {{ clb.HealthCheck.Timeout }}
    healthy_threshold   = {{ clb.HealthCheck.HealthyThreshold }}
    unhealthy_threshold = {{ clb.HealthCheck.UnhealthyThreshold }}
  }

  {{ render_common_tags(clb, indent=2) }}
}

{% endfor %}
{% endblock %}
