Skip to main content

Configure Terraform State storage

Last update:

Terraform State is a file with the .tfstate extension that stores up-to-date information about the state of the infrastructure and the configurations in use.Terraform State is created when the terraform apply command is first applied and is updated each time changes are made to the configuration files. Terraform compares the actual 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. For more information about Terraform State, see the Terraform State in HCP Terraform article and the State section of  the HashiCorp documentation.

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

Configure Terraform State storage in S3 Selectel

For your information

These instructions are for Terraform version 1.6.0 and higher.

  1. Create a bucket in S3.

  2. In the configuration file, add a backend block:

    terraform {
    required_providers {
    selectel = {
    source = "selectel/selectel"
    version = "~> 6.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 S3 bucket.

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

    bucket     = "<bucket_name>"
    access_key = "<access_key>"
    secret_key = "<secret_key>"

    Specify:

    • <bucket_name> — name of the bucket in S3 where the Terraform state file will be stored. You can look in control panel under S3Containers;
    • <access_key> — S3 key Access Key ID, which has been issued to the user;
    • <secret_key> — secret S3 Secret Access Key Secret Access Key.
  4. Initialize the Terraform configuration changes:

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