Task: Configure a query log that can create a log stream and put log events using Route 53 resources. Name the zone "primary", the cloudwatch log group "aws_route53_example_com", and the cloudwatch log resource policy "route53-query-logging-policy"

Specification:
Has one "aws_route53_zone" resource
    with "name"

Has one "aws_cloudwatch_log_group" resource

Has one "aws_cloudwatch_log_resource_policy" resource
    with "policy_document" enabling "logs:CreateLogStream" and "logs:PutLogEvents"
    with "policy_name"

Has one "aws_route53_query_log" resource
    with "cloudwatch_log_group_arn" referencing the "aws_cloudwatch_log_group" resource
    with "zone_id" referencing the "aws_route53_zone" resource
    and depends on the "aws_cloudwatch_log_resource_policy" resource

Terraform configuration (main.tf):
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 5.75"
    }
  }

  required_version = ">= 1.9.0"
}

provider "aws" {
  region  = "us-east-1"
}


resource "aws_route53_zone" "primary" {
  name = "example53.com"
}

resource "aws_cloudwatch_log_group" "aws_route53_example_com" {
  name              = "/aws/route53/${aws_route53_zone.primary.name}"
  retention_in_days = 30
}

# Example CloudWatch log resource policy to allow Route53 to write logs
# to any log group under /aws/route53/*

data "aws_iam_policy_document" "route53-query-logging-policy" {
  statement {
    actions = [
      "logs:CreateLogStream",
      "logs:PutLogEvents",
    ]

    resources = ["arn:aws:logs:*:*:log-group:/aws/route53/*"]

    principals {
      identifiers = ["route53.amazonaws.com"]
      type        = "Service"
    }
  }
}

resource "aws_cloudwatch_log_resource_policy" "route53-query-logging-policy" {
  policy_document = data.aws_iam_policy_document.route53-query-logging-policy.json
  policy_name     = "route53-query-logging-policy"
}

resource "aws_route53_query_log" "example_com" {
  depends_on = [aws_cloudwatch_log_resource_policy.route53-query-logging-policy]

  cloudwatch_log_group_arn = aws_cloudwatch_log_group.aws_route53_example_com.arn
  zone_id                  = aws_route53_zone.primary.zone_id
}

Validation policy (checks.rego):
package terraform.validation

default is_configuration_valid = false

default is_valid_r53_zone = false

default is_valid_cloudwatch_log_group = false

default is_valid_cloudwatch_log_resource_policy = false

default is_valid_route53_query_log = false

# Validate aws_route53_zone resource
is_valid_r53_zone {
    some i
    resource := input.configuration.root_module.resources[i]
    resource.type == "aws_route53_zone"
    resource.expressions.name
}

is_valid_cloudwatch_log_group {
    some i
    resource := input.configuration.root_module.resources[i]
    resource.type == "aws_cloudwatch_log_group"
}

is_valid_cloudwatch_log_resource_policy {
    some i
    resource := input.resource_changes[i]
    resource.type == "aws_cloudwatch_log_resource_policy"
    contains(resource.change.after.policy_document, "logs:PutLogEvents")
    contains(resource.change.after.policy_document, "logs:CreateLogStream")
    resource.change.after.policy_name
}

is_valid_route53_query_log {
    some i
    resource := input.configuration.root_module.resources[i]
    resource.type == "aws_route53_query_log"
    resource.expressions.zone_id.references[0] == "aws_route53_zone.primary.zone_id"
    resource.expressions.cloudwatch_log_group_arn.references[0] == "aws_cloudwatch_log_group.aws_route53_example_com.arn"
    resource.depends_on[0] == "aws_cloudwatch_log_resource_policy.route53-query-logging-policy"
}

# Combine all checks into a final rule
is_configuration_valid {
    is_valid_r53_zone
    is_valid_cloudwatch_log_group
    is_valid_cloudwatch_log_resource_policy
    is_valid_route53_query_log
}