使用带有数据库连接公式的Terraform设置数据库连接警报

发布于 2025-01-25 02:22:16 字数 815 浏览 3 评论 0原文

我正在尝试设置Terraform中数据库连接的CloudWatch警报。

`resource "aws_cloudwatch_metric_alarm" "db_connections" {
  alarm_name          = "db_connections"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "DatabaseConnections"
  namespace           = "AWS/RDS"
  period              = "300"
  statistic           = "Average"
  threshold           = var.db_connection_threshold
  alarm_description   = "Db connections"
  alarm_actions       = [aws_sns_topic.topic.arn]
 

  dimensions = {
    DBInstanceIdentifier = "${var.db_instance_id}"
  }
}
`

我要实现的是使用此公式最大的设置({log(dbinstanceclassmemory/805306368,2)*45},{log(log(dbinstanceclassmemory/8187281408,2)*1000})。如果大于90%,则警报会上升。 在Terraform中是否有任何可能性。我尝试给出var threshold = formula/100,但它不起作用。有什么建议吗?谢谢

I am trying to setup the Cloudwatch alarm for database connections in Terraform.

`resource "aws_cloudwatch_metric_alarm" "db_connections" {
  alarm_name          = "db_connections"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "1"
  metric_name         = "DatabaseConnections"
  namespace           = "AWS/RDS"
  period              = "300"
  statistic           = "Average"
  threshold           = var.db_connection_threshold
  alarm_description   = "Db connections"
  alarm_actions       = [aws_sns_topic.topic.arn]
 

  dimensions = {
    DBInstanceIdentifier = "${var.db_instance_id}"
  }
}
`

What i am trying to achieve is to setup using this formula GREATEST({log(DBInstanceClassMemory/805306368,2)*45},{log(DBInstanceClassMemory/8187281408,2)*1000}). If it is greater than 90 percent then the Alarm rises.
Is there any possibility for that in Terraform. I tried with giving var threshold =formula/100 and it didn't work. Any suggestions? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜宝宝 2025-02-01 02:22:16

不确定您是否必须使用该公式来创建DB连接警报以及您使用的数据库引擎,但是,这是我设法计算数据库的最大连接并创建警报的方式。

我计算了知道实例内存的最大DB连接,并使用页面数据库连接的最大数量。例如,对于mysql:

{dbinstanceclassmemory/12582880}

要找到dbinstanceclassmemory,您必须将实例的内存从gib(gibibytes)转换为字节。

1 gib = 1073741824字节

可以在页面 DB实例类的硬件规格。因此,db.t3.small(mysql)的最大连接数为:

(2 * 1073741824) / 12582880〜 = 170.66710 < / p>

因此该实例的最大值约为 170 Connections < / strong>。知道数字您可以轻松地使用百分比计算警报的阈值。

我的 Terraform 模块看起来像这样:

...
variable "db_instance_name" {
  type        = string
  nullable    = false
}

variable "db_instance_class" {
  type        = string
  nullable    = false
}

variable "db_connections_limit_threshold" {
  type        = number
  default     = 80
  description = "The maximum percent of connections connected to the DB instance. Default: 80"
}

locals {
  datasets = {
    # https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.DBInstanceClass.html#Concepts.DBInstanceClass.Summary
    db_instance_classes_memory_gib = {
      # db.t4g – burstable-performance instance classes with AWS Graviton2 processors
      "db.t4g.2xlarge" : 32,
      "db.t4g.xlarge"  : 16,
      "db.t4g.large"   : 8,
      "db.t4g.medium"  : 4,
      "db.t4g.small"   : 2,
      "db.t4g.micro"   : 1,
      # db.t3 – burstable-performance instance classes
      "db.t3.2xlarge"  : 32,
      "db.t3.xlarge"   : 4,
      "db.t3.large"    : 8,
      "db.t3.medium"   : 4,
      "db.t3.small"    : 2,
      "db.t3.micro"    : 1,
      # db.t2 – burstable-performance instance classes
      "db.t2.2xlarge"  : 32,
      "db.t2.xlarge"   : 16,
      "db.t2.large"    : 8,
      "db.t2.medium"   : 4,
      "db.t2.small"    : 2,
      "db.t2.micro"    : 1
    }
  }
  # https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html#RDS_Limits.MaxConnections
  # Max Connections: DBInstanceClassMemoryBytes/12582880
  # db.t3.micro: 1 GiB (RDS Instance RAM) = 1073741824 bytes (DBInstanceClassMemoryBytes)
  max_connections_limit = (local.datasets.db_instance_classes_memory_gib[var.db_instance_class] * 1073741824) / 12582880
  db_connections_limit_threshold = floor((local.max_connections_limit / 100) * var.db_connections_limit_threshold)
}

# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm
resource "aws_cloudwatch_metric_alarm" "db_connections_limit" {
  alarm_name          = "rds-${var.db_instance_name}-limitDBConnections"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "5"
  metric_name         = "DatabaseConnections"
  namespace           = "AWS/RDS"
  period              = "60"
  statistic           = "Average"
  threshold           = local.db_connections_limit_threshold
  alarm_description   = "Average database connections amount reached ${var.db_connections_limit_threshold} percent of the limit, may cause connection disruption"
  alarm_actions       = var.sns_topic_arns
  ok_actions          = var.sns_topic_arns

  dimensions = {
    DBInstanceIdentifier = var.db_instance_name
  }
}
...

Notes!,因为我找不到有关内存的实例硬件规范的来源,我将其硬编码为local varible 数据集中由于此信息的更改的可能性较小。

Not sure whether you must use that formula to create the DB connections alarm and what database engine you use, however, here is how I managed to calculate the maximum connections for my databases and create the alarm.

I calculated the maximum DB connections knowing the instance's memory and using the formula from the page Maximum number of database connections. For instance, for MySQL:

{DBInstanceClassMemory/12582880}

To find the DBInstanceClassMemory, you must convert the instance's memory from GiB (Gibibytes) to Bytes.

1 GiB = 1073741824 bytes

The instance classes' memory amount in GiB can be found on the page Hardware specifications for DB instance classes. So, the maximum number of connections for db.t3.small (MySQL) is:

(2 * 1073741824) / 12582880 ~= 170.66710

As a result, the instance can have a maximum of around 170 connections. Knowing the number you can easily calculate the threshold for your alarm using the percentage.

My Terraform module looks something like this:

...
variable "db_instance_name" {
  type        = string
  nullable    = false
}

variable "db_instance_class" {
  type        = string
  nullable    = false
}

variable "db_connections_limit_threshold" {
  type        = number
  default     = 80
  description = "The maximum percent of connections connected to the DB instance. Default: 80"
}

locals {
  datasets = {
    # https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.DBInstanceClass.html#Concepts.DBInstanceClass.Summary
    db_instance_classes_memory_gib = {
      # db.t4g – burstable-performance instance classes with AWS Graviton2 processors
      "db.t4g.2xlarge" : 32,
      "db.t4g.xlarge"  : 16,
      "db.t4g.large"   : 8,
      "db.t4g.medium"  : 4,
      "db.t4g.small"   : 2,
      "db.t4g.micro"   : 1,
      # db.t3 – burstable-performance instance classes
      "db.t3.2xlarge"  : 32,
      "db.t3.xlarge"   : 4,
      "db.t3.large"    : 8,
      "db.t3.medium"   : 4,
      "db.t3.small"    : 2,
      "db.t3.micro"    : 1,
      # db.t2 – burstable-performance instance classes
      "db.t2.2xlarge"  : 32,
      "db.t2.xlarge"   : 16,
      "db.t2.large"    : 8,
      "db.t2.medium"   : 4,
      "db.t2.small"    : 2,
      "db.t2.micro"    : 1
    }
  }
  # https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html#RDS_Limits.MaxConnections
  # Max Connections: DBInstanceClassMemoryBytes/12582880
  # db.t3.micro: 1 GiB (RDS Instance RAM) = 1073741824 bytes (DBInstanceClassMemoryBytes)
  max_connections_limit = (local.datasets.db_instance_classes_memory_gib[var.db_instance_class] * 1073741824) / 12582880
  db_connections_limit_threshold = floor((local.max_connections_limit / 100) * var.db_connections_limit_threshold)
}

# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm
resource "aws_cloudwatch_metric_alarm" "db_connections_limit" {
  alarm_name          = "rds-${var.db_instance_name}-limitDBConnections"
  comparison_operator = "GreaterThanThreshold"
  evaluation_periods  = "5"
  metric_name         = "DatabaseConnections"
  namespace           = "AWS/RDS"
  period              = "60"
  statistic           = "Average"
  threshold           = local.db_connections_limit_threshold
  alarm_description   = "Average database connections amount reached ${var.db_connections_limit_threshold} percent of the limit, may cause connection disruption"
  alarm_actions       = var.sns_topic_arns
  ok_actions          = var.sns_topic_arns

  dimensions = {
    DBInstanceIdentifier = var.db_instance_name
  }
}
...

NOTE! As I could not find the source of the instance's hardware specification about memory, I hardcoded it into the local variable datasets as this information is less likely to be changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文