diff --git a/CHANGELOG.md b/CHANGELOG.md index 7917b04..ad2f24e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,4 +56,8 @@ Provides standard and t26 S3 bucket construction. - pin aws provider to < 4.0 - add bucket_key_enabled (for S3 bucket keys; https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucket-key.html) - +* 2.4.2 -- 2022-02-24 + - add variables for controlling region, account, and compacting region if name > 63 + - name_include_region + - name_include_account + - name_include_region_compact diff --git a/common/resources.tf b/common/resources.tf index 07963aa..47aa9b7 100644 --- a/common/resources.tf +++ b/common/resources.tf @@ -1,7 +1,15 @@ locals { + region_short = join("", [for c in split("-", local.region) : substr(c, 0, 1)]) base_name = var.bucket_name - name = replace(var.bucket_name, local._prefixes["s3"], "") - bucket_name = format("%s%s", local._prefixes["s3"], local.name) + b_name = replace(var.bucket_name, local._prefixes["s3"], "") + b_account = var.name_include_account ? format("-%v", local.account_id) : "" + b_region = var.name_include_region ? format("-%v", local.region) : "" + b_region_short = format("-%v", local.region_short) + c_name = format("%v%v%v", local.b_name, local.b_account, local.b_region) + b_bucket_name = format("%v%v", local._prefixes["s3"], local.c_name) + too_long = length(local.b_bucket_name) > 63 && var.name_include_region_compcat ? true : false + name = local.too_long ? format("%v%v%v", local.b_name, local.b_account, local.b_region_short) : local.c_name + bucket_name = format("%v%v", local._prefixes["s3"], local.name) bucket_policy_document = length(var.bucket_policy_document) > 0 ? var.bucket_policy_document : data.aws_iam_policy_document.empty.json # kms_key_arn = aws_kms_key.key.arn @@ -215,3 +223,10 @@ resource "aws_s3_bucket_ownership_controls" "this" { object_ownership = var.bucket_owner } } + +resource "null_resource" "name_too_long" { + count = local.too_long ? 1 : 0 + provisioner "local-exec" { + command = "echo 'The resultant name ${local.b_bucket_name} > 63, shortening to ${local.bucket_name}'" + } +} diff --git a/common/variables.s3.tf b/common/variables.s3.tf index b8bb04b..2c7901a 100644 --- a/common/variables.s3.tf +++ b/common/variables.s3.tf @@ -72,3 +72,21 @@ variable "bucket_key_enabled" { type = bool default = false } + +variable "name_include_region" { + description = "Flag to determine if we include the full region name in the resulting bucket name" + type = bool + default = false +} + +variable "name_include_account" { + description = "Flag to determine if we include the AWS Account id in the resulting bucket name" + type = bool + default = false +} + +variable "name_include_region_compact" { + description = "Flag to determine if we the rewrite the full region name to a shorter region name if the resulting name > 63 characters" + type = bool + default = true +} diff --git a/common/version.tf b/common/version.tf index 58b0c87..930d737 100644 --- a/common/version.tf +++ b/common/version.tf @@ -1,3 +1,3 @@ locals { - _module_version = "2.3.1" + _module_version = "2.4.2" }