Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: variables instead of locals #27

Open
Imany-apk opened this issue Jun 7, 2023 · 1 comment
Open

[Question]: variables instead of locals #27

Imany-apk opened this issue Jun 7, 2023 · 1 comment
Labels
question Further information is requested

Comments

@Imany-apk
Copy link

Imany-apk commented Jun 7, 2023

Is it possible to use variables instead of locals?
Is locals usage just for clean code?

@Imany-apk Imany-apk added the question Further information is requested label Jun 7, 2023
@Shayan-Ghani
Copy link
Member

We typically use locals to collect data from different components and then strip them to something we wish to achieve. here are some examples using functions:

locals {
names = ["Alice", "Bob"]
greetings = ["Hello", "Hi"]

concatenated_list = concat(local.names, local.greetings)
concatenated_string = concat("Hello ", "World!")
}

locals {
words = ["Hello", "World", "Terraform"]

joined_string = join(", ", local.words)
}

locals {
numbers = [1, 2, 3, 4, 5]

doubled_numbers = map(local.numbers, multiply(2))
}

Also, this is what chatgpt says which is quite fine:
Variables are meant to define input values that can be passed to Terraform when executing a plan or apply operation. They allow for flexibility and customization by allowing users to provide different values for those variables based on the environment or specific requirements.

Locals, on the other hand, are used to define intermediate values within the Terraform configuration. They can be derived from variables, data sources, or other resources. Locals help improve the readability and maintainability of the code by providing a way to store and reuse values within the configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants