{% set config = merged_configs.get('aws_ecs_service', {}) %}
{% set comments = compliance_comments.get('aws_ecs_service', []) %}
# ------------------------------------------------------------------------------
# ECS Service
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "aws_ecs_service" "this" {
  name            = "{{ config.get('name', project_id ~ '-service') }}"
  cluster         = aws_ecs_cluster.this.id
  task_definition = aws_ecs_task_definition.this.arn
  desired_count   = {{ config.get('desired_count', 2) }}
  launch_type     = "{{ config.get('launch_type', 'FARGATE') }}"

  enable_execute_command = {{ config.get('enable_execute_command', false) | lower }}

  network_configuration {
    subnets          = {{ config.get('subnets', '[]') }}
    security_groups  = {{ config.get('security_groups', '[]') }}
    assign_public_ip = {{ config.get('assign_public_ip', false) | lower }}
  }

  {% if config.get('load_balancer_target_group_arn') %}
  load_balancer {
    target_group_arn = "{{ config.load_balancer_target_group_arn }}"
    container_name   = "{{ config.get('container_name', 'app') }}"
    container_port   = {{ config.get('container_port', 8080) }}
  }
  {% endif %}

  deployment_circuit_breaker {
    enable   = {{ config.get('circuit_breaker_enabled', true) | lower }}
    rollback = {{ config.get('circuit_breaker_rollback', true) | lower }}
  }

  deployment_minimum_healthy_percent = {{ config.get('deployment_minimum_healthy_percent', 100) }}
  deployment_maximum_percent         = {{ config.get('deployment_maximum_percent', 200) }}

  tags = {
    Name      = "{{ config.get('name', project_id ~ '-service') }}"
    ManagedBy = "complyform"
  }
}
