Terraform
Estuary offers a Terraform provider so you can create and manage data flows using standardized infra-as-code configuration.
Estuary's Terraform provider is currently in beta. We encourage you to work with our support team when using this feature, and welcome your feedback as we finalize its capabilities and design.
While in beta, connector support is limited. If you'd like access to additional connectors with Terraform, reach out to request them.
With Terraform, you can:
- Create, update, or disable Estuary captures and materializations
- Manage bindings
- Trigger backfills
- Configure multiple environments
Further information on working with Terraform is available in Estuary's Terraform registry docs.
If you would like to manage your Estuary resources using infra-as-code but do
not require Terraform, you may also use flowctl in automated contexts.
Setup
Prerequisites
To use Estuary's Terraform provider, you will need:
- Terraform (version 1.11 or higher)
flowctlinstalled, as the Terraform provider usesflowctlto publish changes to your Estuary account- An Estuary tenant with a valid refresh token
You should also be familiar with Estuary's core concepts, such as captures, materializations, and their bindings. These are the resources you will be able to create and manage with Terraform.
Define the provider
A basic Terraform file will include blocks to define Terraform and provider behavior. For the Estuary Terraform provider, you will need to specify the required Terraform version and Estuary provider details.
terraform {
required_version = ">= 1.11"
required_providers {
estuary = {
source = "estuary/estuary"
version = "~> 0.1.2"
}
}
}
# Estuary provider, configured to use the acmeCo/ tenant.
provider "estuary" {
tenant = "acmeCo/"
}
To authenticate, set a FLOW_AUTH_TOKEN environment variable or a token
attribute in the provider block. The value of either should be an Estuary
refresh token.
Define Estuary resources
You can define captures, materializations, and the bindings associated with each in your Terraform files.
Connector definition attributes will follow a similar pattern, including at least:
- A name: This will become part of the full task name, such as in
your-tenant/defined-task-name/resource-type - A config block: Includes all the attributes you need to connect to your source or destination and can set optional attributes to customize the integration
- A bindings block: Define resources for associated data collections
Additional optional blocks are available to manage features like auto-discover, trigger backfills, or disable connectors.
Your Terraform configurations will look similar to the following examples.
- Simple Postgres capture
- Snowflake materialization
- Read-only RDS Postgres capture
# Mark passwords or other credentials as sensitive
variable "postgres_password" {
type = string
sensitive = true
}
resource "estuary_source_postgres" "postgres" {
name = "terraform-postgres"
config = {
address = "host:port"
user = "estuary"
database = "postgres"
history_mode = false
credentials = {
auth_type = "UserPassword"
password = var.postgres_password
}
}
# Add tables you want to capture as bindings
bindings = [
{
resource = {
namespace = "public"
stream = "sales"
}
},
]
}
Includes optional configuration for a custom sync schedule.
variable "snowflake_private_key" {
type = string
sensitive = true
}
resource "estuary_materialization_snowflake" "warehouse" {
name = "warehouse"
config = {
host = "orgname-accountname.snowflakecomputing.com"
database = "ESTUARY_DB"
schema = "ESTUARY_SCHEMA"
warehouse = "ESTUARY_WH"
role = "ESTUARY_ROLE"
timestamp_type = "TIMESTAMP_LTZ"
credentials = {
auth_type = "jwt"
user = "ESTUARY_USER"
private_key = var.snowflake_private_key
}
# A sync schedule will regulate how often data loads into Snowflake
sync_schedule = {
sync_frequency = "15m"
timezone = "America/New_York"
fast_sync_start_time = "09:00"
fast_sync_stop_time = "17:00"
fast_sync_enabled_days = "M-F"
}
}
bindings = [
{
source = "acmeCo/mongodb/shop/users"
resource = { table = "users" }
},
{
source = "acmeCo/mongodb/shop/orders"
resource = { table = "orders" }
},
]
}
Includes optional configuration for auto-discover.
variable "postgres_password" {
type = string
sensitive = true
}
resource "estuary_source_rds_postgres" "postgres" {
name = "prod/rds"
auto_discover = {
add_new_bindings = true
evolve_incompatible_collections = true
}
config = {
address = "123456789.us-east-1.rds.amazonaws.com:5432"
user = "estuary"
database = "postgres"
history_mode = false
credentials = {
auth_type = "UserPassword"
password = var.postgres_password
}
# Read-only mode is considered an advanced configuration: we
# recommend keeping the Estuary reference docs handy to ensure
# your Postgres instance works well with this configuration.
advanced = {
read_only_capture = true
}
}
bindings = [
{
resource = {
namespace = "public"
stream = "sales"
}
},
]
}
Use Estuary's Terraform registry docs as a reference to configure your resources in conjunction with Estuary's connector reference docs. The Terraform docs will provide information on supported resources and attribute configuration while the connector docs will provide best practices and details on working with the connector.
Working with sensitive credentials
Most configurations include some kind of sensitive credential to access your source or destination system. You can handle these fields as sensitive variables in Terraform.
For example, you may define a sensitive variable for a password field:
variable "postgres_password" {
type = string
sensitive = true
}
You can then supply the value through an environment variable using the pattern
TF_VAR_<variable_name>. In this case, it would be TF_VAR_postgres_password.
You may also set values in a .tfvars file that you apply to your Terraform
plan. For example, a secret.tfvars file may contain:
postgres_password = "<YOUR-SECURE-PASSWORD>"
And you can add this file to your plan with
terraform plan -out tfplan -var-file="secret.tfvars".
Learn more about handling sensitive variables in Terraform's documentation.
Add existing resources to Terraform
Once you have defined the resources that you want to manage with Terraform, you can either create them as new resources in Estuary or attach the Terraform definitions to existing resources with imports.
If you're taking over managing an existing resource in Estuary, use import blocks to tie your Terraform definitions with an existing capture or materialization task name. For example:
import {
id = "acmeCo/task-name/source-postgres"
to = estuary_source_postgres.postgres
}
You may also import resources via CLI command.
If you do not tie defined resources to existing tasks in this way, Terraform will create new tasks for your resources the first time you run your Terraform plan.
Terraform commands
There are several common commands you will use when working with Terraform.
See Terraform's CLI documentation for more information on available commands and options.
Initialization
To initialize a working directory for your Terraform config files for the first time, run:
terraform init
This will create a lock file with your provider versions and ensure the directory is set up with Terraform.
Create plans
To create a Terraform plan based on your configuration file(s), run:
terraform plan -out tfplan
You will be able to review plan actions, such as resources the plan will create, update, or destroy.
Apply plans
Once you have reviewed your plan, you can apply changes with:
terraform apply tfplan
The Terraform provider will connect with your Estuary account to create, modify, or remove connector tasks. This will take a minute or so to complete.
Import existing resources
If you need to import existing resources, run:
terraform import estuary_source_postgres.postgres acmeCo/task-name/source-postgres
Where the initial import argument (estuary_source_postgres.postgres) is your
Terraform resource name and the second argument (acmeCo/task-name/source-postgres)
is the full Estuary task name to import.
You may also manage imports with import blocks to retain a versioned history of your imports.