github.com/jpreese/tflint@v0.19.2-0.20200908152133-b01686250fb6/docs/rules/terraform_naming_convention.md (about)

     1  # terraform_naming_convention
     2  
     3  Enforces naming conventions for the following blocks:
     4  
     5  * Resources
     6  * Input variables
     7  * Output values
     8  * Local values
     9  * Modules
    10  * Data sources
    11  
    12  ## Configuration
    13  
    14  Name | Default | Value
    15  --- | --- | ---
    16  enabled | `false` | Boolean
    17  format | `snake_case` | `snake_case`, `mixed_snake_case`, `none` or a custom format defined using the `custom_formats` attribute
    18  custom | `""` | String representation of a golang regular expression that the block name must match
    19  custom_formats | `{}` | Definition of custom formats that can be used in the `format` attribute
    20  data | | Block settings to override naming convention for data sources
    21  locals | | Block settings to override naming convention for local values
    22  module | | Block settings to override naming convention for modules
    23  output | | Block settings to override naming convention for output values
    24  resource | | Block settings to override naming convention for resources
    25  variable | | Block settings to override naming convention for input variables
    26  
    27  
    28  #### `format`
    29  
    30  The `format` option defines the allowed formats for the block label. 
    31  This option accepts one of the following values:
    32  
    33  * `snake_case` - standard snake_case format - all characters must be lower-case, and underscores are allowed.
    34  * `mixed_snake_case` - modified snake_case format - characters may be upper or lower case, and underscores are allowed.
    35  * `none` - signifies "this block shall not have its format checked". This can be useful if you want to enforce no particular format for a block.
    36  
    37  #### `custom`
    38  
    39  The `custom` option defines a custom regex that the identifier must match. This option allows you to have a bit more finer-grained control over identifiers, letting you force certain patterns and substrings.
    40  
    41  #### `custom_formats`
    42  
    43  The `custom_formats` attribute defines additional formats that can be used in the `format` option. Like `custom`, it allows you to define a custom regular expression that the identifier must match, but it also lets you supply a description that will be shown when the check fails. Also, it allows you to reuse a custom regex.
    44  
    45  This attribute is a map, where the keys are the identifiers of the custom formats, and the values are objects with a `regex` and a `description` key.
    46  
    47  ## Examples
    48  
    49  ### Default - enforce snake_case for all blocks
    50  
    51  #### Rule configuration
    52  
    53  ```hcl
    54  rule "terraform_naming_convention" {
    55    enabled = true
    56  }
    57  ```
    58  
    59  #### Sample terraform source file
    60  
    61  ```hcl
    62  data "aws_eip" "camelCase" {
    63  }
    64  
    65  data "aws_eip" "valid_name" {
    66  }
    67  ```
    68  
    69  ```
    70  $ tflint
    71  1 issue(s) found:
    72  
    73  Notice: data name `camelCase` must match the following format: snake_case (terraform_naming_convention)
    74  
    75    on template.tf line 1:
    76     1: data "aws_eip" "camelCase" {
    77  
    78  Reference: https://github.com/terraform-linters/tflint/blob/v0.15.3/docs/rules/terraform_naming_convention.md
    79   
    80  ```
    81  
    82  
    83  ### Custom naming expression for all blocks
    84  
    85  #### Rule configuration
    86  
    87  ```hcl
    88  rule "terraform_naming_convention" {
    89    enabled = true
    90  
    91    custom = "^[a-zA-Z]+([_-][a-zA-Z]+)*$"
    92  }
    93  ```
    94  
    95  #### Sample terraform source file
    96  
    97  ```hcl
    98  resource "aws_eip" "Invalid_Name_With_Number123" {
    99  }
   100  
   101  resource "aws_eip" "Name-With_Dash" {
   102  }
   103  ```
   104  
   105  ```
   106  $ tflint
   107  1 issue(s) found:
   108  
   109  Notice: resource name `Invalid_Name_With_Number123` must match the following RegExp: ^[a-zA-Z]+([_-][a-zA-Z]+)*$ (terraform_naming_convention)
   110  
   111    on template.tf line 1:
   112     1: resource "aws_eip" "Invalid_Name_With_Number123" {
   113  
   114  Reference: https://github.com/terraform-linters/tflint/blob/v0.15.3/docs/rules/terraform_naming_convention.md
   115   
   116  ```
   117  
   118  
   119  ### Custom format for all blocks
   120  
   121  #### Rule configuration
   122  
   123  ```hcl
   124  rule "terraform_naming_convention" {
   125    enabled = true
   126    format = "custom_format"
   127  
   128    custom_formats = {
   129      custom_format = {
   130        description = "Custom Format"
   131        regex       = "^[a-zA-Z]+([_-][a-zA-Z]+)*$"
   132      }
   133    }
   134  }
   135  ```
   136  
   137  #### Sample terraform source file
   138  
   139  ```hcl
   140  resource "aws_eip" "Invalid_Name_With_Number123" {
   141  }
   142  
   143  resource "aws_eip" "Name-With_Dash" {
   144  }
   145  ```
   146  
   147  ```
   148  $ tflint
   149  1 issue(s) found:
   150  
   151  Notice: resource name `Invalid_Name_With_Number123` must match the following format: Custom Format (terraform_naming_convention)
   152  
   153    on template.tf line 1:
   154     1: resource "aws_eip" "Invalid_Name_With_Number123" {
   155  
   156  Reference: https://github.com/terraform-linters/tflint/blob/v0.15.3/docs/rules/terraform_naming_convention.md
   157   
   158  ```
   159  
   160  
   161  ### Override default setting for specific block type
   162  
   163  #### Rule configuration
   164  
   165  ```hcl
   166  rule "terraform_naming_convention" {
   167    enabled = true
   168  
   169    module {
   170      custom = "^[a-zA-Z]+(_[a-zA-Z]+)*$"
   171    }
   172  }
   173  ```
   174  
   175  #### Sample terraform source file
   176  
   177  ```hcl
   178  // data name enforced with default snake_case
   179  data "aws_eip" "eip_1a" {
   180  }
   181  
   182  module "valid_module" {
   183    source = ""
   184  }
   185  
   186  module "invalid_module_with_number_1a" {
   187    source = ""
   188  }
   189  ```
   190  
   191  ```
   192  $ tflint
   193  1 issue(s) found:
   194  
   195  Notice: module name `invalid_module_with_number_1a` must match the following RegExp: ^[a-zA-Z]+(_[a-zA-Z]+)*$ (terraform_naming_convention)
   196  
   197    on template.tf line 9:
   198     9: module "invalid_module_with_number_1a" {
   199  
   200  Reference: https://github.com/terraform-linters/tflint/blob/v0.15.3/docs/rules/terraform_naming_convention.md
   201   
   202  ```
   203  
   204  ### Disable for specific block type
   205  
   206  #### Rule configuration
   207  
   208  ```hcl
   209  rule "terraform_naming_convention" {
   210    enabled = true
   211  
   212    module {
   213      format = "none"
   214    }
   215  }
   216  ```
   217  
   218  #### Sample terraform source file
   219  
   220  ```hcl
   221  // data name enforced with default snake_case
   222  data "aws_eip" "eip_1a" {
   223  }
   224  
   225  // module names will not be enforced
   226  module "Valid_Name-Not-Enforced" {
   227    source = ""
   228  }
   229  ```
   230  
   231  
   232  ### Disable for all blocks but enforce a specific block type
   233  
   234  #### Rule configuration
   235  
   236  ```hcl
   237  rule "terraform_naming_convention" {
   238    enabled = true
   239    format  = "none"
   240  
   241    local {
   242      format = "snake_case"
   243    }
   244  }
   245  ```
   246  
   247  #### Sample terraform source file
   248  
   249  ```hcl
   250  // Data block name not enforced
   251  data "aws_eip" "EIP_1a" {
   252  }
   253  
   254  // Resource block name not enforced
   255  resource "aws_eip" "EIP_1b" {
   256  }
   257  
   258  // local variable names enforced
   259  locals {
   260    valid_name   = "valid"
   261    invalid-name = "dashes are not allowed with snake_case"
   262  }
   263  ```
   264  
   265  ```
   266  $ tflint
   267  1 issue(s) found:
   268  
   269  Notice: local value name `invalid-name` must match the following format: snake_case (terraform_naming_convention)
   270  
   271    on template.tf line 12:
   272    12: invalid-name = "dashes are not allowed with snake_case"
   273  
   274  Reference: https://github.com/terraform-linters/tflint/blob/v0.15.3/docs/rules/terraform_naming_convention.md
   275   
   276  ```
   277  
   278  ## Why
   279  
   280  Naming conventions are optional, so it is not necessary to follow this. 
   281  But this rule is useful if you want to force the following naming conventions in line with the [Terraform Plugin Naming Best Practices](https://www.terraform.io/docs/extend/best-practices/naming.html).
   282  
   283  ## How To Fix
   284  
   285  Update the block label according to the format or custom regular expression.