State Locking with DynamoDB

The Terraform S3 Backend docs show how to create a DynamoDB table in your AWS account, which it uses to lock the remote State file.

This ensure that the state is only being updated from one place at a time (no concurrent updates), useful in team settings where people or processes may attempt to run an apply at the same time.

We create a table of any name, and ensure the primary key is named LockID and is type string.

File cloudcasts.tf

Once we create the DynamoDB table, we can update our configuration to make use of it for our backend:

1  
2 terraform {
3 required_providers {
4 aws = {
5 source = "hashicorp/aws"
6 version = "~> 3.0"
7 }
8 }
9  
10 backend "s3" {
11 bucket = "terraform-course-cloudcasts"
12 key = "cloudcasts/terraform.tfstate"
13 region = "us-east-2"
14 profile = "cloudcasts"
15+ dynamodb_table = "cloudcasts-terraform-course"
16 }
17 }

Don't miss out

Sign up to learn when new content is released! Courses are in production now.