Share on Twitter
Share on Facebook
Share on HackerNews

Introducing Terraform for Sentry

*The Sentry Terraform Provider is an open-source project built and maintained by a community developer and officially sponsored by Sentry. You can find more information on the Terraform Registry and in this GitHub repository. *

What is Terraform?

While some repetitive tasks serve a purpose or can be downright enjoyable - i.e my Sunday gardening routine - manually updating the same tool settings across multiple projects one by one does not belong in this category.

That’s what intrigued me about Terraform. Terraform is a tool that allows developers to automate infrastructure creation through code in a declarative fashion. In other words, Terraform looks through a platform’s configuration file and checks for discrepancies between what’s defined there and the reality of what infrastructure exists. It then automates the API calls needed to achieve the desired configuration. Terraform relies on Providers to interact with remote platforms and services like Sentry.

Fueled by my curiosity and repulsion towards monotonous, repetitive tasks, I teamed up with Sentry to build a Terraform Provider to help developers, SREs, and engineering leaders save time provisioning their Sentry projects while giving the option to work from an interface they may already be familiar with.

Why I built the Sentry Provider

Having used Sentry at an enterprise-level company before, I’ve seen the complexities of managing infrastructure for a multi-product, multi-platform at a large scale. Engineering teams often manage 20+ Sentry projects at once, so having a way to provision and maintain infrastructure parameters with code can make it much easier to track, control, and maintain the state of each project.

That’s why the Sentry Terraform Provider I built supports a variety of Sentry-specific parameters, including: Organization, Teams, Projects, Client Keys, Dashboards, Issue Alerts, and Metric Alerts. This means you can automate error and performance alert settings across multiple projects (for easy detection of error spikes or latency issues), configure dashboards, and verify and enforce naming conventions – all from your command line.

In addition to contributing back to the community, I found building and maintaining these two open-source projects has been a great way to learn how open-source works. One of the most rewarding outcomes is the sense of community and relationships that I have built with other developers and companies trying to solve similar issues.

How to set it up

Requiring the Provider The first thing you will need to do is to include the Sentry Terraform Provider in a “required_providers” block:

terraform {
  required_providers {
    sentry = {
      source = "jianyuan/sentry"
      version = "~> 0.9"
    }
  }
}

This informs Terraform of the location of the Provider in the Terraform Registry and the version to use in your project.

Configuring the Provider Next, you will need to configure the Provider by providing an authentication token. You can create an authentication token by creating an internal integration.

provider "sentry" {
  token = "your-authentication-token"
}

It’s best practice not to store the authentication token in plain text. As an alternative, the Provider can source the authentication token from the SENTRY_AUTH_TOKEN environment variable. If you choose to do this, you can omit the token variable from the configuration block above.

Then, run terraform init to install and use the Provider.

Data source: Organization In this example, we will be using an existing organization to house all of the resources that we will be creating. Take note of your organization “slug”, which is the unique ID used to identify your organization. Use it to configure a new organization data source block:

data "sentry_organization" "demo" {
  slug = "my-sentry-organization"
}

We will now create resources.

Creating a team The first resource we will create is a Sentry Team. The required fields are:

  • organization - organization slug
  • name - the name of the team

You can pull the organization from the data source we defined in the previous section.

resource "sentry_team" "my_team" {
  organization = data.sentry_organization.demo.id

  name = "my-team"
}

Creating a project Next, we will create a Sentry Project. The required fields are:

  • organization - organization slug
  • team - team slug
  • name - the name of the project
  • platform - the platform of the project

Like before, you can pull the organization and team from the resource we defined in the previous section.

resource "sentry_project" "my_project" {
  organization = sentry_team.my_team.organization
  team         = sentry_team.my_team.slug

  name     = "my-project"
  platform = "go"
}

Now that we have defined two resources, we will create them. Run terraform apply and inspect the changes. If you are happy with the proposed changes, confirm and Terraform will go ahead and create the new resources on Sentry.

If you log in to Sentry, you should see the new team and project.

Data source: Client Key We will automatically create a default Client Key for every new project. You can use the Key data source to retrieve it:

data "sentry_key" "default" {
  organization = sentry_project.my_project.organization
  project      = sentry_project.my_project.slug

  first = true
}

The first = true attribute is used to select the Client First Key for the project.

A common use case is to configure one of Sentry’s SDKs. You can retrieve the DSN value from the data source. For example, you can expose the DSN as an output of a Terraform module, which you can then refer to elsewhere in your Terraform project:

output "dsn_public" {
  value = data.sentry_key.default.dsn_public
}

Conclusion

The Sentry Terraform Provider enables teams to bulk update project-specific configurations, automatically track changes, and create/maintain resources like alerts and dashboards, at scale. By being able to programmatically manage repetitive tasks, your team can spend less time configuring and more time building. For further set up instructions and to see a sample configuration, refer to the documentation and this demo.

Your code is broken. Let's Fix it.
Get Started

More from the Sentry blog

ChangelogDashboardsDiscoverDogfooding ChroniclesEcosystemError MonitoringEventsGuest PostsMobileMoonlightingOpen SourcePerformance MonitoringRelease HealthSDK UpdatesSentry

Do you like corporate newsletters?

Neither do we. Sign up anyway.

© 2022 • Sentry is a registered Trademark
of Functional Software, Inc.