> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-poc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate Terraform with Tiger Cloud

> Provision and manage your infrastructure as code with predictable deployments

export const VPC = 'VPC';

export const SELF_LONG_CAP = 'Self-hosted TimescaleDB';

export const PG = 'Postgres';

export const COMPANY = 'Tiger Data ';

export const CLOUD_LONG = 'Tiger Cloud';

export const SERVICE_LONG = 'Tiger Cloud service';

export const SELF_LONG = 'self-hosted TimescaleDB';

[Terraform][terraform] is an infrastructure-as-code tool that enables you to safely and predictably provision and manage infrastructure.

This page explains how to configure Terraform to manage your {SERVICE_LONG} or {SELF_LONG}.

## Prerequisites

To follow the steps on this page:

* Create a target [{SERVICE_LONG}][create-service] with Real-time analytics enabled.<p />

  You need [your connection details][connection-info]. This procedure also
  works for [{SELF_LONG}][enable-timescaledb].

[create-service]: /deploy-and-operate/tiger-cloud/get-started/create-services

[enable-timescaledb]: /deploy-and-operate/self-hosted/install-and-update/install-self-hosted

[connection-info]: /integrations/find-connection-details

* [Download and install][terraform-install] Terraform.

## Configure Terraform

Configure Terraform based on your deployment type:

<Tabs>
  <Tab title={CLOUD_LONG}>
    You use the [{COMPANY} Terraform provider][terraform-provider] to manage {SERVICE_LONG}s:

    1. **[Generate client credentials][generate-credentials]**

    2. **Configure {COMPANY} Terraform provider**

       1. Create a `main.tf` configuration file with at least the following content. Change `x.y.z` to the [latest version][terraform-provider] of the provider.

          ```hcl theme={"dark"}
          terraform {
            required_providers {
              timescale = {
                source  = "timescale/timescale"
                version = "x.y.z"
              }
            }
          }

          # Authenticate using client credentials generated in Tiger Cloud Console.
          # When required, these credentials will change to a short-lived JWT to do the calls.
          provider "timescale" {
           project_id = var.ts_project_id
           access_key = var.ts_access_key
           secret_key = var.ts_secret_key
          }

          variable "ts_project_id" {
           type = string
          }

          variable "ts_access_key" {
           type = string
          }

          variable "ts_secret_key" {
           type = string
          }
          ```

       2. Create a `terraform.tfvars` file in the same directory as your `main.tf` to pass in the variable values:

          ```hcl theme={"dark"}
          export TF_VAR_ts_project_id="<your-timescale-project-id>"
          export TF_VAR_ts_access_key="<your-timescale-access-key>"
          export TF_VAR_ts_secret_key="<your-timescale-secret-key>"
          ```

    3. **Add your resources**

       Add your {SERVICE_LONG}s or {VPC} connections to the `main.tf` configuration file. For example:

       ```hcl theme={"dark"}
       resource "timescale_service" "test" {
         name              = "test-service"
         milli_cpu         = 500
         memory_gb         = 2
         region_code       = "us-east-1"
         enable_ha_replica = false

         timeouts = {
           create = "30m"
         }
       }

       resource "timescale_vpc" "vpc" {
         cidr         = "10.10.0.0/16"
         name         = "test-vpc"
         region_code  = "us-east-1"
       }
       ```

    You can now manage your resources with Terraform. See more about [available resources][terraform-resources] and [data sources][terraform-data-sources].
  </Tab>

  <Tab title={SELF_LONG_CAP}>
    You use the [`cyrilgdn/postgresql`][pg-provider] {PG} provider to connect to your {SELF_LONG} instance.

    Create a `main.tf` configuration file with the following content, using your [connection details][connection-info]:

    ```hcl theme={"dark"}
       terraform {
        required_providers {
         postgresql = {
          source  = "cyrilgdn/postgresql"
          version = ">= 1.15.0"
         }
        }
       }

       provider "postgresql" {
        host            = "your-timescaledb-host"
        port            = "your-timescaledb-port"
        database        = "your-database-name"
        username        = "your-username"
        password        = "your-password"
        sslmode         = "require" # Or "disable" if SSL isn't enabled
       }
    ```

    You can now manage your database with Terraform.
  </Tab>
</Tabs>

[connection-info]: /integrations/find-connection-details

[generate-credentials]: /integrations/find-connection-details#create-client-credentials

[pg-provider]: https://registry.terraform.io/providers/cyrilgdn/postgresql/latest

[services-portal]: https://console.cloud.timescale.com/dashboard/services

[terraform]: https://developer.hashicorp.com/terraform

[terraform-data-sources]: https://registry.terraform.io/providers/timescale/timescale/latest/docs/data-sources/products

[terraform-install]: https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

[terraform-provider]: https://registry.terraform.io/providers/timescale/timescale/latest/docs

[terraform-resources]: https://registry.terraform.io/providers/timescale/timescale/latest/docs/resources/peering_connection
