I have attached terraform log screenshot. The task asks me to create an EC2 and cloudwatch alram for it to send a notification to SNS topic(which is already created at the start of the lab), when said EC2 CPU usage is over 90 percent for 5 consecutive mins.
I have compare the my terraform code to other users’ ones and it’s the same, but when i type terraform apply command, it was stuck in EC2 creation step.
Please note that, the following code is from starting another lab, so EC2 names and sns-topic names are different from the screenshot pictures but everything else is the same.
main.tf
resource "aws_sns_topic" "sns_topic" {
name = "xfusion-sns-topic"
}
data "aws_sns_topic" "existing_topic" {
name = "xfusion-sns-topic"
}
resource "aws_instance" "datacenter_ec2" {
ami = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"
tags = {
Name = "xfusion-ec2"
}
}
resource "aws_cloudwatch_metric_alarm" "datacenter_alarm" {
alarm_name = "xfusion-alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 1
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 300
statistic = "Average"
threshold = 90
alarm_actions = [data.aws_sns_topic.existing_topic.arn]
dimensions = {
InstanceId = aws_instance.datacenter_ec2.id
}
}
outputs.tf
output "KKE_instance_name" {
value = aws_instance.xfusion-ec2.tags["Name"]
}
output "KKE_alarm_name" {
value = aaws_cloudwatch_metric_alarm.xfusion-alarm.alarm_name
}

