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.73k stars 9.09k forks source link

AWS CloudWatch Metric Alarm (aws_cloudwatch_metric_alarm) - Anomaly detection #9293

Closed airmonitor closed 4 years ago

airmonitor commented 5 years ago

Community Note

Description

Hello. Recently CloudWatch Alarm get a new feature - Anomaly Detection. More information here - https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Anomaly_Detection.html

Will you be able to add this feature?

New or Affected Resource(s)

Potential Terraform Configuration

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file. For
# security, you can also encrypt the files using our GPG public key.

References

dskrvk commented 5 years ago

I was able to get the expected config by configuring the alarm manually and running plan:

resource "aws_cloudwatch_metric_alarm" "MyApiRequestCount" {
  alarm_name                = "MyApiRequestCount"
  comparison_operator       = "GreaterThanUpperThreshold"
  evaluation_periods        = "2"
  datapoints_to_alarm       = "2"
  threshold                 = "0"
  metric_query {
    id = "m1"

    metric {
      metric_name = "RequestCount"
      namespace   = "AWS/ApplicationELB"
      period      = "300"
      stat        = "Sum"

      dimensions = {
        LoadBalancer = "app/MyApi/..."
      }
    }
  }
  metric_query {
    id = "ad1"
    label = "RequestCount (expected)"
    return_data = true
    expression = "ANOMALY_DETECTION_BAND(m1, 2)"
  }
  treat_missing_data        = "missing"
  alarm_description         = "Too many incoming requests"
  alarm_actions             = []
  ok_actions                = []
}

I then ran apply and got my state files in sync with no actual changes to the infrastructure.

The only issue is that currently this CloudWatch feature doesn't seem to be working correctly - it doesn't change the alarm status when the conditions are met. I can see the metric going outside the band on the graph, but this never results in an ALARM status. Not an issue with terraform though - this happens with manually-configured alarms.

unacceptable commented 4 years ago

@dskrvk when running terraform apply you will run into an error because the following value in the ThresholdMetricId value not being set in the API:

Error: Creating metric alarm failed: ValidationError: Metric expression must evaluate to a single time series
        status code: 400, request id: 0f5e3a11-ac17-4fe6-86d5-f7b52cc898e3

  on Terraform/cloudwatch_buckets.tf line 1, in resource "aws_cloudwatch_metric_alarm" "bucket_name":
   1: resource "aws_cloudwatch_metric_alarm" "bucket_name" {

I guess this issue will be resolved by setting threshold_metric_id after https://github.com/terraform-providers/terraform-provider-aws/pull/9828 is merged.

@apparentlymart whenever you get a second could you poke someone about this PR on Monday?

unacceptable commented 4 years ago

I hate to be "that guy", but this is blocking a monitoring story in my sprint. I am just about to throw my monitor in CloudFormation and call the stack creation via Terraform, but I really don't want to add tech debt to my future workload.

What are we waiting on? As discussed in the PR this CloudWatch functionality is now "Generally Available". Who do we need to get involved in this PR?

shadycuz commented 4 years ago

I also need this and it appears we have some duplicate issues being created.

10551

isaccavalcante commented 4 years ago

I'm also "blocked" by this feature. I"m deploying a bunch of lambdas that use cloudwatch, and we have to manually update them after the deployment.

bflad commented 4 years ago

Please note that we are using this issue to track the aws_cloudwatch_metric_alarm enhancement for anomaly detection in CloudWatch. There is also now a separate feature request for an aws_cloudwatch_anomaly_detector resource at #10551. I will update this issue title accordingly.

bflad commented 4 years ago

Support for this functionality in the aws_cloudwatch_metric_alarm resource has been merged and will release with version 2.40.0 of the Terraform AWS Provider, next week. Thanks to @hakopako for the implementation. 👍

ghost commented 4 years ago

This has been released in version 2.40.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!