Adding Health Check’s for Route-53 using Terraform

Adding Health Check’s for Route-53 using Terraform

OverView :-

Route 53, #AmazonWebService managed #DNS service, plays a crucial role in directing traffic across your #infrastructure. To enhance the reliability of your applications, you can set up health checks for your #Route-53 records using #Terraform. Health checks monitor the status of your resources and automatically adjust routing based on their availability.

Pre-requestites :-

  1. An AWS account with appropriate permissions to create Secrets Manager resources.

  2. #Terraform installed on your local machine.

  3. AWS CLI configured with your #AWS credentials.

Step-1 :- Setting Up Your #Terraform Configuration Files Create three files: main.tf, variable.tf, and output.tf . Open your preferred text editor(vscode) and create these files.

Step-2 :- Writing the #Terraform Code Now, let’s dive into the main.tf file and start writing our Terraform code.Next define the sns topic and route-53 health check resources required for our #deployment. Here’s an example of how you can define the secret:

Health checks Using Terraform :-

# Create an SNS topic for the alarm actions
resource "aws_sns_topic" "sns_topic" {
    name = var.topic_name
}

resource "aws_cloudwatch_metric_alarm" "healthcheck_alarm" {
  alarm_name          = "route-53-health_check_alarm"
  comparison_operator = "GreaterThanOrEqualToThreshold"
  evaluation_periods  = 1
  metric_name         = "HealthCheckStatus"
  namespace           = "AWS/Route53"
  period              = 60
  statistic           = "Minimum"
  threshold           = 1
  alarm_description   = "This metric monitors route-53-healthchecks"
  actions_enabled     = "true"
  alarm_actions       = [aws_sns_topic.sns_topic.arn]
  treat_missing_data  = "breaching"
  dimensions = {
      HealthCheckId = aws_route53_health_check.route-53_healthcheck.id
   }
  depends_on = [
     aws_route53_health_check.route-53_healthcheck
    ]
}

# Define the health check resource
resource "aws_route53_health_check" "route-53_healthcheck" {
  fqdn              = "record.${var.domain_name}"
  port              = 443 
  type              = "HTTPS"
  resource_path     = "/auth/login"
  failure_threshold = "5"
  request_interval  = "30"
  #alarm_identifier  = "realogy-app-${var.env_name}-health_check_alarm"
  cloudwatch_alarm_name = aws_cloudwatch_metric_alarm.healthcheck_alarm.name
  cloudwatch_alarm_region = "${var.region}"
  tags = {
    Name = "route-53-health_checks"
  }
}

step-3 :- Now define the variables for the above main.tf file

#variable.tf
variable "domain_name" {
  type    = string
  default = "my_domain.com"
}

variable "topic_name" {
  type    = string
  default = "route-53-sns-topic"
}

variable "region" {
  type    = string
  default = "us-east-1"
}

Step-4 :- if u need outputs for the above configuration, define the outputs in outputs.tf file as shown like below.

#outputs.tf
output "sns_topic_arn" {
 value = aws_sns_topic.sns_topic.arn
}
output "cloudwatch_metric_alarm_arn" {
 value = aws_cloudwatch_metric_alarm.healthcheck_alarm.arn
}

Step-5 :- Finally, Deploy the above code using terraform commands. Open a comand prompt and locate to your terraform configuration directoruy and Run below commands one-by-one to deploy the code.

terraform init
terraform plan
terraform apply

Step-6 :- Once the code deploys, Login to your aws console and naviagate to #CloudWatch alarm service and check the alarms are being created or not. Also go to route-53 and check the health checks are being added.

Source-code :- github.com/yourusername/terraform-s3-iam.git

Conclusion :-

#Integrating health checks with #Route-53 using Terraform provides a dynamic and efficient approach to managing the availability of your resources. By automating the setup and management of health checks, you ensure that your #infrastructure is responsive, #reliable, and capable of providing a seamless user #experience.