Skip to main content
Configure Terraform State storage
Last update:

Configure Terraform State storage

Terraform State is a file with an extension of .tfstateTerraform State is created after the first application of the Terraform State command. Terraform State is created after the first application of the command terraform apply and is updated whenever changes are made to the configuration files. Terraform compares the real infrastructure with its description in the Terraform State file. If Terraform finds discrepancies, it modifies the real infrastructure so that it matches the description in Terraform State again. Read more about Terraform State in the article Terraform State in HCP Terraform and section State HashiCorp documentation.

The Terraform State file is stored locally by default. To allow users to retrieve IDs of created resources and share infrastructure state, we recommend storing Terraform State remotely. For example, you can configure Terraform State storage in Selectel object storage.

Configure Terraform State storage in Selectel object storage

For your information

These instructions are for Terraform version 1.6.0 and higher.

  1. Create a container in the object store.

  2. Add a block to the configuration file backend:

    terraform {
    required_providers {
    selectel = {
    source = "selectel/selectel"
    version = "6.0.0"
    }
    openstack = {
    source = "terraform-provider-openstack/openstack"
    version = "2.1.0"
    }
    }

    backend "s3" {
    endpoints = { s3 = "https://s3.ru-1.storage.selcloud.ru" }
    key = "<file_name>.tfstate"
    region = "ru-1"
    skip_region_validation = true
    skip_credentials_validation = true
    skip_requesting_account_id = true
    skip_s3_checksum = true
    skip_metadata_api_check = true
    }
    }

    Specify <file_name> — the name of the Terraform state file in the object storage container.

  3. Create a file secret.backend.tfvars and add sensitive information to it that is required for authorization in the object store:

    bucket     = "<container_name>"
    access_key = "<access_key>"
    secret_key = "<secret_key>"

    Specify:

    • <container_name> — name of the container in the object store where the Terraform state file will be stored. You can look in control panels under Object StorageContainers;
    • <access_key> — the S3 key Access Key ID, which is issued to the user;
    • <secret_key> — Secret S3 Secret Access Key.
  4. Initialize the Terraform configuration changes:

    terraform init -backend-config=secret.backend.tfvars