I'm trying to unify naming of resources, depending on the environment (dev, stage or prod). I will illustrate with an example.
Let's say I want to create an aws iam user resource like:
resource "aws_iam_user" "iam_foo" {
name = "foo_dev"
}
Here I hard-coded "dev" into the name of the resource. But ideally I would like that to be done dynamically based on the folder it is located in (folder resembles an environment like dev or prod).
The folder structure looks something like:
├── README.md
├── meow-development
│ ├── locals.tf -> ../locals.tf
│ ├── main.tf
│ └── s3.tf
├── meow-production
│ ├── locals.tf -> ../locals.tf
│ ├── main.tf
│ └── s3.tf
├── meow-staging
│ ├── locals.tf -> ../locals.tf
│ ├── main.tf
│ └── s3.tf
So what I am trying to achieve is something like:
resource "aws_iam_user" "iam_foo" {
name = naming_function(name) # Not intended as actual code
}
The naming function takes a name as input and names it according to the env. So if this resource is created under dev, then naming_function(woof)
should return the string "woof_dev"
So my questions are:
- How can I name things dynamically?
- Is using a naming convention that includes the environment such as meow_{env_name} correct?