Skip to content

Commit d39f19c

Browse files
shmickaknyshmaximmi
authored
Limit bucket name to 63 chars (#33)
* Limit bucket name to 63 chars As per S3 specs, buckets cannot be more than 63 characters long * bucket name added to be able to override it Co-authored-by: Andriy Knysh <[email protected]> Co-authored-by: Maxim Mironenko <[email protected]>
1 parent c991644 commit d39f19c

File tree

5 files changed

+15
-3
lines changed

5 files changed

+15
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ Available targets:
182182
| region | AWS Region the S3 bucket should reside in | string | - | yes |
183183
| restrict_public_buckets | Whether Amazon S3 should restrict public bucket policies for this bucket | bool | `true` | no |
184184
| role_arn | The role to be assumed | string | `` | no |
185+
| s3_bucket_name | S3 bucket name. If not provided, the name will be generated by the label module in the format namespace-stage-name | string | `` | no |
185186
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | string | `` | no |
186187
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map(string) | `<map>` | no |
187188
| terraform_backend_config_file_name | Name of terraform backend config file | string | `terraform.tf` | no |

docs/terraform.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
| region | AWS Region the S3 bucket should reside in | string | - | yes |
2727
| restrict_public_buckets | Whether Amazon S3 should restrict public bucket policies for this bucket | bool | `true` | no |
2828
| role_arn | The role to be assumed | string | `` | no |
29+
| s3_bucket_name | S3 bucket name. If not provided, the name will be generated by the label module in the format namespace-stage-name | string | `` | no |
2930
| stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | string | `` | no |
3031
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map(string) | `<map>` | no |
3132
| terraform_backend_config_file_name | Name of terraform backend config file | string | `terraform.tf` | no |

examples/complete/fixtures.us-west-1.tfvars

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ namespace = "eg"
55
stage = "test"
66

77
name = "terraform-tfstate-backend"
8+
9+
s3_bucket_name = "tfstate-backend-test-bucket"

main.tf

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ locals {
1111
var.terraform_backend_config_file_path,
1212
var.terraform_backend_config_file_name
1313
)
14+
15+
bucket_name = var.s3_bucket_name != "" ? var.s3_bucket_name : module.s3_bucket_label.id
1416
}
1517

1618
module "base_label" {
@@ -51,7 +53,7 @@ data "aws_iam_policy_document" "prevent_unencrypted_uploads" {
5153
]
5254

5355
resources = [
54-
"arn:aws:s3:::${module.s3_bucket_label.id}/*",
56+
"arn:aws:s3:::${local.bucket_name}/*",
5557
]
5658

5759
condition {
@@ -79,7 +81,7 @@ data "aws_iam_policy_document" "prevent_unencrypted_uploads" {
7981
]
8082

8183
resources = [
82-
"arn:aws:s3:::${module.s3_bucket_label.id}/*",
84+
"arn:aws:s3:::${local.bucket_name}/*",
8385
]
8486

8587
condition {
@@ -94,7 +96,7 @@ data "aws_iam_policy_document" "prevent_unencrypted_uploads" {
9496
}
9597

9698
resource "aws_s3_bucket" "default" {
97-
bucket = module.s3_bucket_label.id
99+
bucket = substr(local.bucket_name, 0, 63)
98100
acl = var.acl
99101
region = var.region
100102
force_destroy = var.force_destroy

variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,9 @@ variable "terraform_state_file" {
202202
default = "terraform.tfstate"
203203
description = "The path to the state file inside the bucket"
204204
}
205+
206+
variable "s3_bucket_name" {
207+
type = string
208+
default = ""
209+
description = "S3 bucket name. If not provided, the name will be generated by the label module in the format namespace-stage-name"
210+
}

0 commit comments

Comments
 (0)