Skip to content

Commit

Permalink
- add variables for controlling region, account, and compacting regio…
Browse files Browse the repository at this point in the history
…n if name > 63

  - name_include_region
  - name_include_account
  - name_include_region_compact
  • Loading branch information
badra001 committed Feb 24, 2022
1 parent 348da7a commit d3169bc
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 17 additions & 2 deletions common/resources.tf
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}'"
}
}
18 changes: 18 additions & 0 deletions common/variables.s3.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion common/version.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
locals {
_module_version = "2.3.1"
_module_version = "2.4.2"
}

0 comments on commit d3169bc

Please sign in to comment.