# Configuration

### Provider Block

The TeraSwitch provider requires configuration before you can manage resources. Add the following to your Terraform configuration:

```hcl
terraform {
  required_providers {
    teraswitch = {
      source  = "TeraSwitch/teraswitch"
      version = "~> 0.0.7"
    }
  }
}

provider "teraswitch" {
  api_key    = var.teraswitch_api_key
  project_id = var.teraswitch_project_id
}
```

### Configuration Arguments

| Argument     | Type   | Required | Description                                    |
| ------------ | ------ | -------- | ---------------------------------------------- |
| `api_key`    | string | Yes      | Your TeraSwitch API key for authentication     |
| `project_id` | number | Yes      | The project ID where resources will be created |

### Authentication Methods

#### Environment Variables (Recommended)

Set environment variables to keep credentials out of your configuration files:

```bash
export TERASWITCH_API_KEY="your-api-key"
export TERASWITCH_PROJECT_ID="12345"
```

With environment variables set, you can simplify your provider block:

```hcl
provider "teraswitch" {
  # Credentials loaded from environment
}
```

#### Terraform Variables

Use input variables for flexibility across environments:

```hcl
variable "teraswitch_api_key" {
  description = "TeraSwitch API key"
  type        = string
  sensitive   = true
}

variable "teraswitch_project_id" {
  description = "TeraSwitch project ID"
  type        = number
}

provider "teraswitch" {
  api_key    = var.teraswitch_api_key
  project_id = var.teraswitch_project_id
}
```

Pass values via a `.tfvars` file or command line:

```bash
terraform apply -var="teraswitch_api_key=xxx" -var="teraswitch_project_id=123"
```

{% hint style="danger" %}
Never commit API keys to version control. Add `*.tfvars` and any files containing credentials to your `.gitignore`.
{% endhint %}

#### Secrets Managers

For production environments, integrate with secrets management solutions:

**HashiCorp Vault:**

```hcl
data "vault_generic_secret" "teraswitch" {
  path = "secret/teraswitch"
}

provider "teraswitch" {
  api_key    = data.vault_generic_secret.teraswitch.data["api_key"]
  project_id = data.vault_generic_secret.teraswitch.data["project_id"]
}
```

**AWS Secrets Manager:**

```hcl
data "aws_secretsmanager_secret_version" "teraswitch" {
  secret_id = "teraswitch-credentials"
}

locals {
  tsw_creds = jsondecode(data.aws_secretsmanager_secret_version.teraswitch.secret_string)
}

provider "teraswitch" {
  api_key    = local.tsw_creds.api_key
  project_id = local.tsw_creds.project_id
}
```

### Multiple Provider Configurations

Use provider aliases to manage resources across multiple projects:

```hcl
provider "teraswitch" {
  alias      = "production"
  api_key    = var.teraswitch_api_key
  project_id = var.production_project_id
}

provider "teraswitch" {
  alias      = "staging"
  api_key    = var.teraswitch_api_key
  project_id = var.staging_project_id
}

resource "teraswitch_cloud_compute" "prod_web" {
  provider = teraswitch.production
  # ... configuration
}

resource "teraswitch_cloud_compute" "staging_web" {
  provider = teraswitch.staging
  # ... configuration
}
```

### Obtaining Credentials

#### API Key

1. Log in to the [TeraSwitch Console](https://console.tsw.io)
2. Navigate to **Account** > **API Tokens**
3. Click **Create Token**
4. Copy the generated token immediately (it won't be shown again)

See [API Tokens](/account/api-tokens.md) for detailed instructions.

#### Project ID

Your project ID is visible in the console URL when viewing a project:

```
https://console.tsw.io/projects/12345/...
                              ^^^^^
                              Project ID
```

You can also find it on the project settings page.

### Best Practices

1. **Use environment variables** for local development
2. **Use secrets managers** for production and CI/CD
3. **Never commit credentials** to version control
4. **Rotate API keys** periodically
5. **Use separate API keys** for different environments (dev, staging, prod)
6. **Limit API key permissions** where possible

{% hint style="info" %}
Consider using Terraform workspaces or separate state files for different environments to prevent accidental changes to production resources.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.teraswitch.com/integrations/terraform/configuration.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
