Monitoring as Code + Infrastructure as code: Why you should create your monitoring along with your resources on AWS

Woltter Xavier
3 min readSep 1, 2021
Photo by Ibrahim Boran on Unsplash

Monitoring as code is a monitoring strategy that ensures monitoring of infrastructure and applications for high availability and serviceability. Monitoring as code means monitoring configuration details are stored in version control repositories along with your infrastructure, making them easier to share with team members or track changes over time. In this blog post, we will discuss the benefits of monitoring as code using Terraform as a tool for state management and why you should consider it for your business.

Getting ready

Before monitoring infrastructure, you should first have a solid understanding of why monitoring is important. Monitoring helps to ensure that your services are available all the time and provides key insights into application performance so that you can keep it up and running at peak levels. You need monitoring because unplanned downtime or poor service quality impact revenue, customer satisfaction, and brand reputation.

Monitoring as code makes monitoring easier to manage since it allows you to codify monitoring configurations into a single source of truth. This means everyone can access the monitoring data in real-time using one version control repository along with infrastructure configuration files that both contain all changes over time making them easy to share across the team or reproduce the monitoring environment.

Benefits of monitoring as code using Terraform

Terraform is a powerful tool that allows you to declare infrastructure as code and codify monitoring configurations in one place. Monitoring configuration files are written in YAML or JSON formats using the HCL (Hashicorp Configuration Language). We can take advantage of the state management nature of Terraform and apply monitoring as code in our existing AWS resources provisioned using infrastructure as code.

Once you have configured your monitoring infrastructure using terraform, it allows you to easily create a single monitoring environment that monitors all of your infrastructure. This environment can be used by monitoring tools to send monitoring data and metrics in real-time.

Terraform also allows you to easily add, remove or update monitoring configuration files without having the fear that it will break existing monitoring configurations. You can use Terraform’s plan command which creates a preview of changes before making same as your infrastructure.

Why you should create your monitoring along with your resource

If you are monitoring your resources, then monitoring should be treated as infrastructure. As a result, monitoring configurations need to be version controlled and managed along with the rest of infrastructure configuration files. This allows for easier collaboration across teams and ensures that there is no discrepancy between monitoring practices used by different team members. Managing monitoring using Terraform provides an added

Let’s provision a ECS cluster in AWS with monitoring using Datadog. We will use monitoring as code leveraging Terraform to build our monitoring along with the infrastructure.

Here we add the resource monitoring along with our resource.

We’re going to have the following monitor on Datadog provisioned along with our ECS cluster:

From metrics in Datadog we can create dashboards and alerts like this: 🚀

Conclusion

In conclusion monitoring as code using terraform provides monitoring teams the ability to codify infrastructure monitoring configurations into a single source of truth. With Terraform, it is possible create monitoring environment that monitors all infrastructure and services efficiently at scale while allowing for easy updates and changes over time without breaking existing monitoring implementations.

Linkedin: https://www.linkedin.com/in/woltter/
Github: https://github.com/wvxavier

--

--