External monitoring integration with StatusCake

External monitoring is helpful because it gives insights on how performant your infrastructure is, as seen from the outside, maybe from many places in the world. We can build our own availability monitoring systems, or we can use third-party services. StatusCake is a good example for us as they have a good API and a free service tier for us to try with Terraform. We'll monitor two things: host latency and HTTP availability.

Getting ready

To step through this recipe, you will need the following:

  • A working Terraform installation
  • A StatusCake account (https://statuscake.com)
  • Optionally, an infrastructure managed by Terraform (refer to the previous recipes)
  • An Internet connection

How to do it…

Start by setting the new statuscake provider, as we did with AWS or GitHub, using a username and API key:

provider "statuscake" {
  username = "${var.statuscake_username}"
  apikey   = "${var.statuscake_apikey}"
}

Declare the variables in variables.tf:

variable "statuscake_username" {
  default     = "changeme"
  description = "Sets the StatusCake Username"
}

variable "statuscake_apikey" {
  default     = "hackme"
  description = "Sets the StatusCake API Key"
}

Also, don't forget to set those variables to your own values in terraform.tfvars.

Creating an automated ping monitoring test

Let's create an initial test, a simple ICMP ping to a server whose IP is 1.2.3.4, every 5 minutes:

resource "statuscake_test" "latency" {
  website_name = "My Server Latency"
  website_url  = "1.2.3.4"
  test_type    = "PING"
  check_rate   = 300
  paused       = false
}

Note

The website_name or website_url can be a reference to an existing Terraform resource. If our AWS instance resource is named centos, you can access the value dynamically like this, instead of a static value:

website_url = "${aws_instance.centos.public_ip}"

If your resource has a count number, you can iterate through it so all the available instances are automatically monitored. It works like this:

resource "statuscake_test" "another_latency" {
  website_name = "${element(aws_instance.centos.*.public_ip, count.index)}"
  website_url  = "${element(aws_instance.centos.*.public_ip, count.index)}"
  test_type    = "PING"
  check_rate   = 300
  paused       = false
}

Another useful feature is to switch the value of paused to true for planned downtimes, so you're not hammered with alerts you're already aware of.

Creating an HTTPS test

A very common test we'll want to make is HTTP availability. It's really no different than an ICMP check;

resource "statuscake_test" "http" {
  website_name = "www.myweb.com Availability"
  website_url  = "https://www.myweb.com:443"
  test_type    = "HTTP"
  check_rate   = 300
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.107.210