YURI GRINSHTEYN: We try to automate as much as possible
in our environments, but we often treat
monitoring as an afterthought.
Today, I'm going to show you how to get started with automating
the setup of your monitoring configuration
for a new project using Terraform.
This is "The Stack Doctor."
[MUSIC PLAYING]
Let's start by reviewing some basics.
We need to cover two major things--
what should we automate when setting up monitoring,
and how we can do that.
Using the Cloud Monitoring API, we can automate three things--
uptime checks, which are automated probers that
can let us know if our service is down or slow,
alerting policies that create incidents and generate
notifications, and dashboards that
show us the state and health of our systems.
Now that we know what we can automate,
the next question is how.
Here, we also have three options.
We can use the API directly, we can use Cloud Deployment
Manager, or we can use an open-source tool
like Terraform.
Let's take a quick look at each of those.
Our first option is to use the API or the gcloud command
to create our entities.
For example, you can make a call to
the uptimeCheckConfigs.create method
to create a new uptime check.
You can use the API Explorer to determine
exactly how to make the call.
Our recommendation would be to use the API directly only when
no other options are available, since tools
like Deployment Manager and Terraform
make this much easier.
Our second option is to use Cloud Deployment Manager.
It allows you to specify all the resources needed
for your application in a declarative format using
YAML files and then execute them using
the gcloud command-line tool.
You can also use templates to parameterize the configuration
for better reuse.
Deployment Manager has full support for Google Cloud
services, and the state or information
about what's already been deployed
is stored in the cloud.
Our final option is Terraform.
It's an open-source product that allows you
to provision resources across multiple clouds
through the use of providers.
You define the resources you want to provision in .TF files,
then use the Terraform command-line tool to validate,
plan, and apply your deployment.
The state is stored locally.
Now that we've outlined both what we can automate
and what our options for automation are,
let's take a look at a specific example.
Today, I want to show you how to create an uptime check using
Terraform.
You'll need to start by downloading and installing
Terraform to your workstation.
Note that, if you're using Cloud Shell, it's installed for you.
You can use terraform version to confirm.
Next, let's create our Terraform file.
First, we'll specify the provider.
We want to use Google, and we'll provide a default zone
and region for compute resources.
Next, we're going to define the resource we're creating.
For our example, we're creating an HTTPS uptime check
against the root of the web server
on the standard HTTPS port, which is 443.
Next, we specify the monitored resource.
This is where we tell the uptime check the host we're targeting.
Note that we do not provide the HTTP or HTTPS here.
We've already specified that.
Finally and optionally, we can provide a text string.
The uptime check will validate that the content returned
from the server includes that string
so that we can check availability, latency,
and correctness all at once.
We're now ready to test and run our code.
We're going to use the Terraform init,
plan, and apply commands to make sure our code is correct,
see exactly what it's going to do,
and actually create the check.
First, run terraform init to install the Google Cloud
provider and initialize your Terraform configuration.
Make sure you get the confirmation message
that this step was successful.
Next, run terraform plan to confirm that the code is
going to do what you expect.
In our case, it should create a single new resource,
the uptime check we've defined.
Finally, run terraform apply to actually create the check.
Note that you'll need to explicitly confirm that you
want to perform this operation.
At the end, confirm that our operation
created the single new resource by looking
at the output of the command.
Next, head to the console to see your uptime
check in the console.
Note that it may take a few minutes for it
to start running and returning data.
Let's review what we covered today.
We learned that cloud monitoring allows
us to automate the creation of some common resources using
the monitoring configuration, like uptime checks,
alerting policies, and dashboards.
We also reviewed automation options
and looked at the monitoring API, Cloud Deployment Manager,
and Terraform.
Finally, we walked through a specific example
of creating an uptime check using Terraform.
I hope you've found this introduction to setting up
automation and treating your monitoring as code useful.
That's it for this episode.
Come back next time when we dive into automating
creating dashboards.
This is The Stack Doctor.
Stay healthy out there.
[MUSIC PLAYING]