In Terraform, the widely accepted and recommended naming convention for variables is
snake_case, which means:
- All letters are lowercase.
- Words are separated by underscores.
- Names should be descriptive and indicate the variable’s purpose or usage.
Following this convention ensures consistency, readability, and easier collaboration, especially in a team environment.
Here are some guidelines and examples:
- Descriptive Names: The variable name should clearly indicate its purpose. It’s often better to have a slightly longer, descriptive name than a short, cryptic one.Good:
- Avoid Redundancy: If the context of a variable is clear, avoid using redundant prefixes or suffixes.Good:
ec2_instance_count(if it’s clear that you’re working with EC2 resources)
- Plural Names for Lists/Sets: If a variable is expected to be a list or a set, use a plural name.Good:
- No Uppercase or CamelCase: Stick to lowercase letters and avoid using uppercase or CamelCase.Bad:
Terraform variable names are not case-sensitive, but it is generally considered best practice to use snake case, with all words separated by underscores. This is the most common naming convention used in Terraform, and it is also consistent with the Terraform configuration language itself.
Here are some examples of good Terraform variable names:
You should also avoid using reserved words, single letters, and special characters in your variable names. It is also a good idea to use descriptive names that are easy to understand.
Here are some tips for naming Terraform variables:
- Use all lowercase letters.
- Use underscores to separate words.
- Avoid using special characters.
- Use descriptive names that are easy to understand.
- Avoid using reserved words.
- Avoid using single letters.
- Use abbreviations for common terms.
- Use prefixes to indicate the type of variable. For example, you could use the prefix
account_for variables that are related to AWS accounts.
Katika Terraform, kukubaliwa sana na kupendekezwa jina mkataba kwa ajili ya vigezo ni nyoka_case, ambayo ina maana: - Herufi zote ni ndogo. - Maneno hutenganishwa kwa alama za chini. - Majina inapaswa kuwa descriptive na kuonyesha kusudi variable au matumizi. Kufuata mkataba huu inahakikisha uthabiti, usomaji, na ushirikiano rahisi, hasa katika mazingira ya timu. Hapa ni baadhi ya miongozo na mifano: - Maelezo majina: Jina variable lazima wazi kuonyesha kusudi lake. Mara nyingi ni bora kuwa na jina la muda mrefu zaidi kuliko moja fupi, ya siri. - Kuepuka redundancy: Kama muktadha wa kutofautiana ni wazi, kuepuka kutumia prefixes redundant au suffixes. - Kwa ujumla, ni bora kutumia jina la nyoka, na maneno yote kutenganishwa na underlines. Hii ni kawaida jina mkataba kutumika katika Terraform, na pia ni sambamba na Terraform Configuration lugha yenyewe. Hapa ni baadhi ya mifano ya majina mazuri Terraform variable: Unapaswa pia kuepuka kutumia maneno yaliyohifadhiwa, herufi moja, na wahusika maalum katika majina yako variable. Pia ni wazo zuri kutumia majina ya ufafanuzi ambayo ni rahisi kuelewa. Hapa ni baadhi ya vidokezo kwa ajili ya kutaja Terraform vigezo: - Matumizi ya herufi zote ndogo. - Tumia alama za chini kutenganisha maneno. - Epuka kutumia herufi za pekee. - Tumia majina yanayofafanua mambo ambayo ni rahisi kuelewa. - Epuka kutumia maneno yasiyofaa. - Epuka kutumia herufi moja. - Tumia vifupisho kwa ajili ya maneno ya kawaida. - Matumizi prefixes kuonyesha aina ya variable. Kwa mfano, unaweza kutumia kiambishi cha account_for variables ambazo zinahusiana na akaunti za AWS.