hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.83k stars 9.17k forks source link

AWS Synthetics Canary Missing support for AWS Cloudwatch Alarms #19993

Open akshayvvr opened 3 years ago

akshayvvr commented 3 years ago

Community Note

Description

At present, it would appear the AWS Provider does not support the ability to set alarms for aws_synthetics_canary resources. This feature would make it much easier to create, associate alarms and notify stakeholders when there is an outage.

New or Affected Resource(s)

Potential Terraform Configuration

resource "aws_synthetics_canary" "some" {
  name                 = "some-canary"
  artifact_s3_location = "s3://some-bucket/"
  execution_role_arn   = "some-role"
  handler              = "exports.handler"
  zip_file             = "test-fixtures/lambdatest.zip"
  runtime_version      = "syn-1.0"

  schedule {
    expression = "rate(0 minute)"
  }
  alarm {
    metric_name="some_value"
    condition="some_value"
    threshold="some_value"
    period="some_value"

  }
}

References

ewbankkit commented 3 years ago

Creating alarms for Synthetics Canaries via the AWS Console is described here.

@akshayvvr Thanks for raising this issue. The CloudWatch Alarms associated with a Canary when created via the AWS Console are not specified in the CreateCanary API and so are not part of the aws_synthetics_canary resource. To associate alarms with the canary you will need to use the aws_cloudwatch_metric_alarm resource with one or more of the CloudWatch Metrics published by the canary.

akshayvvr commented 3 years ago

Creating alarms for Synthetics Canaries via the AWS Console is described here.

@akshayvvr Thanks for raising this issue. The CloudWatch Alarms associated with a Canary when created via the AWS Console are not specified in the CreateCanary API and so are not part of the aws_synthetics_canary resource. To associate alarms with the canary you will need to use the aws_cloudwatch_metric_alarm resource with one or more of the CloudWatch Metrics published by the canary.

Yes, That is correct. Right I use the Synthetics namespace that is auto published. But still, on the canary console, It doesn't show up as an alarm is associated with the Canary.

mpostument commented 2 years ago

You need to name alarm like Synthetics-Alarm-canary_name and in this case it will be displayed on canary console

illagrenan commented 1 year ago

I first created CloudWatch alarms via the console, then manually imported them into Terraform (to make sure the metrics, periods, etc. were set correctly). Here are the corresponding three CloudWatch alarms in Terraform:

resource "aws_cloudwatch_metric_alarm" "duration_alarm" {
  alarm_name                = "Synthetics-Alarm-${aws_synthetics_canary.this.name}-Duration"
  comparison_operator       = "GreaterThanThreshold"
  evaluation_periods        = 1
  metric_name               = "Duration"
  namespace                 = "CloudWatchSynthetics"
  period                    = 300
  statistic                 = "Average"
  threshold                 = 30000
  alarm_description         = "Synthetics alarm metric: Duration GreaterThanThreshold 30000"
  insufficient_data_actions = []
  treat_missing_data        = "breaching"

  dimensions = {
    CanaryName = aws_synthetics_canary.this.name
  }

  alarm_actions = [
    aws_sns_topic.duration_alarm.arn,
  ]

  ok_actions = [
    aws_sns_topic.duration_alarm.arn,
  ]

  tags = var.tags
}

resource "aws_cloudwatch_metric_alarm" "success_alarm" {
  alarm_name                = "Synthetics-Alarm-${aws_synthetics_canary.this.name}-SuccessPercent"
  alarm_description         = "Synthetics alarm metric: SuccessPercent LessThanThreshold 90"
  comparison_operator       = "LessThanThreshold"
  evaluation_periods        = 1
  metric_name               = "SuccessPercent"
  namespace                 = "CloudWatchSynthetics"
  period                    = 300
  statistic                 = "Average"
  threshold                 = 90
  insufficient_data_actions = []
  treat_missing_data        = "breaching"

  dimensions = {
    CanaryName = aws_synthetics_canary.this.name
  }

  alarm_actions = [
    aws_sns_topic.success_alarm.arn,
  ]

  ok_actions = [
    aws_sns_topic.success_alarm.arn,
  ]

  tags = var.tags
}

resource "aws_cloudwatch_metric_alarm" "failed_alarm" {
  alarm_name                = "Synthetics-Alarm-${aws_synthetics_canary.this.name}-Failed"
  alarm_description         = "Synthetics alarm metric: Failed GreaterThanOrEqualToThreshold 1"
  comparison_operator       = "GreaterThanOrEqualToThreshold"
  evaluation_periods        = 1
  metric_name               = "Failed"
  namespace                 = "CloudWatchSynthetics"
  period                    = 900
  statistic                 = "Sum"
  threshold                 = 1
  insufficient_data_actions = []
  treat_missing_data        = "notBreaching"

  dimensions = {
    CanaryName = aws_synthetics_canary.this.name
  }

  alarm_actions = [
    aws_sns_topic.failed_alarm.arn,
  ]

  ok_actions = [
    aws_sns_topic.failed_alarm.arn,
  ]

  tags = var.tags
}

image