{% set config = merged_configs.get('aws_sns_topic', {}) %}
{% set comments = compliance_comments.get('aws_sns_topic', []) %}
# ------------------------------------------------------------------------------
# SNS Topic
# ------------------------------------------------------------------------------
{% for comment in comments %}
# {{ comment }}
{% endfor %}

resource "aws_sns_topic" "this" {
  name = "{{ config.get('name', project_id ~ '-topic') }}"

  # Encryption
  kms_master_key_id = "{{ config.get('kms_master_key_id', 'alias/aws/sns') }}"

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

resource "aws_sns_topic_policy" "this" {
  arn = aws_sns_topic.this.arn

  policy = jsonencode({
    Version = "2012-10-17"
    Id      = "{{ project_id }}-sns-policy"
    Statement = [
      {
        Sid    = "EnforceHTTPS"
        Effect = "Deny"
        Principal = "*"
        Action    = "sns:Publish"
        Resource  = aws_sns_topic.this.arn
        Condition = {
          Bool = {
            "aws:SecureTransport" = "false"
          }
        }
      }
    ]
  })
}
