Skip to content

Setting create_bucket=false raises error if bucket does not exist #364

@ciklista

Description

@ciklista

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?

"_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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions