3

Create a cloudwatch alarm:

resource "aws_cloudwatch_metric_alarm" "cloudfront-500-errors" {
  alarm_name          = "${var.ENVIRONMENT_NAME}-AWS-CloudFront-High-5xx-Error-Rate"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = 1
  metric_name         = "5xxErrorRate"
  namespace           = "AWS/Cloudfront"
  period              = 60
  statistic           = "Average"
  threshold           = 5
  treat_missing_data  = "notBreaching"
  alarm_actions       = [aws_sns_topic.my-sns-topic.arn]
  actions_enabled     = true

  dimensions = {
    DistributionId = aws_cloudfront_distribution.this.id
    Region         = "Global"
  }
}

I can create an alarm for cloudwatch in terraform, but how do I get the alarm here in "Cloudfront" ?

enter image description here

Josh Adams
  • 2,113
  • 2
  • 13
  • 25
  • I guess "terraform apply" completed successfully. and in this case, although this is a rather silly assumption, but still check the selected region in the browser – dmkvl Apr 25 '20 at 12:28
  • Correct region in browser. The alarm exists in cloudwatch, but I can't seem to figure out how to get it to show up in cloudfront alarms, unless i manually create it using the console gui. – Josh Adams Apr 25 '20 at 14:59

2 Answers2

2

The answer to this question is that, this UI is really just a link to the cloudwatch metrics. The real issue is my namespace was:

 namespace           = "AWS/Cloudfront"

and it needed to be :

 namespace           = "AWS/CloudFront"
Josh Adams
  • 2,113
  • 2
  • 13
  • 25
0

Also, Cloudfront cloudwatch metrics are only available in us-east-1.

Therefore, be sure to have an aws provider for that region set up and point the alarm to it.

resource "aws_cloudwatch_metric_alarm" "cloudfront-500-errors" {
  provider            = aws.useast1
  alarm_name          = "training_cloudfrotn_alarm"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = 1
  metric_name         = "4xxErrorRate"
  namespace           = "AWS/CloudFront"
  period              = 60
  statistic           = "Average"
........
}

and the provider:

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

how it looks in Clodufront's GUI