-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Description
Description
The following will raise an error if some-bucket does not exist.
module "demo_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
# version = "5.7.1" # release 5.8.0 raises an error when create_bucket = false
create_bucket = false
bucket = "some-bucket"
}
Error:
│ Error: Invalid index
│
│ on .terraform/modules/demo_bucket/main.tf line 21, in locals:
│ 21: "_S3_BUCKET_ID_" = var.is_directory_bucket ? aws_s3_directory_bucket.this[0].bucket : aws_s3_bucket.this[0].id,
│ ├────────────────
│ │ aws_s3_bucket.this is empty tuple
│
│ The given key does not identify an element in this collection value: the collection has no elements.
│ Error: Invalid index
│
│ on .terraform/modules/demo_bucket/main.tf line 22, in locals:
│ 22: "_S3_BUCKET_ARN_" = var.is_directory_bucket ? aws_s3_directory_bucket.this[0].arn : aws_s3_bucket.this[0].arn,
│ ├────────────────
│ │ aws_s3_bucket.this is empty tuple
│
│ The given key does not identify an element in this collection value: the collection has no elements.
╵
I believe this to be cause by release 5.8.0, specifically by this PR: #363
The error is thrown from this location. I think there needs to be a check that skips setting the policy placeholders if the bucket doesn't exist. @antonbabenko , do you have an idea for a quick fix?
terraform-aws-s3-bucket/main.tf
Lines 21 to 22 in 06b96ee
| "_S3_BUCKET_ID_" = var.is_directory_bucket ? aws_s3_directory_bucket.this[0].bucket : aws_s3_bucket.this[0].id, | |
| "_S3_BUCKET_ARN_" = var.is_directory_bucket ? aws_s3_directory_bucket.this[0].arn : aws_s3_bucket.this[0].arn, |
- ✋ I have searched the open/closed issues and my issue is not listed.
Versions
-
Module version [Required]: 5.8.0
-
Terraform version: Terraform v1.12.2
- Provider version(s): provider registry.terraform.io/hashicorp/aws v6.16.0
Reproduction Code [Required]
Steps to reproduce the behavior:
Run terraform plan on the above code.
Expected behavior
I expect the module to do nothing, if create_bucket=false and the bucket does not exist.
This can be useful if running terraform against multiple targets where some condition controls whether or not buckets are required in a particular target.