I don’t think I fully understand your question, but I do notice a problem with your configuration even though I’m not sure if it relates to what you are asking:
In all instances across both of your module calls you have the example1 variable set to "example1". You have four instances of this module in total, so this is telling Terraform that four different S3 buckets should exist and should all be named "example1".
Amazon S3 requires bucket names to be unique, so the desired state described by this configuration is impossible to reach.
To achieve a working configuration you will need to set the example1 variable differently for each instance of the module.
Should have made it a bit less confusing and hopefully this clears things up
Goal - Lessen the amount of file locations where I had to put code to name a bucket.
Currently to create multi resources. I am doing this -
create a resource block for each resource
then variable for each name
and in the module block add each variable
What I was hoping to do is
create a “static” resource block inside my modules folder. This resource block will be referenced multiple times to create buckets using the for_each function to set their name. While having a separate customizable resource blocks in the same module folder to be created individually
Original code gave me created 10 resources instead of 6. Trying to change it up put me in a rabbit hole I couldnt get out of
resource “aws_s3_bucket” “testing” {
bucket = var.bucket_name # Replace with your desired bucket name
}
resource “aws_s3_bucket” “testing2” {
bucket = var.bucket_name2 # Replace with your desired bucket name
}
resource “aws_s3_bucket” “s3-acl” {
bucket = “multi-bucket-test”
}
For your module to work as a multi-instance module you will need to make sure all of the buckets declared in it can have variable bucket names.
The example you shared most recently still declares a bucket with the hard-coded name "multi-bucket-test", and so there can only be one instance of this module due to the requirement for the names to be unique. If you change that third declaration to also use an input variable then it should work, as long as each instance of the module chooses a different name for the third bucket.
Alternatively, if what you are trying to describe is a variable number of testing1 and a variable number of testing2 instances, but only exactly one “s3-acl”, then it might be more appropriate to change your module so that it takes set(string) variables specifying the multiple names and then use for_each in the resource blocks inside the module instead. Then you will have only one instance of the module but a dynamic number of instances of the first and second S3 bucket resources.
Thank you for the insight and advice. Sorry for the late reply, was doing some testing.
Removed bucket name inside the resource block. And it is creating multiple buckets now.
Issue I am running into now is that my two resource blocks are getting created over and over again.
When I try to use a set(string) I get this error.
│ Error: Incorrect attribute value type
│
│ on …..\modules\s3basic\main.tf line 89, in resource “aws_s3_bucket” “fill_in_name”:
│ 89: bucket = var.bucket_name
│ ├────────────────
│ │ var.bucket_name is a set of string
│
│ Inappropriate value for attribute “bucket”: string required.