'failed creating CloudWatch Metric Alarm: ValidationError: One or more metrics in your request are Forbidden

I need to create a cloudwatch alarm in Account 1 and attach it to Route53 Health check. This Cloudwatch alarm is based on a metric in Account 2. The Cloudwatch Cross account is enabled it Account 1 and the role has the trust policy to assume Account 2:

resource "aws_cloudwatch_metric_alarm" "internal_lb_response_time" {
  alarm_name          = "int-alb-response-time"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "2"
  threshold           = "10"
  alarm_description   = "This metric monitors the target response time"
  treat_missing_data = "notBreaching"
  
   metric_query {
    id = "target_response_time_internal_lb"
    return_data = true
    account_id = "34999999999"
    
    metric {
      namespace           = "AWS/ApplicationELB"
      metric_name         = "TargetResponseTime"
      period              = "60"
      stat                = "p95"
      dimensions = {
        LoadBalancer = var.internal_alb
      } 
    }
   }

  tags = merge(var.tags,{ "Name": "int-alb-response-time"})
}

When I apply this in terraform, I get this error:

Error: failed creating CloudWatch Metric Alarm (int-alb-response-time): ValidationError: One or more metrics in your request are Forbidden.
│       status code: 400, request id: 82184b29-3bc0-4a61-a95d-309855375041
│ 

When I comment the line :

account_id = "34999999999" 

The alarm gets created in account 1 but no data points are showing. How to create a cross account cloudwatch alarm ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source