如何编写 terraform 代码来为过多的数据库连接创建 aws_cloudwatch_metric_alarm?

Posted

技术标签:

【中文标题】如何编写 terraform 代码来为过多的数据库连接创建 aws_cloudwatch_metric_alarm?【英文标题】:How do I write a terraform code to create an aws_cloudwatch_metric_alarm for too many db connections? 【发布时间】:2021-04-24 06:42:09 【问题描述】:

在 AWS CloudWatch 中,我可以创建一个警报,当我的数据库连接过多时会提醒我:

我已经使用 terraform 创建了另一个警报...

resource "aws_cloudwatch_metric_alarm" "cpu_utilization_too_high" 
  alarm_name          = "cpu_utilization_too_high"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "CPUUtilization"
  namespace           = "AWS/RDS"
  period              = "600"
  statistic           = "Average"
  threshold           = var.cpu_utilization_threshold
  alarm_description   = "Average database CPU utilization over last 10 minutes too high"
  alarm_actions       = [aws_sns_topic.topic.arn]
  ok_actions          = [aws_sns_topic.topic.arn]

  dimensions = 
    DBInstanceIdentifier = "$var.db_instance_id"
  

现在我想使用 terraform 创建一个警报,提醒我注意我的数据库连接,但我不知道将 metric_name 设置为什么...

  metric_name         = ???TooMuchConnectingtoDataBase???

我查看了 terraform 文档,但它没有记录 metric_name 的用途。 https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm

【问题讨论】:

【参考方案1】:

根据doc,

resource "aws_cloudwatch_metric_alarm" "too_many_db_connections" 
  alarm_name          = "too_many_db_connections"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "DatabaseConnections"
  namespace           = "AWS/RDS"
  period              = "600"
  statistic           = "Average"
  threshold           = var.db_connection_threshold
  alarm_description   = "Average db connections over last 10 minutes is too high"
  alarm_actions       = [aws_sns_topic.topic.arn]
  ok_actions          = [aws_sns_topic.topic.arn]

  dimensions = 
    DBInstanceIdentifier = "$var.db_instance_id"
  

【讨论】:

以上是关于如何编写 terraform 代码来为过多的数据库连接创建 aws_cloudwatch_metric_alarm?的主要内容,如果未能解决你的问题,请参考以下文章

如何发现现有的 Terraform 版本

如何自动切换角色策略(Terraform)

写在前面-Terraform

如何在 VS Code 中配置 terraform 代码的对齐和缩进?

如何安装多个或两个版本的 Terraform?

如何从 terraform 状态中删除资源?