100-Days-Of-DevOps-Challenge-KodeKloud

Create and Configure Alarm Using CloudWatch Using Terraform

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.

  1. Launch EC2 Instance: Create an EC2 instance named nautilus-ec2 using any appropriate Ubuntu AMI (you can use AMI ami-0c02fb55956c7d316).

  2. Create CloudWatch Alarm: Create a CloudWatch alarm named nautilus-alarm with the following specifications:

    • Statistic: Average
    • Metric: CPU Utilization
    • Threshold: >= 90% for 1 consecutive 5-minute period
    • Alarm Actions: Send a notification to the nautilus-sns-topic SNS topic.
  3. Update the main.tf file (do not create a separate .tf file) to create a EC2 Instance and CloudWatch Alarm.

  4. 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:

  1. The Terraform working directory is /home/bob/terraform.

  2. Right-click under the EXPLORER section in VS Code and select Open in Integrated Terminal to launch the terminal.

  3. Before submitting the task, ensure that terraform plan returns No changes. Your infrastructure matches the configuration.

Steps

  1. Create a variables.tf file with these contents:

     variable "prefix" {
         default = "nautilus"
     }
    

    Replace nautilus according to your task description

  2. 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.

  3. 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
     }
    
  4. Run the terraform commands:

     terraform init
     terraform plan
     terraform apply -auto-approve
    

Youtube Video

Referrence

Good To Know

CloudWatch Alarm Configuration

EC2 Instance Monitoring

SNS Topic Integration

Terraform Best Practices

Monitoring Thresholds

Common Issues & Solutions

Production Considerations