---
title: "Configure Terraform State storage"
sidebar_label: "Configure Terraform State storage"
sidebar_position: 3
description: "What Terraform State is, options for storing Terraform State, how to configure Terraform State storage in Selectel S3"
---

import Formbricks from '@theme/MDXComponents/Formbricks'

# Configure Terraform State storage

Terraform State — is a file with the `.tfstate` extension that stores current information about the infrastructure state and the configurations in use. Terraform State is created after the first time the `terraform apply` command is run and is updated every time changes are made to configuration files. Terraform compares the actual infrastructure with its description in the Terraform State file. If Terraform detects discrepancies, it modifies the actual infrastructure to match the description in the Terraform State again. Learn more about Terraform State in the [Terraform State in HCP Terraform](https://developer.hashicorp.com/terraform/cloud-docs/workspaces/state) article and the [State](https://developer.hashicorp.com/terraform/language/state) section of the HashiCorp documentation.

By default, the Terraform State file is stored locally. To allow users to retrieve the IDs of created resources and share the infrastructure state, we recommend storing the Terraform State remotely. For example, you can [configure Terraform State storage in Selectel S3](#configure-state-storage-in-selectel-s3).

## Configure Terraform State storage in Selectel S3 \{#configure-state-storage-in-selectel-s3}

:::info

This instruction is for Terraform version 1.6.0 and later.

:::

1. [Create a bucket in S3](/s3/buckets/create-bucket/).

2. Add the `backend:` block to the configuration file:

   ```hcl
   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 sensitive information required for S3 authorization to it:

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

   Specify:

   * `<bucket_name>` — the name of the S3 bucket where the Terraform State file will be stored. You can find it in the [Control panel](https://my.selectel.ru/storage/containers) in the **S3** → **Containers section**;
   * `<access_key>` — the Access Key ID identifier that is [issued to the user](/access-control/manage/edit-user-data-or-role/#manage-s3-keys);
   * `<secret_key>` — the S3 Secret Access Key.

4. Initialize the Terraform configuration changes:

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

<Formbricks />
