The Nautilus DevOps team has been tasked with setting up an EC2 instance for their application. To ensure the application performs optimally, they also need to create a CloudWatch alarm to monitor the instance’s CPU utilization. The alarm should trigger if the CPU utilization exceeds 90% for one consecutive 5-minute period. To send notifications, use the SNS topic named nautilus-sns-topic, which is already created.
Launch EC2 Instance: Create an EC2 instance named nautilus-ec2 using any appropriate Ubuntu AMI (you can use AMI ami-0c02fb55956c7d316).
Create CloudWatch Alarm: Create a CloudWatch alarm named nautilus-alarm with the following specifications:
AverageCPU Utilization>= 90% for 1 consecutive 5-minute periodnautilus-sns-topic SNS topic.Update the main.tf file (do not create a separate .tf file) to create a EC2 Instance and CloudWatch Alarm.
Create an outputs.tf file to output the following values:
KKE_instance_name for the EC2 instance name.KKE_alarm_name for the CloudWatch alarm name.Notes:
The Terraform working directory is /home/bob/terraform.
Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.
Before submitting the task, ensure that terraform plan returns No changes. Your infrastructure matches the configuration.
Create a variables.tf file with these contents:
variable "prefix" {
default = "nautilus"
}
Replace
nautilusaccording to your task description
Create a main.tf file with these contents:
resource "aws_sns_topic" "sns_topic" {
name = "${var.prefix}-sns-topic"
}
resource "aws_instance" "ec2_instance" {
ami = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"
tags = {
Name = "${var.prefix}-ec2"
}
}
resource "aws_cloudwatch_metric_alarm" "cw_alarm" {
alarm_name = "${var.prefix}-alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 1
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = 300
statistic = "Average"
threshold = 90
dimensions = {
InstanceId = aws_instance.ec2_instance.id
}
alarm_actions = [aws_sns_topic.sns_topic.arn]
}
There is a complete terraform file: main.tf with vpc, subnet, igw, route table, security group, ec2 key pair, sns topic and cloudwatch alarm. You can give it a look.
Create an outputs.tf file with these contents:
output "KKE_instance_name" {
value = aws_instance.ec2_instance.tags["Name"]
}
output "KKE_alarm_name" {
value = aws_cloudwatch_metric_alarm.cw_alarm.alarm_name
}
Run the terraform commands:
terraform init
terraform plan
terraform apply -auto-approve
GreaterThanOrEqualToThreshold with threshold 90 means CPU >= 90%Average, Maximum, Minimum, Sum, SampleCount - Average is most common for CPUInstanceId not instanceId for EC2 instancesCPUUtilization, NetworkIn, NetworkOut, DiskReadOps, DiskWriteOpsami-0c02fb55956c7d316 is for us-east-1aws_sns_topic.name.arn to reference topic ARN in alarm actionstags["Name"] for dynamic outputs