LogicMonitor seeks to disrupt AI landscape with $800M strategic investment at $2.4B valuation to revolutionize data centers.

Learn More

Announcing the LogicMonitor Terraform Provider

Click* Click* Scroll* Click*

That sound is the bane of my existence. That sound – the sound of commands being typed and buttons being clicked – signified how we handled many aspects of our technical operations work a few years ago. Manually…

Click*

Back when we had a smaller cloud infrastructure, EC2 hosts, roles/policies, DNS records, etc. were all created manually. As our cloud presence grew, we developed a few in-house solutions to help manage the creation of new resources. This approach became difficult to manage as our server count rose and infrastructure complexity grew. At that point, our EC2 Management Console looked like a scene from a Mad Max movie. In order to efficiently build, scale, and manage our infrastructure, we adopted HashiCorp’s Terraform.

Why Terraform

Terraform is an open source orchestration tool for provisioning, managing, and versioning cloud infrastructure. Terraform uses a configuration file as a blueprint of the desired infrastructure state, and changes the target environment by updating or creating resources to match the defined state. Having a defined outline of your datacenter and following this “infrastructure as code” model allows for repeatable automation.

We were in the process of moving LogicMonitor’s main application towards a service oriented architecture. Terraform gave us the ability to abstract different pieces of our infrastructure configurations into archivable, version controlled code. As Terraform became our go-to tool for managing our cloud infrastructure, another problem arose… how were we going to get all these newly provisioned items into our monitoring solution?

Click*

I think every great Ops tool is the offspring of momentary brilliance and just pure, unwavering laziness. That is how the LogicMonitor Terraform Provider came to be. As we started to move away from a single monolithic application, we had to provision more instances and utilize other services in Amazon Web Services to match the increased complexity of our infrastructure. The LogicMonitor Terraform Provider saves our operations team time and occasional carpal tunnel by automatically adding and managing these new resources within our monitoring solution.

Getting Started

For those new to Terraform, I recommend reading the official Terraform Getting Started documentation. This article does a good job of introducing the application and its various elements (i.e. resources, variables, dependencies etc). In the following few examples I’ll show you how to use the LogicMonitor Terraform Provider to add devices and device groups into LogicMonitor. We will also combine elements in Terraform to create a real world example of how to implement the LogicMonitor provider in conjunction to other resources.

First we need to authenticate (grab a set of API credentials within LogicMonitor). One way is to initialize within the logicmonitor provider block.


    provider "logicmonitor" {
      api_id  = "${var.logicmonitor_api_id}"
      api_key = "${var.logicmonitor_api_key}"
      company = "${var.logicmonitor_company}"
    }
  

The other is setting the credentials through the provider’s supported environment variables.


    export LM_API_ID=xyz
    export LM_API_KEY=xyz
    export LM_COMPANY=xyz
  

To simply create a device group resource into LogicMonitor, create a file main.tf with the contents:


    resource "logicmonitor_device_group" "group1" {
      name = "NewGroup"
      properties {
        "snmp.community" = "xxxxx"
        "system.categories" = "a,b,c,d"
        "jmx.port" = "9003"
      }
    }
  

A terraform apply will create a new device group named “NewGroup” with set properties for “snmp.community”, “system.categories”, and “jmx.port”.


    logicmonitor_device_group.group1: Creating...
      disable_alerting:             "" => "true"
      name:                         "" => "NewGroup"
      properties.%:                 "0" => "3"
      properties.jmx.port:          "" => "9003"
      properties.snmp.community:    "" => "xxxxx"
      properties.system.categories: "" => "a,b,c,d"
    logicmonitor_device_group.group1: Creation complete (ID: 779)
  


Terraform

The terraform apply run earlier created a terraform.tfstate file locally. This file will be used by Terraform to compare resources defined in your template to the actual state of your infrastructure. This includes the configuration of these resources within LogicMonitor. If a device is deleted or group properties are updated within LogicMonitor, the next terraform apply will configure your LogicMonitor account to match the local state file. This state file should be kept remotely when collaborating with teammates to avoid multiple contributors using their own local saved state file.

Terraform Data Sources

Terraform Data Sources allow data to be fetched so that it can be used elsewhere in other pieces of terraform configurations. For example, in the LogicMonitor Terraform Provider, we currently do not provision Collector resources but users can use Terrafrom data sources to define which existing Collector to associate a device to (note that a device must be associated to a Collector). The Collector data source looks up active Collectors based on date of creation or by filtered name/description. Device group data sources can also be used to filter for specific groups that match a name or custom properties.

Here is an example of adding a device using the data sources to look up a specific device group and Collector:


    data "logicmonitor_collectors" "collectors" {
      most_recent = true
    }
    resource "logicmonitor_device" "host" {
      ip_addr          = "10.32.12.18"
      disable_alerting = true
      collector        = "${data.logicmonitor_findcollectors.collectors.id}"
      hostgroup_id     = "${data.logicmonitor_devicegroup.devicegroups.id}"
      properties {
       "app" = "haproxy"
       "system.categories" = "a,b,c,d"
      }
    }
    
    data "logicmonitor_device_group" "devicegroups" {
      filters {
        "property" = "name"
        "operator" = "~"
        "value"    = "Mesos"
      },
    }
  

Using The LogicMonitor Provider with other providers

The power of Terraform really shines when using resources in conjunction with each other. For example, the snippet below shows how to use the Terraform AWS Provider alongside the LogicMonitor Provider.


    data "logicmonitor_collectors" "collectors" {
      most_recent = true
    }
    resource "aws_instance" "instance1" {
      ami           = "${var.ami}"
      instance_type = "${var.instance_type}"
      ....
    }
    
    resource "logicmonitor_device" "newdevice" {
      ip_addr   = "${aws_instance.instance1.*.private_ip}"
      collector = "${data.logicmonitor_collectors.collectors.id}"
    }
  

The resource aws_instance will provision an AWS Instance with a configured ami and instance type. The aws_instance.instance1.*.private_ip is a computed attribute as it is created by AWS. This attribute is the generated output of a resource which then can be used as an input for other resources. Since Terraform orders resource creation based on the references created in the main.tf file, Terraform will not add the ec2 instance into LogicMonitor as a device until that instance is created and the IP is known.

Click*

The LogicMonitor Terraform Provider can be found on Terraform’s Github Account. It is being actively developed and we encourage and appreciate feedback and pull requests. You can start automating the creation of your cloud infrastructure with Terraform, and if you are a LogicMonitor customer and are tired of clicking on that “Add Device” button, or “Manage Host Groups” … or even “Submit”…

You only have 1 more click to go

Unless… you are not a customer… then you have two clicks

The LogicMonitor provider is under active development. We encourage you to contribute!

Subscribe to our blog

Get articles like this delivered straight to your inbox