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.
To step through this recipe, you will need the following:
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
.
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 }
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.
3.139.107.210