Skip to main content
Use Terraform variables and workspaces to maintain identical resource configurations across environments such as preproduction and production. The configuration below uses provider v0: permanent_api_token and resource types such as gcore_cdn_resource. Provider v2 authenticates with api_key and uses different resource type names. The provider overview shows both provider blocks.
Configure an API token and initialize Terraform before proceeding.

Step 1. Define variables

Create a variables.tf file to declare the parameters that differ between environments. The following variables are common across all products:
Add the resource-specific variable for the target product:

Step 2. Configure environment variables

Create a separate .tfvars file for each environment. The common fields are identical across products:
Add the resource-specific value for each environment:

Step 3. Write the resource configuration

Add the following to main.tf. The provider block is identical for all products:
Add the resource block for the target product:

Step 4. Initialize Terraform

From the project directory, run:
This downloads the Gcore provider and prepares the working directory.

Step 5. Use Terraform workspaces

Terraform workspaces manage multiple environments from the same configuration — each workspace maintains an independent state file, so changes in preprod do not affect production. Create workspaces for preproduction and production:

Step 6. Apply the configuration

Switch to the preproduction workspace and apply the configuration:
Then switch to the production workspace and apply the configuration there as well:
The same configuration now runs in both environments, with each workspace tracking its own state independently.