S3 Remote State Backend
This repository helps you to store Terraform State in S3 bucket in an Amazon Web Services (AWS). The S3 bucket can be used for remote state storage.
Pre-requisites
- You must have Terraform installed on your computer.
- You must have an Amazon Web Services (AWS) account.
This templated tested with Terraform v0.11.11 and provider.aws v1.54.0.
Configure AWS Access Key
Technically, Terraform can read AWS Access Key from env
file. Thus, you can configure AWS Access Key with AWS CLI:
aws configure
Configure your AWS access keys as environment variables:
export AWS_ACCESS_KEY_ID=(your access key id)
export AWS_SECRET_ACCESS_KEY=(your secret access key)
Configuration
In vars.tf
you can define Bucket Name and AWS Region of your bucket by using the default
parameter:
variable "bucket_name" {
description = "The name of the S3 bucket. Must be globally unique."
default = "(YOUR_BUCKET_NAME)"
}
variable "aws_region" {
description = "The AWS region to use"
default = "(AWS Region)"
}
If you don't configure above, terraform asks you prior to apply.
Terraform
Validate the templates:
terraform plan
Deploy the code:
terraform apply
In case if you need to clean up when you're done you need to change prevent_destroy = false
in main.tf
first and then perform following:
terraform apply
terraform destroy
Usage as a Backend Configuration
For using this bucket as remote state storage you need to add following configuration in terraform file:
terraform {
backend "s3" {
bucket = "Bucket Name"
key = "(Path To Your tfstate file)/terraform.tfstate"
region = "(Bucket Region)"
}
}