Home

Awesome

dbt Cloud Terraforming

dbtcloud-terraforming has been created to be used along with the dbt Cloud Terraform provider maintained by dbt Labs.

It can be used to generate the relevant Terraform configuration files based on existing dbt Cloud configuration.

dbtcloud-terraforming is an application that allows dbt Cloud users
to be able to adopt Terraform by giving them a feasible way to get
all of their existing dbt Cloud configuration into Terraform.

Usage:
  dbtcloud-terraforming [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  generate    Fetch resources from the dbt Cloud API and generate the respective Terraform stanzas
  help        Help about any command
  import      Output `terraform import` compatible commands and/or import blocks (require terraform >= 1.5) in order to import resources into state
  version     Print the version number of dbtcloud-terraforming

Flags:
  -a, --account string                  Use specific account ID for commands. [env var: DBT_CLOUD_ACCOUNT_ID]
  -h, --help                            help for dbtcloud-terraforming
      --host-url string                 Host URL to use to query the API, includes the /api part. [env var: DBT_CLOUD_HOST_URL]
      --linked-resource-types strings   List of resource types to make dependencies links to instead of using IDs. Can be set to 'all' for linking all resources
      --modern-import-block             Whether to generate HCL import blocks for generated resources instead of terraform import compatible CLI commands. This is only compatible with Terraform 1.5+
  -p, --projects ints                   Project IDs to limit the import for. Imports all projects if not set. [env var: DBT_CLOUD_PROJECTS]
      --resource-types strings          List of resource types you wish to generate
      --terraform-binary-path string    Path to an existing Terraform binary (otherwise, one will be downloaded)
      --terraform-install-path string   Path to an initialized Terraform working directory (default ".")
  -t, --token string                    API Token. [env var: DBT_CLOUD_TOKEN]
  -v, --verbose                         Specify verbose output (same as setting log level to debug)

This tool can be used to load existing dbt Cloud configuration into Terraform. Currently the following resources are supported:

ResourceResource ScopeGenerate SupportedImport SupportedRequires manual setup
dbtcloud_bigquery_connectionProject🔒
dbtcloud_bigquery_credentialProject
dbtcloud_connectionProject🔒*
dbtcloud_databricks_credentialProject✅*🔒
dbtcloud_environmentProject
dbtcloud_environment_variableProject🔒*
dbtcloud_environment_variable_job_overrideProject
dbtcloud_extended_attributes(*)Project
dbtcloud_fabric_connectionProject
dbtcloud_fabric_credentialProject🔒
dbtcloud_global_connectionAccount🔒*
dbtcloud_groupAccount
dbtcloud_jobProject
dbtcloud_license_mapAccount
dbtcloud_notificationAccount
dbtcloud_postgres_credentialProject🔒*
dbtcloud_projectProject
dbtcloud_project_artefacts (deprecated)Project
dbtcloud_project_connection (deprecated)Project
dbtcloud_project_repositoryProject
dbtcloud_repositoryProject
dbtcloud_service_tokenAccount
dbtcloud_snowflake_credentialProject🔒
dbtcloud_user_groupsAccount
dbtcloud_webhookAccount

Notes:

How to use the tool

Installation

All OSes

Download the executable for your platform from the GitHUb releases page and extract it. You can then add it to your PATH and run it with dbtcloud-terraforming or run it based on its location (e.g. ./dbtcloud-terraforming).

To update to the latest version, you can head back to the release page and download and extract the executable again.

Alternatively, you can use eget to more easily download the relevant executable.

MacOS and Linux

The CLI can be installed with brew, running brew install dbt-labs/dbt-cli/dbtcloud-terraforming.

To update to the latest version, you can run brew upgrade dbt-labs/dbt-cli/dbtcloud-terraforming.

Connecting to dbt Cloud

To connect to dbt Cloud, you need to provide an API token and a dbt Cloud Account ID. If your account is not hosted on cloud.getdbt.com you will also need to provide the relevant API endpoint.

Example:

export DBT_CLOUD_TOKEN=<token>
export DBT_CLOUD_ACCOUNT_ID=123
export DBT_CLOUD_HOST_URL="https://emea.dbt.com/api"

or for Powershell

$Env:DBT_CLOUD_TOKEN = '<token>'
$Env:DBT_CLOUD_ACCOUNT_ID = 123
$Env:DBT_CLOUD_HOST_URL = 'https://emea.dbt.com/api'

Executing the tool

Pre-requisite

By default, the tool requires a file called main.tf with information about the dbt Cloud Terraform provider, in the current directory:

terraform {
  required_providers {
    dbtcloud = {
      source = "dbt-labs/dbtcloud"
    }
  }
}

If you already have a file defining the provider, you can point dbtcloud-terraforming to it via the flag --terraform-install-path

Running the different commands

Install the tool and run commands like below:

To generate the config

dbtcloud-terraforming generate --resource-types dbtcloud_project,dbtcloud_environment,dbtcloud_job --linked-resource-types dbtcloud_project,dbtcloud_environment

To generate the import blocks

dbtcloud-terraforming import --resource-types dbtcloud_project,dbtcloud_environment,dbtcloud_job --modern-import-block

Once both of the outputs are generated, you can copy paste them in a terraform file having the dbtcloud provider already set up and you can run a terraform plan. You should see that all the resources are going to be imported and that no change will be triggered.

The different resource_types that can be used are the ones from the table above. They are a subset of the resources available in the dbt Cloud Terraform provider. Generating and importing multiple resource types at once is possible by separating them with ,

Selecting specific projects

By default, the tool loads all projects but we can restrict the projects to focus on by selecting --projects 123,456,789 with 123, 456 and 789 being the projects we want to load in Terraform

Linking resources in the configuration

When generating the configuration, if --linked-resource-types is not set for any resource, all the existing IDs (Project ID, Environment ID etc...) will be stored in the config as IDs.

This is OK to load the configuration in Terraform, but it also means that if those projects change on the dbt Cloud side, Terraform will start failing. Additionally, if a project is getting deleted for example, Terraform won't understand the impact to other objects, like environments, and you will start seeing errors.

Setting up --linked-resource-types with the resource types you want to link will "link" resources together, like in a typical Terraform configuration.

With this example:

dbtcloud-terraforming generate --resource-types dbtcloud_project,dbtcloud_environment,dbtcloud_job --linked-resource-types dbtcloud_project,dbtcloud_environment

This can be especially useful if you want to replicate an existing project. To do so, you can generate all the config without importing it. You could change the name of a project, and after running a terraform apply all the objects will be newly created, replicating your existing config in another project.

Contributing

Currently, the best way to contribute is to raise bugs/feature requests as GitHub issues.

Credits

A big part of this tool has been inspired from the Cloudflare library cf-terraforming