Skip to content

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

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE
MIT
LICENSE-NOZAQ
Notifications You must be signed in to change notification settings

escapace/terraform-aws-terraform-backend

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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.

Inputs

Name Description Type Default Required
additional_tag_map Additional key-value pairs to add to each map in tags_as_list_of_maps. Not added to tags or id. This is for some rare cases where resources want additional configuration of tags and therefore take a list of maps with tag key, value, and additional configuration. map(string) {} no
attributes ID element. Additional attributes (e.g. workers or cluster) to add to id, in the order they appear in the list. New attributes are appended to the end of the list. The elements of the list are joined by the delimiter and treated as a single ID element. list(string) [] no
context Single object for setting entire context at once. See description of individual variables for details. Leave string and numeric variables as null to use default value. Individual variable settings (non-null) override settings in context object, except for attributes, tags, and additional_tag_map, which are merged. any { "additional_tag_map": {}, "attributes": [], "delimiter": null, "descriptor_formats": {}, "enabled": true, "environment": null, "id_length_limit": null, "label_key_case": null, "label_order": [], "label_value_case": null, "labels_as_tags": [ "unset" ], "name": null, "namespace": null, "regex_replace_chars": null, "stage": null, "tags": {}, "tenant": null } no
delimiter Delimiter to be used between ID elements. Defaults to - (hyphen). Set to "" to use no delimiter at all. string null no
descriptor_formats Describe additional descriptors to be output in the descriptors output map. Map of maps. Keys are names of descriptors. Values are maps of the form { format = string labels = list(string) } (Type is any so the map values can later be enhanced to provide additional options.) format is a Terraform format string to be passed to the format() function. labels is a list of labels, in order, to pass to format() function. Label values will be normalized before being passed to format() so they will be identical to how they appear in id. Default is {} (descriptors output will be empty). any {} no
dynamodb_table_billing_mode Controls how you are charged for read and write throughput and how you manage capacity. string "PAY_PER_REQUEST" no
enabled Set to false to prevent the module from creating any resources bool null no
environment ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' string null no
id_length_limit Limit id to this many characters (minimum 6). Set to 0 for unlimited length. Set to null for keep the existing setting, which defaults to 0. Does not affect id_full. number null 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 30 no
kms_key_description The description of the key as viewed in AWS console. string "The key used to encrypt the remote state bucket." no
kms_key_enable_key_rotation Specifies whether key rotation is enabled. bool true no
label_key_case Controls the letter case of the tags keys (label names) for tags generated by this module. Does not affect keys of tags passed in via the tags input. Possible values: lower, title, upper. Default value: title. string null no
label_order The order in which the labels (ID elements) appear in the id. Defaults to ["namespace", "environment", "stage", "name", "attributes"]. You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. list(string) null no
label_value_case Controls the letter case of ID elements (labels) as included in id, set as tag values, and output by this module individually. Does not affect values of tags passed in via the tags input. Possible values: lower, title, upper and none (no transformation). Set this to title and set delimiter to "" to yield Pascal Case IDs. Default value: lower. string null no
labels_as_tags Set of labels (ID elements) to include as tags in the tags output. Default is to include all labels. Tags with empty values will not be included in the tags output. Set to [] to suppress all generated tags. Notes: The value of the name tag, if included, will be the id, not the name. Unlike other null-label inputs, the initial setting of labels_as_tags cannot be changed in later chained modules. Attempts to change it will be silently ignored. set(string) [ "default" ] no
name ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'. This is the only ID element not also included as a tag. The "name" tag is set to the full id string. There is no tag with the value of the name input. string null no
namespace ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique string null no
noncurrent_version_expiration Specifies when noncurrent object versions expire. See the aws_s3_bucket document for detail. object({ days = number }) null 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 })) [ { "days": 7, "storage_class": "GLACIER" } ] no
regex_replace_chars Terraform regular expression (regex) string. Characters matching the regex will be removed from the ID elements. If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits. string null 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 false 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 null no
s3_logging_target_prefix The prefix to apply on bucket logs, e.g "logs/". string "" no
stage ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' string null no
tags Additional tags (e.g. {'BusinessUnit': 'XYZ'}). Neither the tag keys nor the tag values will be modified by this module. map(string) {} no
tenant ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for string null no
terraform_iam_policy_create Specifies whether to terraform IAM policy is created. bool true no

Outputs

Name Description
configuration Terraform S3 backend configuration.
dynamodb_table The DynamoDB table to manage lock states.
iam_policy The IAM Policy to access remote state environment.
kms_key The KMS customer master key to encrypt state buckets.
kms_key_alias The alias of the KMS customer master key used to encrypt state bucket and dynamodb.
s3_bucket The S3 bucket to store the remote state file.

Resources

Name Type
aws_dynamodb_table.lock resource
aws_iam_policy.terraform resource
aws_kms_alias.this resource
aws_kms_key.this resource
aws_s3_bucket.state resource
aws_s3_bucket_acl.state resource
aws_s3_bucket_lifecycle_configuration.state resource
aws_s3_bucket_logging.state resource
aws_s3_bucket_ownership_controls.state resource
aws_s3_bucket_policy.state_force_ssl resource
aws_s3_bucket_public_access_block.state resource
aws_s3_bucket_server_side_encryption_configuration.state resource
aws_s3_bucket_versioning.state resource
aws_iam_policy_document.state_force_ssl data source

Requirements

Name Version
terraform >=1.9.8
aws >=5.72.1

About

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

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE
MIT
LICENSE-NOZAQ

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HCL 99.5%
  • JavaScript 0.5%