# state-backend / aws / t1 "Standard" # # The bucket every other Terraform folder keeps its state in: your own key, # every version kept for a set time, TLS required, ACLs off, and protected # from being destroyed by accident. # # Verify: # Start terraform plan in two terminals against a folder using this backend. # The second waits for the state lock. data "aws_caller_identity" "current" {} data "aws_region" "current" {} data "aws_partition" "current" {} locals { account_id = data.aws_caller_identity.current.account_id tags = merge({ owner = var.owner environment = var.environment data_classification = "restricted" managed_by = "terraform" }, var.extra_tags) } resource "aws_kms_key" "state" { description = "${var.name} Terraform state" deletion_window_in_days = 30 enable_key_rotation = true policy = data.aws_iam_policy_document.key.json } # Written out, though it matches what AWS would apply by default, so anyone # reading this folder can see who controls the key without looking it up. data "aws_iam_policy_document" "key" { statement { sid = "AccountManagesKeyThroughIAM" effect = "Allow" principals { type = "AWS" identifiers = ["arn:${data.aws_partition.current.partition}:iam::${local.account_id}:root"] } actions = ["kms:*"] resources = ["*"] } } resource "aws_kms_alias" "state" { name = "alias/${var.name}" target_key_id = aws_kms_key.state.key_id } resource "aws_s3_bucket" "state" { bucket = "${var.name}-${local.account_id}" # Losing this bucket loses the record of everything Terraform manages. A # destroy that reaches it fails, and removing this line is a deliberate edit # that shows up in review. lifecycle { prevent_destroy = true } } resource "aws_s3_bucket_versioning" "state" { bucket = aws_s3_bucket.state.id versioning_configuration { status = "Enabled" } } resource "aws_s3_bucket_server_side_encryption_configuration" "state" { bucket = aws_s3_bucket.state.id rule { apply_server_side_encryption_by_default { sse_algorithm = "aws:kms" kms_master_key_id = aws_kms_key.state.arn } bucket_key_enabled = true } } resource "aws_s3_bucket_public_access_block" "state" { bucket = aws_s3_bucket.state.id block_public_acls = true block_public_policy = true ignore_public_acls = true restrict_public_buckets = true } resource "aws_s3_bucket_ownership_controls" "state" { bucket = aws_s3_bucket.state.id rule { object_ownership = "BucketOwnerEnforced" } } # Versioning keeps every write forever unless something expires the old ones. # This keeps them long enough to roll back, then lets them go. resource "aws_s3_bucket_lifecycle_configuration" "state" { bucket = aws_s3_bucket.state.id depends_on = [aws_s3_bucket_versioning.state] rule { id = "expire-old-state-versions" status = "Enabled" filter {} noncurrent_version_expiration { noncurrent_days = var.keep_old_versions_days } abort_incomplete_multipart_upload { days_after_initiation = 7 } } } data "aws_iam_policy_document" "state" { statement { sid = "DenyUnencryptedTransport" effect = "Deny" actions = ["s3:*"] principals { type = "AWS" identifiers = ["*"] } resources = [aws_s3_bucket.state.arn, "${aws_s3_bucket.state.arn}/*"] condition { test = "Bool" variable = "aws:SecureTransport" values = ["false"] } } # Every state write must name this key. A client that forgets encryption, or # tries another key, is refused. statement { sid = "DenyWritesWithoutThisKey" effect = "Deny" actions = ["s3:PutObject"] principals { type = "AWS" identifiers = ["*"] } resources = ["${aws_s3_bucket.state.arn}/*"] condition { test = "StringNotEqualsIfExists" variable = "s3:x-amz-server-side-encryption-aws-kms-key-id" values = [aws_kms_key.state.arn] } } } resource "aws_s3_bucket_policy" "state" { bucket = aws_s3_bucket.state.id policy = data.aws_iam_policy_document.state.json depends_on = [aws_s3_bucket_public_access_block.state] }