{% set config = merged_configs.get('aws_cloudwatch_log_group', {}) %}
{% set comments = compliance_comments.get('aws_cloudwatch_log_group', []) %}
# ------------------------------------------------------------------------------
# CloudWatch Log Group
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "aws_cloudwatch_log_group" "this" {
  name              = "{{ config.get('name', '/' ~ project_id ~ '/application') }}"
  retention_in_days = {{ config.get('retention_in_days', 90) }}
  kms_key_id        = {{ config.get('kms_key_id', 'null') }}
  log_group_class   = "{{ config.get('log_group_class', 'STANDARD') }}"

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

{% if config.get('metric_filters') %}
{% for filter in config.get('metric_filters', []) %}
resource "aws_cloudwatch_log_metric_filter" "{{ filter.get('name', 'filter_' ~ loop.index) }}" {
  name           = "{{ filter.get('name', 'filter-' ~ loop.index) }}"
  pattern        = "{{ filter.get('pattern', '') }}"
  log_group_name = aws_cloudwatch_log_group.this.name

  metric_transformation {
    name      = "{{ filter.get('metric_name', 'metric-' ~ loop.index) }}"
    namespace = "{{ filter.get('metric_namespace', project_id ~ '/CustomMetrics') }}"
    value     = "{{ filter.get('metric_value', '1') }}"
  }
}
{% endfor %}
{% endif %}
