DEV Community

Cover image for Saving Terraform State in S3
Sibelius Seraphini for Woovi

Posted on

Saving Terraform State in S3

Terraform is a solution to provision infrastructure using code (IaC).
Terraform stores the infrastructure generated in a state.
This state maps the real world and metadata to Terraform.
This ensures that you won't duplicate or recreate existing resources.

Storing Terraform State in an S3 bucket

You can use S3 as the backend of the state like this

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 4.0"
    }
  }
  required_version = ">= 1.0"

  backend "s3" {
    bucket                  = "terraform-state-dev"
    key                     = "my-key"
    region                  = "us-east-1"
    shared_credentials_file = "~/.aws/credentials"
  }
}
Enter fullscreen mode Exit fullscreen mode

You can have a single key to store all your Terraform state, or you can have one key for each Terraform state.

You can start your Terraform using terraform init

After any terraform apply or terraform apply the state in your S3 will be updated.

At Woovi, we decided to have one key per Terraform provision.

How to migrate existing local state?

You can use this command to migrate existing local or another backend state to your S3 bucket

terraform init -migrate-state
Enter fullscreen mode Exit fullscreen mode

In Resume

If you lose your Terraform state, this can cause significant trouble.
Keeping them in an S3 bucket can make it shareable across your team.


Woovi
Woovi is a fintech platform revolutionizing how businesses and developers handle payments in Brazil. Built with a developer-first mindset, Woovi simplifies integration with instant payment methods like Pix, enabling companies to receive payments seamlessly and automate financial workflows.

If you want to work with us, we are hiring!

Top comments (0)