> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://apidocs.polytomic.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://apidocs.polytomic.com/_mcp/server.

This example creates a Model Sync from BigQuery to LinkedIn Ads using
the Polytomic Terraform provider.

Prerequisites:

* Install the [Polytomic Terraform provider](https://registry.terraform.io/providers/polytomic/polytomic/latest).
* Set your Polytomic API key as an environment variable.

## 1. Configure the Polytomic provider

Configure the Polytomic provider in your Terraform script:

```terraform
terraform {
  required_providers {
    polytomic = {
      source  = "polytomic/polytomic"
      version = "~> 1.5"
    }
  }
}

provider "polytomic" {
    api_key = var.polytomic_api_key
}
```

Define `var.polytomic_api_key` in your variables file or pass it at apply time.

> 🤝 Partner keys
>
> If you are using a partner key, set `partner_key` on the provider and
> set `organization` on each resource that targets a specific organization.
>
> ```terraform
> provider "polytomic" {
>   partner_key = "<value from settings page>"
> }
> ```

## 2. Create a BigQuery Connection

Define a resource for the BigQuery connection:

```terraform
resource "polytomic_bigquery_connection" "user_data" {
  name = "BigQuery Users"

  configuration = {
    auth_method     = "service_account_key"
    service_account = file("bq.json")
  }
}
```

This example loads the service account key with the `file` function.
Any string value or variable works as well.

## 3. Create a LinkedIn Ads Connection

Create the LinkedIn Ads Connection through Polytomic's OAuth flow so
Polytomic can request the required LinkedIn Ads API permissions on your
behalf.
Use the admin console or [embedded authentication](/guides/embedding-authentication).

Once the Connection exists, reference it from Terraform with the
Polytomic LinkedIn Ads datasource. Pass the Connection ID from the admin
console:

```terraform
data "polytomic_linkedinads_connection" "destination" {
  id = "<linkedin-ads-connection-id>"
}
```

## 4. Create a source model for the LinkedIn Ads audience

Define a model that queries BigQuery:

```terraform
resource "polytomic_model" "audience_source" {
  name          = "LinkedIn Audience Source"
  connection_id = polytomic_bigquery_connection.user_data.id

  configuration = jsonencode({
    query = "SELECT first_name, last_name, email FROM acme_inc.publicusers"
  })

  fields = ["email", "first_name", "last_name"]
}
```

## 5. Create the Model Sync

Define a resource that syncs the model to a LinkedIn Ads audience:

```terraform
resource "polytomic_sync" "bigquery_to_linkedin_ads_audience" {
  name   = "Audience to LinkedIn Ads Sync"
  mode   = "create"
  active = true

  schedule = {
    frequency = "daily"
    hour      = "0"
    minute    = "0"
  }

  target = {
    connection_id = data.polytomic_linkedinads_connection.destination.id
    create = {
      "name" : var.linkedin_audience_name,
      "type" : "my_user_audience",
      "account" : var.linkedin_ad_account
    }
  }

  fields = [
    {
      source = {
        model_id = polytomic_model.audience_source.id
        field    = "email"
      }
      target = "user_email"
    },
    {
      source = {
        model_id = polytomic_model.audience_source.id
        field    = "first_name"
      }
      target = "firstName"
    },
    {
      source = {
        model_id = polytomic_model.audience_source.id
        field    = "last_name"
      }
      target = "lastName"
    },
  ]
}

```

## 6. Apply the configuration

Initialize Terraform and apply:

```shell
terraform init
terraform apply
```

Review the planned changes and confirm to proceed.

For more details, see the [Polytomic Terraform provider documentation](https://registry.terraform.io/providers/polytomic/polytomic/latest).