Sets up log publishing from an OpenSearch domain to CloudWatch Logs with appropriate IAM permissions.

Terraform Template

data "aws_iam_policy_document" "example" {

  statement {
    actions = ["logs:PutLogEvents", "logs:PutLogEventsBatch", "logs:CreateLogStream"]
    effect = "Allow"

    principals {
      identifiers = ["es.amazonaws.com"]
      type = "Service"
    }
    resources = ["arn:aws:logs:*"]
  }
}

resource "aws_cloudwatch_log_group" "example" {
  name = "example"
}

resource "aws_cloudwatch_log_resource_policy" "example" {
  policy_document = data.aws_iam_policy_document.example.json
  policy_name = "example"
}

resource "aws_opensearch_domain" "example" {

  log_publishing_options {
    cloudwatch_log_group_arn = aws_cloudwatch_log_group.example.arn
    log_type = "INDEX_SLOW_LOGS"
  }
}