Skip to content

A Terraform module for creating an Amazon S3 bucket and DynamoDB table for storing Terraform state

License

Notifications You must be signed in to change notification settings

infrable-io/terraform-aws-s3-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS S3 Backend Terraform Module

MIT License Maintained by Infrable

A Terraform module for creating an Amazon S3 bucket and DynamoDB table for storing Terraform state.

Overview

From the Terraform documentation on S3 backends...

Stores the state as a given key in a given bucket on Amazon S3. This backend also supports state locking and consistency checking via DynamoDB, which can be enabled by setting the dynamodb_table field to an existing DynamoDB table name.

Usage

terraform {
  backend "s3" {
    bucket         = "<s3_bucket>"
    key            = "<path/to/key>"
    region         = "us-east-1"
    dynamodb_table = "<dynamodb_table>"
  }
}

Chicken and Egg Problem

Where does one store the Terraform state of the infrastructure for storing Terraform state? Answer: locally. Before using the Amazon S3 bucket and DynamoDB table created by this module to store its own state, you must first store state on the local filesystem. Thereafter, state can be stored using the S3 backend.

Step 1: Initialize Terraform using local backend

$ terraform init
Initializing modules...
...

Initializing the backend...
...

Terraform has been successfully initialized!

Step 2: Apply state

$ terraform apply
...

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Step 3: Reconfigure Terraform with S3 backend

terraform {
  backend "s3" {
    bucket         = "<s3_bucket>"
    key            = "<path/to/key>"
    region         = "us-east-1"
    dynamodb_table = "<dynamodb_table>"
  }
}
$ terraform init -reconfigure
Initializing modules...

Initializing the backend...
Do you want to copy existing state to the new backend?
  Pre-existing state was found while migrating the previous "local" backend to the
  newly configured "s3" backend. No existing state was found in the newly
  configured "s3" backend. Do you want to copy this state to the new "s3"
  backend? Enter "yes" to copy and "no" to start with an empty state.

  Enter a value: yes

Terraform Module Documentation

Resources

Name Type
aws_dynamodb_table.dynamodb_table resource
aws_s3_bucket.s3_bucket resource

Inputs

Name Description Type Default Required
dynamodb_table Name of the DynamoDB table to store Terraform state lock. string n/a yes
s3_bucket Name of the Amazon S3 bucket to store Terraform state. string n/a yes

Outputs

Name Description
dynamodb_table The name of the DynamoDB table
s3_bucket The name of the S3 bucket

About

A Terraform module for creating an Amazon S3 bucket and DynamoDB table for storing Terraform state

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages