# Terraform configuration to create a CloudWatch Metric Alarm for a custom metric.
# Publishing custom metric data is a runtime operation performed by applications
# or AWS services, not directly by Terraform.

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

# --- CloudWatch Metric Alarm ---
resource "aws_cloudwatch_metric_alarm" "custom_metric_alarm" {
  alarm_name          = "MyTerraformCustomMetricAlarm"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = 1
  metric_name         = "CustomTransactionCount"
  namespace           = "MyApplication"
  period              = 60 # seconds
  statistic           = "Sum"
  threshold           = 5
  alarm_description   = "This alarm triggers when CustomTransactionCount exceeds 5 in a 1-minute period."

  dimensions = {
    Service = "Auth"
    Region  = "us-east-1"
  }

  # Optional: Configure actions (e.g., send notification to an SNS topic)
  # alarm_actions = [aws_sns_topic.alarm_notifications.arn]
  # ok_actions    = [aws_sns_topic.alarm_notifications.arn]

  tags = {
    Name        = "MyTerraformCustomMetricAlarm"
    Environment = "Dev"
  }
}

# --- Outputs ---
output "cloudwatch_alarm_name" {
  value       = aws_cloudwatch_metric_alarm.custom_metric_alarm.alarm_name
  description = "The name of the CloudWatch alarm."
}

output "cloudwatch_alarm_arn" {
  value       = aws_cloudwatch_metric_alarm.custom_metric_alarm.arn
  description = "The ARN of the CloudWatch alarm."
}
