Skip to content

jottenlips/terraform-state-s3-backend-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Getting started with Terraform state, using an s3 backend

Chicken or Egg?

In order to use terraform to keep track of your infrastructure, you need a terraform backend. A terraform backend on AWS requires a dynamodb table and an s3 bucket. You could spin up these resources in the console or the aws cli, but what if you want these resources under terraform as well? These 3 steps will get your terraform state under terraform.

  1. comment out the terraform backend to deploy bucket and dynamodb, deploy
terraform init
terraform plan
terraform apply
  1. uncomment terraform backend, import dynamodb and s3_bucket that were created, deploy
terraform import aws_dynamodb_table.terraform_state_lock "tf-lock-table"
terraform import aws_s3_bucket.terraform_states "tf-states-s3backend"
terraform plan
terraform apply

You now have a terraform backend on s3!

  1. Use terraform state in other repos for you aws account
terraform {
  backend "s3" {
    bucket                  = "tf-states-s3backend"
    key                     = "environments/develop/network.tfstate"
    region                  = "us-east-1"
    encrypt                 = true
    profile                 = "default"
    dynamodb_table          = "tf-lock-table"
    shared_credentials_file = "$HOME/.aws/credentials"
  }
}