/terraform-aws-remote-state-s3-backend

A terraform module to set up remote state management with S3 backend for your account.

Primary LanguageHCLMIT LicenseMIT

terraform-aws-remote-state-s3-backend

Github Actions Releases

Terraform Module Registry

A terraform module to set up remote state management with S3 backend for your account. It creates an encrypted S3 bucket to store state files and a DynamoDB table for state locking and consistency checking. Resources are defined following best practices as described in the official document and ozbillwang/terraform-best-practices.

Features

  • Create a S3 bucket to store remote state files.
  • Encrypt state files with KMS.
  • Enable bucket replication and object versioning to prevent accidental data loss.
  • Automatically transit non-current versions in S3 buckets to AWS S3 Glacier to optimize the storage cost.
  • Optionally you can set to expire aged non-current versions(disabled by default).
  • Optionally you can set fixed S3 bucket name to be user friendly(false by default).
  • Create a DynamoDB table for state locking, encryption is optional.
  • Optionally create an IAM policy to allow permissions which Terraform needs.

Usage

The module outputs terraform_iam_policy which can be attached to IAM users, groups or roles running Terraform. This will allow the entity accessing remote state files and the locking table. This can optionally be disabled with terraform_iam_policy_create = false

provider "aws" {
  region = "us-east-1"
}

provider "aws" {
  alias  = "replica"
  region = "us-west-1"
}

module "remote_state" {
  source = "nozaq/remote-state-s3-backend/aws"

  providers = {
    aws         = aws
    aws.replica = aws.replica
  }
}

resource "aws_iam_user" "terraform" {
  name = "TerraformUser"
}

resource "aws_iam_user_policy_attachment" "remote_state_access" {
  user       = aws_iam_user.terraform.name
  policy_arn = module.remote_state.terraform_iam_policy.arn
}

Note that you need to provide two providers, one for the main state bucket and the other for the bucket to which the main state bucket is replicated to. Two providers must point to different AWS regions.

Once resources are created, you can configure your terraform files to use the S3 backend as follows.

terraform {
  backend "s3" {
    bucket         = "THE_NAME_OF_THE_STATE_BUCKET"
    key            = "some_environment/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    kms_key_id     = "THE_ID_OF_THE_KMS_KEY"
    dynamodb_table = "THE_ID_OF_THE_DYNAMODB_TABLE"
  }
}

THE_NAME_OF_THE_STATE_BUCKET, THE_ID_OF_THE_DYNAMODB_TABLE and THE_ID_OF_THE_KMS_KEY can be replaced by state_bucket.bucket, dynamodb_table.id and kms_key.id in outputs from this module respectively.

See the official document for more detail.

Compatibility

Requirements

Name Version
terraform >= 1.1.4
aws >= 4.0

Providers

Name Version
aws >= 4.0
aws.replica >= 4.0

Inputs

Name Description Type Required
dynamodb_enable_server_side_encryption Whether or not to enable encryption at rest using an AWS managed KMS customer master key (CMK) bool no
dynamodb_table_billing_mode Controls how you are charged for read and write throughput and how you manage capacity. string no
dynamodb_table_name The name of the DynamoDB table to use for state locking. string no
enable_replication Set this to true to enable S3 bucket replication in another region bool no
iam_policy_attachment_name The name of the attachment. string no
iam_policy_name_prefix Creates a unique name beginning with the specified prefix. string no
iam_role_arn Use IAM role of specified ARN for s3 replication instead of creating it. string no
iam_role_name_prefix Creates a unique name beginning with the specified prefix. string no
kms_key_deletion_window_in_days Duration in days after which the key is deleted after destruction of the resource, must be between 7 and 30 days. number no
kms_key_description The description of the key as viewed in AWS console. string no
kms_key_enable_key_rotation Specifies whether key rotation is enabled. bool no
noncurrent_version_expiration Specifies when noncurrent object versions expire. See the aws_s3_bucket document for detail.
object({
days = number
})
no
noncurrent_version_transitions Specifies when noncurrent object versions transitions. See the aws_s3_bucket document for detail.
list(object({
days = number
storage_class = string
}))
no
override_s3_bucket_name override s3 bucket name to disable bucket_prefix and create bucket with static name bool no
replica_bucket_prefix Creates a unique replica bucket name beginning with the specified prefix. string no
s3_bucket_force_destroy A boolean that indicates all objects should be deleted from S3 buckets so that the buckets can be destroyed without error. These objects are not recoverable. bool no
s3_bucket_name If override_s3_bucket_name is true, use this bucket name for replica instead of dynamic name with bucket_prefix string no
s3_bucket_name_replica If override_s3_bucket_name is true, use this bucket name instead of dynamic name with bucket_prefix string no
s3_logging_target_bucket The name of the bucket for log storage. The "S3 log delivery group" should have Objects-write und ACL-read permissions on the bucket. string no
s3_logging_target_prefix The prefix to apply on bucket logs, e.g "logs/". string no
state_bucket_prefix Creates a unique state bucket name beginning with the specified prefix. string no
tags A mapping of tags to assign to resources. map(string) no
terraform_iam_policy_create Specifies whether to terraform IAM policy is created. bool no
terraform_iam_policy_name_prefix Creates a unique name beginning with the specified prefix. string no

Outputs

Name Description
dynamodb_table The DynamoDB table to manage lock states.
kms_key The KMS customer master key to encrypt state buckets.
replica_bucket The S3 bucket to replicate the state S3 bucket.
state_bucket The S3 bucket to store the remote state file.
terraform_iam_policy The IAM Policy to access remote state environment.