Dynamic resource names

Hi team,
Me new to the terraform environment. My customer would like to create the bit bucket pipeline which dynamically creates the s3 buckets (one at a time ). Customer don’t want to use the module but have asked to create using the simple resource. My concern is the resouce name, I don’t want my team to modify the terraform script to change the resource name every time they create new s3 bucket.

What will be the best approach to build the dynamic bitbucket pipeline, with a dynamic resource name?

Regards,
nikhil shah

Could you give some code to explain specifically what you are asking as I’m not clear?

Are you asking about the best way to create multiple resources? If so, maybe taking a look at for_each (possibly using a variable to pass the values from a .tfvars file) might help?

Hi Stuart,
My requirements are developer should run the bitbucket pipeline to create the new s3 buckets.
When they run the pipeline, they will provide the bucket name and it should create the bucket.

With my current file (below), the resource name needs to change the everytime i.e. change the terraform file.

resource “aws_s3_bucket” “new-resource-name” {

bucket      =   var.awsBucketName
tags        =   var.bukcketTags
tags_all    =   var.bucketTagsAll

versioning {
            enabled       =   var.bucketVersioning
            mfa_delete    =   var.bucketMFADelete
          }

}

Is it possible to give the resource name during the execution so no need to change the terraform code for every execution? Or what could be the best way to address the requirements?

Please be noted, my customer doesn’t want to use the module approach.

Regards,
Nikhil Shah

You mention “giving the bucket name during the execution” which sounds a bit like you are trying to use Terraform in a way it isn’t designed for.

What is expected is that the Terraform code lists all the buckets being managed, not just anything “new”. This can be done by creating new resources in the code, or using the count and for_each mechanisms.

Modules are a separate concept, mostly as a tool to make grouping of resources & code reuse easier. I’m not really sure what you are meaning by “the module approach”, but that is a separate item anyway.

My suggestion would be to look at using for_each. The process would then be that the customer edits the terraform.tfvars file, which contains a map that lists all of the buckets to create/manage. If more buckets are desired, entries are added to that file, which once merged triggers the pipeline to ask Terraform to make the appropriate changes.

Hi Stuart,
Thank you for your suggestion. I have used the for_each option to address the requirements. One point, it is really risky to provide the capabilities to developers to modify the variable list, as if they deleted one of the resources (human error) then the respective resource gets terminated.

I understand the terraform is built to create the resources based on the defined script, but if it interpolation in resource names then it will resolve multiple issues.

Regards,
Nikhil Shah

There is a risk yes, which is why for any changes (not just in Terraform but Git in general) it is recommended to look at using PRs with someone else reviewing them.

Using count or for_each are the methods for making resource names more dynamic.

Hi @divynikhu ,

You’ve said a few things in this thread which seem a bit odd:

Terraform is fundamentally designed for developers to update input files, and as a result have changes applied to a system.

If that’s literally not what you want to happen, as the quotes seem to suggest, why do you want to use Terraform?

I should also ask: You’ve covered creating buckets. But, how do you intend modifications of existing buckets, or deleting buckets, to be performed?

use the variable and pass the value from CLI during terraform apply,
incorporate this logic i your pipeline, modify the pipeline accordingly
**if possible you can take out the for each logic from terraform and execute it in pipeline