[英]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.