Demo Code

In order to maintain your tfstate file properly, you MUST have versioning enabled on your S3 bucket.

Here is the code I used to create the backend.tf file. You'll need to update it with the name of your S3 bucket and the path to your terraform.tfstate file.

terraform {
    backend "s3" {
        bucket = "<Name of your S3 bucket>"
        key = "<Path To Your terraform.tfstate file>" 
        region = "us-east-1"
    }
}

Here is the code for terraform.tf used to create the S3 backend. You can model yours off my example, or be creative and create your own- just make sure you destroy any infrastructure you create!

provider "aws" {
  access_key = "<Your Access Key>"
  secret_key = "<Your Secret Key>"
  region = "us-east-1"
}

resource "aws_instance" "Backend" {
  count = "2"
  ami = "ami-0323c3dd2da7fb37d"
  instance_type = "t2.micro"
}

Save your backend.tf file with your terraform.tf (or main.tf file) in a working directory under your Terraform root directory.