Skip to content

Instantly share code, notes, and snippets.

@rowleyaj
Last active February 11, 2022 20:19
Show Gist options
  • Save rowleyaj/13737308799838ff989dc15a278e1cf7 to your computer and use it in GitHub Desktop.
Save rowleyaj/13737308799838ff989dc15a278e1cf7 to your computer and use it in GitHub Desktop.
Terraform S3 Backend

Testing the S3 backend + DynamoDB locking

  1. Clone this gist and change directory to it
  2. Rename run-2nd.tf to an alternative file ending to prevent it being run.
  3. terraform init
  4. Normally you would plan and save to a file but for this example we're going to just apply directly terraform apply
  5. Rename run-2nd.tf back to it's original name
  6. The backend has changed so requires a new terraform init
  7. terraform apply
# This uses Terraform to configure the resources required for remote state
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "state" {
bucket = "rowleyaj-tf-state-demo"
tags {
Name = "rowleyaj-tf-state-demo"
Environment = "testing"
}
}
resource "aws_dynamodb_table" "state" {
name = "rowleyaj-terraform-state-lock"
read_capacity = 5
write_capacity = 5
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
tags {
Name = "rowleyaj-terraform-state-lock"
Environment = "testing"
}
}
resource "null_resource" "sleep" {
provisioner "local-exec" {
command = "sleep 10"
}
}
# This doesn't need to be it's own file but I've seperated it out to show the
# process used during this test. Either add this as separate file or append
# to the file above and re-run
terraform {
required_version = "~> 0.10"
backend "s3" {
bucket = "rowleyaj-tf-state-demo"
key = "v1/terraform-remote-state-example"
region = "us-east-1"
encrypt = true
dynamodb_table = "rowleyaj-terraform-state-lock"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment