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 article and the 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 Servercore S3.
Configure Terraform State storage in Servercore S3
This instruction is for Terraform version 1.6.0 and later.
-
Add the
backend:block to the configuration file:terraform {required_providers {servercore = {source = "terraform.servercore.com/servercore/servercore"version = "~> 6.0"}openstack = {source = "terraform-provider-openstack/openstack"version = "2.1.0"}}backend "s3" {endpoints = { s3 = "https://s3.uz-2.srvstorage.uz" }key = "<file_name>.tfstate"region = "uz-2"skip_region_validation = trueskip_credentials_validation = trueskip_requesting_account_id = trueskip_s3_checksum = trueskip_metadata_api_check = true}}Specify
<file_name>— the name of the Terraform State file in the S3 bucket. -
Create a
secret.backend.tfvarsfile and add sensitive information required for S3 authorization to it: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 in the S3 → Containers section;<access_key>— the S3 Access Key ID that was issued to the user;<secret_key>— the S3 Secret Access Key.
-
Initialize the Terraform configuration changes:
terraform init -backend-config=secret.backend.tfvars