Reputation: 2326
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:
Upvotes: 1
Views: 618
Reputation: 5549
Create a terraform.tfvars
inside the meow-development
folder with
env_name = "dev"
In the main.tf
inside the meow-development
folder:
resource "aws_iam_user" "iam_foo" {
name = "foo_${var.env_name}"
}
Same for other environments. Regarding naming convention, depends on the resource, having env as part of the name is considered a best practice.
Upvotes: 2