AWS, DevOps, Terraform, S3

2026-10-15

Building a Terraform S3 Remote State Backend on AWS

When you first use Terraform, it usually saves your infrastructure state locally in a file called

As terraform.tfstate

Think of this file as Terraform’s record of the resources it manages, including their AWS IDs and other attributes. It can also contain sensitive values, so keep it out of Git.

Local state works for learning and small experiments. But once teammates or CI pipelines manage the same infrastructure, everyone needs access to the same state.

For this field notes, we’ll move the state to Amazon S3, so first:

You’ll need:

Use your normal AWS authentication method, such as an AWS profile. Don’t put credentials directly in Terraform files. NEVER. EVER.

Why use remote state at all?

An S3 backend gives us the following benefits:

Versioning and locking need to be enabled, so we’ll configure both.

Terraform recommends using a remote backend with locking instead of committing state to source control. Reference: Terraform state documentation

The Bootstrap problem

There is a small chicken-and-egg problem here :

Terraform needs the S3 bucket to exist before it can store state there.

We’ll handle that with two separate Terraform directories:

Each directory is a separate Terraform configuration with its own state. We run bootstrap/ first, then infra/.

Keep the bootstrap state safe too, it still tracks the bucket we just created.

What should the state bucket protect exactly?

The bucket should have:

Note: force_destroy = false stops Terraform from automatically emptying the bucket during deletion

AWS recommends using Block Public Access and versioning to protect S3 data and recover previous versions, just incase. References: AWS S3 security guidance

Create and Protect! (The bucket):

For this guide, we will need the following resources:

Create bootstrap/state.tf. We’ll start with the bucket itself:


resource "aws_s3_bucket" "terraform_state" {

  bucket        = var.state_bucket_name

  force_destroy = false



  tags = {

    Project     = var.project_name

    Environment = "bootstrap"

    ManagedBy   = "Terraform"

    Purpose     = "Terraform remote state"

  }

}

force_destroy = false prevents Terraform from automatically emptying the bucket during deletion. It does not prevent deletion of an already-empty bucket.

Add the following resources to the same file.

Enable versioning so previous state versions remain available for recovery:


resource "aws_s3_bucket_versioning" "terraform_state" {

  bucket = aws_s3_bucket.terraform_state.id



  versioning_configuration {

    status = "Enabled"

  }

}

Versioning protects the state history; it doesn’t automatically undo changes to your AWS infrastructure.

Configure encryption for stored objects:


resource "aws_s3_bucket_server_side_encryption_configuration" "terraform_state" {

  bucket = aws_s3_bucket.terraform_state.id



  rule {

    apply_server_side_encryption_by_default {

      sse_algorithm = "AES256"

    }

  }

}

This uses S3-managed encryption keys, so we don’t need to create a separate KMS key.

Block public access:


resource "aws_s3_bucket_public_access_block" "terraform_state" {

  bucket = aws_s3_bucket.terraform_state.id



  block_public_acls       = true

  block_public_policy     = true

  ignore_public_acls      = true

  restrict_public_buckets = true

}

Disable ACLs with bucket-owner-enforced ownership:


resource "aws_s3_bucket_ownership_controls" "terraform_state" {

  bucket = aws_s3_bucket.terraform_state.id



  rule {

    object_ownership = "BucketOwnerEnforced"

  }

}

This keeps access management policy-based rather than using separate object ACLs.

Set a retention period

Versioning keeps older copies of the state. For this example, we’ll expire versions 365 days after they become noncurrent:


resource "aws_s3_bucket_lifecycle_configuration" "terraform_state" {

  bucket = aws_s3_bucket.terraform_state.id



  depends_on = [aws_s3_bucket_versioning.terraform_state]



  rule {

    id     = "state-retention"

    status = "Enabled"



    filter {}



    noncurrent_version_expiration {

      noncurrent_days = 365

    }



    abort_incomplete_multipart_upload {

      days_after_initiation = 7

    }

  }

}

The empty filter {} applies the rule to all objects in the bucket. The expiration setting targets older versions, not the current state.

The 365-day period is an example—adjust it to your recovery requirements.

Require HTTPS

Next, reject requests sent over unencrypted HTTP:


data "aws_iam_policy_document" "terraform_state" {

  statement {

    sid    = "DenyInsecureTransport"

    effect = "Deny"



    principals {

      type        = "*"

      identifiers = ["*"]

    }



    actions = ["s3:*"]

    resources = [

      aws_s3_bucket.terraform_state.arn,

      "${aws_s3_bucket.terraform_state.arn}/*"

    ]



    condition {

      test     = "Bool"

      variable = "aws:SecureTransport"

      values   = ["false"]

    }

  }

}


resource "aws_s3_bucket_policy" "terraform_state" {

  bucket = aws_s3_bucket.terraform_state.id

  policy = data.aws_iam_policy_document.terraform_state.json

}

The data source generates the policy JSON; the resource attaches it to the bucket.

The wildcard principal does not make the bucket public: this is a deny statement. It rejects insecure requests without granting access to anyone.

Run the bootstrap stack

From your project directory:


cd bootstrap

terraform init

terraform fmt

terraform validate

terraform plan -out=bootstrap.tfplan

terraform apply bootstrap.tfplan

Review the plan before applying it. Once the apply finishes, check the bucket in AWS and confirm its settings.

The bootstrap state is still local. Keep it safe and out of Git—it tracks the bucket and its configuration. You can migrate it later using a separate state key.

Configure the infrastructure backend

Now create infra/backend.tf:


terraform {

  required_version = ">= 1.10.0"



  backend "s3" {

    bucket       = "my-eks-project-tfstate-unique-suffix"

    key          = "infra/terraform.tfstate"

    region       = "us-east-1"

    encrypt      = true

    use_lockfile = true

  }

}

Here’s what these settings mean:

| Setting | Purpose |

| --- | --- |

| bucket | The bucket created by bootstrap |

| key | The object path where this configuration's state is stored |

| region | The bucket's AWS Region |

| encrypt | Enables server-side encryption for backend writes |

| use_lockfile | Enables native S3 state locking |

Give independent configurations different state keys so they don’t accidentally share the same state.

Backend blocks cannot reference normal Terraform variables, so bucket = var.state_bucket_name won’t work here. Use literal values or supply them through terraform init -backend-config=....

Initialize the remote backend

For a new infrastructure configuration:


cd ../infra

terraform init

If infra/ already has local state you want to move, use this instead and follow the migration prompts:


terraform init -migrate-state

When Terraform writes the infrastructure state, it will use:

s3://my-eks-project-tfstate-unique-suffix/infra/terraform.tfstate

During operations that acquire a lock, Terraform also uses:

infra/terraform.tfstate.tflock

The lock object is normally removed when Terraform releases the lock, so you may not see it after a successful run.

We’re using native S3 locking here. A DynamoDB table isn’t needed; DynamoDB-based locking is deprecated.

Make sure Terraform has access

The AWS identity running Terraform needs these backend permissions:

| Target | Required actions |

| --- | --- |

| State bucket | s3:ListBucket, scoped to the relevant prefix |

| State object | s3:GetObject, s3:PutObject |

| Lock object | s3:GetObject, s3:PutObject, s3:DeleteObject |

These permissions are separate from the permissions needed to create your application infrastructure. The HTTPS-only bucket policy above does not grant them.

Scope access to the intended bucket and state paths.

Try it out!

With your infrastructure resources and provider configured in infra/, run:


terraform plan -out=infra.tfplan

terraform apply infra.tfplan

terraform state list

The saved plan lets you apply exactly what you reviewed. After applying, check S3 for the state object and its version history.

Keep state files and saved plan files out of Git because they can contain sensitive values. Commit .terraform.lock.hcl—that file records provider dependency selections, not infrastructure state locks.

Clean up the lab

When you’re finished, run this from infra/:


terraform destroy

This removes infrastructure managed by that configuration. The state bucket remains because it belongs to the separate bootstrap configuration.

Keep the bucket if you plan to reuse the project. Only retire it once no configurations depend on it, and remember that deleting a versioned bucket requires handling its stored versions too.

References