Published 2022-06-23
By default, Terraform stores the state file locally when the terraform plan
command is executed.
The state was stored under a file named terraform.tfstate
.
However, this isn't practical because it is not reliable. It's just like storing your code at a remote repository such as Github vs storing your code at your local machine.
Which do you feel more comfortable with? 100% for me is storing the code at a remote repository.
Since we made up our mind to store the state file remotely, the next question is where could we store this remote state? We have multiple options here such as:
In this post, I will be storing the remote state in Amazon S3 since it is one of the most popular cloud services in the world.
Here is the TLDR version of the steps:
backend
in terraform blocks3
as the backendConfigure the minimal 3 required fields within the backend
block.
bucket
- The bucket you have created in AWS S3 for storing the state file.key
- The path and name of the state file within the bucketregion
- The region of the S3 Bucketinit
and apply
.In the below terraform file, I use docker provider to:
tutorial
.s3
block.// main.tf
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.13.0"
}
}
backend "s3" {
bucket = "tek-terraform-bk"
key = "test.tfstate"
region = "ap-southeast-1"
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 8000
}
}
Below is the screenshot of the terraform state file being saved into the S3 bucket.
I hope this post does provide value to you.
Thank you.