简体   繁体   English

如何通过 Terraform 创建 EKS 节点的 Cloudwatch 警报

[英]How to create Cloudwatch Alarm of EKS Nodes via Terraform

I'm trying to create an alarm of a EKS Nodes by terraform but I'm not able to do it, because I don't know how to do a reference of the node instances.我正在尝试通过 terraform 创建 EKS 节点警报,但我无法做到,因为我不知道如何引用节点实例。 I have this code:我有这个代码:

resource "aws_cloudwatch_metric_alarm" "cpu_high_nodes" {
  for_each   = local.ob
  
  alarm_name          = "${var.cluster_name}-nodes-cpu-high-${each.value}"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = "2"
  metric_name         = "CPUUtilization"
  namespace           = "AWS/EC2"
  period              = "300"
  statistic           = "Maximum"
  threshold           = "85"
  alarm_description   = "Scale up if the cpu avg is above 85% for 10 minutes"

dimensions = {
    InstanceId =  "instance-id"
 }

If I put the InstanceId by hand, the alarm works perfectly,but problem is to get an output from the task which create the nodes..如果我手动放置 InstanceId,警报会完美运行,但问题是从创建节点的任务中获取 output ..

resource "aws_launch_template" "worker-node" {
  for_each               = local.ob

  image_id               = "ami-038341f2c72928ada"
  name                   = "${var.cluster_name}-worker-node-${each.value}"
  instance_type          = "t3.medium"

  block_device_mappings {
    device_name = "/dev/xvda"

    ebs {
      volume_size = 20
      volume_type = "gp2"
    }
  }
  user_data                     = base64encode(data.template_file.user_data.template)
  tag_specifications {
    resource_type = "instance"
    tags = {
      "Instance Name" = "${var.cluster_name}-node-${each.value}"
       Name = "${var.cluster_name}-node-${each.value}"
    }
  }
}

If I get.id of this task I'm getting de launch template ID and actually I need the EC2 instance ID, but I don't know how to get it..如果我获取此任务的.id,我将获得启动模板 ID,实际上我需要 EC2 实例 ID,但我不知道如何获取它。

ANy idea?任何想法?

THx谢谢

One way of doing it is to use Cluster-Name tags in your EC2 Node instances and reference them by a data source.一种方法是在您的 EC2 节点实例中使用 Cluster-Name 标签并通过数据源引用它们。

Example:例子:

data "aws_instances" "eks-instances" {
  filter {
    name   = "tag:eks:cluster-name"
    values = [var.eks_cluster_name]
  }
}

resource "aws_cloudwatch_metric_alarm" "cpu_high_nodes" {
  for_each = toset(data.aws_instances.eks-instances.ids)
  .
  .
  .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Terraform:Cloudwatch Canary Synthetics,如何创建指标警报 - Terraform: Cloudwatch Canary Synthetics, How to create metric alarm 无法通过Terraform在CloudWatch警报中使用单位 - Cannot use unit in cloudwatch alarm via Terraform Terraform:无法在实例上创建cloudwatch警报 - Terraform : Unable to create cloudwatch alarm on a instance Terraform AWS Cloudwatch 警报 - Terraform AWS Cloudwatch alarm 如何编写 terraform 代码来为太多的数据库连接创建 aws_cloudwatch_metric_alarm? - How do I write a terraform code to create an aws_cloudwatch_metric_alarm for too many db connections? 如何通过 CDK 创建带节点的 EKS 集群? - How do I create an EKS cluster with nodes via CDK? 使用Terraform创建云监视警报(metric_alarm)。 如何为单个主机使用alarm_actions? - Using Terraform to create a cloudwatch alert (metric_alarm). How can I use alarm_actions for an individual host? 是否可以使用 Terraform 为 AutoScaling 组的同一 CloudWatch 警报创建多个条件? - It is possible to create multiple conditions for the same CloudWatch alarm for AutoScaling group with Terraform? 节点未通过 Terraform 连接到 EKS 集群 - The nodes doesn't attach to the EKS cluster via Terraform 使用 Terraform 的 CloudWatch 指标警报 - CloudWatch metric alarm using Terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM