0
votes

I have 10 dynamo DB tables and I want to have 5 alarms per dynamo DB table, but only want to define those 5 alarms and not repeat them per table in terraform code. on AWS there will still be 50 alarms but I want them not to be repeated in terraform code.

My alarms across all tables will mostly look the same. i.e throughputexceeded exception, Latency Exception, and hence don't want to create separate alarms per table in terraform code. I want that anytime I create a table in terraform these basic alarms gets automatically created without someone needing to add them to terraform

How can I achieve this using terraform?

1
Why not have 50 alarms? You can obviously create a combined alarm that monitors the SUM or AVG or MAX of multiple tables, but why would you want that? - luk2302
xyproblem.info - what is your actual problem? - Ermiya Eskandary
@luk2302 My alarms across all tables will mostly look the same. i.e throughputexceeded exception, Latency Exception, and hence don't want to create separate alarms per table in terraform code. I want that anytime I create a table in terraform these basic alarms gets automatically created without someone needing to add them to terraform. - forgot1993
Create a terraform module for them and reuse them across the board - your question is more about AWS than terraform - Ermiya Eskandary
Yes, it will create those 5 alarms for that table - check out terraform.io/docs/language/modules/develop/index.html - try out some code and then open a new question if you really get stuck but terraform docs are easy to read and examples online are plentiful - hope that helps! - Ermiya Eskandary

1 Answers

0
votes

If you already have a list somewhere with their names, great, if no you can do something like:

monitoring.tf:

locals {

  dynamodb_names = [
    "tableA",
    "tableB",
    "tableC",
    ...
  ]
}

And then you can create a single resource that will loop that list.

resource "aws_cloudwatch_metric_alarm" "foobar" {
  for_each = toset(local.dynamodb_names)
  alarm_name                = "dynamodb-alarm-${each.key}"
  comparison_operator       = "GreaterThanOrEqualToThreshold"
  evaluation_periods        = "2"
  metric_name               = "CPUUtilization"
  namespace                 = "AWS/DynamoDB"
  period                    = "60"
  statistic                 = "Average"

  dimensions =  {
     TableName = "${each.key}"
  }

It will create all alarms based on the names that are in the dynamodb_names list.