Day 100: Create and Configure Alarm Using CloudWatch Using Terraform

This is task if failing with belo message can any one help me on this.

output is success.

Changes to Outputs:

  • KKE_alarm_name = “devops-alarm”
  • KKE_instance_name = “devops-ec2”

“Terraform plan is still showing pending changes, the resources may not have been updated or recreated as expected.”

Hi @arun1432

Once you apply your Terraform config and create the resources. As per the last instruction of the ask, you need to run terraform plan again.

santosh after terraform apply please find the screen shot for results which I got, after that when enter command terraform plan getting message.

Note: You didn’t use the -out option to save this plan, so Terraform can’t guarantee to take exactly these actions if you run “terraform
apply” now.


Again hit the command terraform apply below screen shot for the output, but task is failed due to your ec2 instance failed.

Hi @arun1432

Could you please share your Terraform code so we can support you better?

Please find the below code that I am using for task in Devops day 100 course.

EC2 Instance

resource “aws_instance” “xfusion-ec2” {
ami = “ami-0c02fb55956c7d316”
instance_type = “t2.micro”
tags = {
Name = “xfusion-ec2”
}
}

CloudWatch Alarm

resource “aws_cloudwatch_metric_alarm” “xfusion-alarm” {
alarm_name = “xfusion-alarm”
alarm_description = “Alarm when CPU exceeds 90% for 5 minutes”
comparison_operator = “GreaterThanOrEqualToThreshold”
evaluation_periods = 1
metric_name = “CPUUtilization”
namespace = “AWS/EC2”
period = 300
statistic = “Average”
threshold = 90
actions_enabled = true

Reference the existing SNS topic ARN directly

alarm_actions = [“arn:aws:sns:us-east-1:226175311211:xfusion-sns-topic”]

dimensions = {
InstanceId = aws_instance.xfusion-ec2.id
}

tags = {
Name = “xfusion-alarm”
}
}

=================================

output “KKE_instance_name” {
value = aws_instance.xfusion-ec2.tags[“Name”]
}

output “KKE_alarm_name” {
value = aws_cloudwatch_metric_alarm.xfusion-alarm.alarm_name
}

Hi @arun1432

Here’s my Terraform code that passed the task, please review it and compare it with yours, then try again. I’m not sure which validation error you encountered when the task failed, but make sure that the alarm_actions attribute references the aws_sns_topic correctly.

resource "aws_sns_topic" "sns_topic" {
  name = "nautilus-sns-topic"
}

resource "aws_instance" "nautilus_ec2" {
  ami           = "ami-0c02fb55956c7d316"   # Ubuntu AMI
  instance_type = "t2.micro"
  tags = {
    Name = "nautilus-ec2"
  }
}

# --------------------------
# CloudWatch Alarm
# --------------------------
resource "aws_cloudwatch_metric_alarm" "nautilus_alarm" {
  alarm_name          = "nautilus-alarm"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = 1
  metric_name         = "CPUUtilization"
  namespace           = "AWS/EC2"
  period              = 300
  statistic           = "Average"
  threshold           = 90

  alarm_description   = "Alarm when CPU utilization exceeds 90% for 5 minutes"
  dimensions = {
    InstanceId = aws_instance.nautilus_ec2.id
  }

  alarm_actions = [
    aws_sns_topic.sns_topic.arn
  ]

  treat_missing_data = "notBreaching"
}

Hi raymond,

I compared with your code all looks good. Now task is success. not sure what went worng earlier.
Thank you for sharing the code.

@arun1432 Some times also eve if your code is right and you don’t follow this last instruction
Note:
3. Before submitting the task, ensure that terraform plan returns No changes. Your infrastructure matches the configuration.

if you don’t follow this then you will fail the task