github.com/hugorut/terraform@v1.1.3/website/docs/cli/commands/validate.mdx (about)

     1  ---
     2  page_title: 'Command: validate'
     3  description: >-
     4    The `terraform validate` command is used to validate the syntax of the
     5    terraform files.
     6  ---
     7  
     8  # Command: validate
     9  
    10  The `terraform validate` command validates the configuration files in a
    11  directory, referring only to the configuration and not accessing any remote
    12  services such as remote state, provider APIs, etc.
    13  
    14  Validate runs checks that verify whether a configuration is syntactically
    15  valid and internally consistent, regardless of any provided variables or
    16  existing state. It is thus primarily useful for general verification of
    17  reusable modules, including correctness of attribute names and value types.
    18  
    19  It is safe to run this command automatically, for example as a post-save
    20  check in a text editor or as a test step for a re-usable module in a CI
    21  system.
    22  
    23  Validation requires an initialized working directory with any referenced
    24  plugins and modules installed. To initialize a working directory for
    25  validation without accessing any configured remote backend, use:
    26  
    27  ```
    28  $ terraform init -backend=false
    29  ```
    30  
    31  To verify configuration in the context of a particular run (a particular
    32  target workspace, input variable values, etc), use the `terraform plan`
    33  command instead, which includes an implied validation check.
    34  
    35  ## Usage
    36  
    37  Usage: `terraform validate [options]`
    38  
    39  This command accepts the following options:
    40  
    41  * `-json` - Produce output in a machine-readable JSON format, suitable for
    42    use in text editor integrations and other automated systems. Always disables
    43    color.
    44  
    45  * `-no-color` - If specified, output won't contain any color.
    46  
    47  ## JSON Output Format
    48  
    49  When you use the `-json` option, Terraform will produce validation results
    50  in JSON format to allow using the validation result for tool integrations, such
    51  as highlighting errors in a text editor.
    52  
    53  As with all JSON output options, it's possible that Terraform will encounter
    54  an error prior to beginning the validation task that will thus not be subject
    55  to the JSON output setting. For that reason, external software consuming
    56  Terraform's output should be prepared to find data on stdout that _isn't_ valid
    57  JSON, which it should then treat as a generic error case.
    58  
    59  The output includes a `format_version` key, which as of Terraform 1.1.0 has
    60  value `"1.0"`. The semantics of this version are:
    61  
    62  * We will increment the minor version, e.g. `"1.1"`, for backward-compatible
    63    changes or additions. Ignore any object properties with unrecognized names to
    64    remain forward-compatible with future minor versions.
    65  * We will increment the major version, e.g. `"2.0"`, for changes that are not
    66    backward-compatible. Reject any input which reports an unsupported major
    67    version.
    68  
    69  We will introduce new major versions only within the bounds of
    70  [the Terraform 1.0 Compatibility Promises](/language/v1-compatibility-promises).
    71  
    72  In the normal case, Terraform will print a JSON object to the standard output
    73  stream. The top-level JSON object will have the following properties:
    74  
    75  * `valid` (boolean): Summarizes the overall validation result, by indicating
    76    `true` if Terraform considers the current configuration to be valid or
    77    `false` if it detected any errors.
    78  
    79  * `error_count` (number): A zero or positive whole number giving the count
    80    of errors Terraform detected. If `valid` is `true` then `error_count` will
    81    always be zero, because it is the presence of errors that indicates that
    82    a configuration is invalid.
    83  
    84  * `warning_count` (number): A zero or positive whole number giving the count
    85    of warnings Terraform detected. Warnings do not cause Terraform to consider
    86    a configuration to be invalid, but they do indicate potential caveats that
    87    a user should consider and possibly resolve.
    88  
    89  * `diagnostics` (array of objects): A JSON array of nested objects that each
    90    describe an error or warning from Terraform.
    91  
    92  The nested objects in `diagnostics` have the following properties:
    93  
    94  * `severity` (string): A string keyword, currently either `"error"` or
    95    `"warning"`, indicating the diagnostic severity.
    96  
    97    The presence of errors causes Terraform to consider a configuration to be
    98    invalid, while warnings are just advice or caveats to the user which do not
    99    block working with the configuration. Later versions of Terraform may
   100    introduce new severity keywords, so consumers should be prepared to accept
   101    and ignore severity values they don't understand.
   102  
   103  * `summary` (string): A short description of the nature of the problem that
   104    the diagnostic is reporting.
   105  
   106    In Terraform's usual human-oriented diagnostic messages, the summary serves
   107    as a sort of "heading" for the diagnostic, printed after the "Error:" or
   108    "Warning:" indicator.
   109  
   110    Summaries are typically short, single sentences, but can sometimes be longer
   111    as a result of returning errors from subsystems that are not designed to
   112    return full diagnostics, where the entire error message therefore becomes the
   113    summary. In those cases, the summary might include newline characters which
   114    a renderer should honor when presenting the message visually to a user.
   115  
   116  * `detail` (string): An optional additional message giving more detail about
   117    the problem.
   118  
   119    In Terraform's usual human-oriented diagnostic messages, the detail provides
   120    the paragraphs of text that appear after the heading and the source location
   121    reference.
   122  
   123    Detail messages are often multiple paragraphs and possibly interspersed with
   124    non-paragraph lines, so tools which aim to present detail messages to the
   125    user should distinguish between lines without leading spaces, treating them
   126    as paragraphs, and lines with leading spaces, treating them as preformatted
   127    text. Renderers should then soft-wrap the paragraphs to fit the width of the
   128    rendering container, but leave the preformatted lines unwrapped.
   129  
   130    Some Terraform detail messages currently contain an approximation of bullet
   131    lists using ASCII characters to mark the bullets. This is not currently a
   132    contractural formatting convention and so renderers should avoid depending on
   133    it and should instead treat those lines as either paragraphs or preformatted
   134    text per the rules above. A future version of this format may define some
   135    additional rules for processing other text conventions, but will do so within
   136    the bounds of the rules above to achieve backward-compatibility.
   137  
   138  * `range` (object): An optional object referencing a portion of the configuration
   139    source code that the diagnostic message relates to. For errors, this will
   140    typically indicate the bounds of the specific block header, attribute, or
   141    expression which was detected as invalid.
   142  
   143    A source range is an object with a property `filename` which gives the
   144    filename as a relative path from the current working directory, and then
   145    two properties `start` and `end` which are both themselves objects
   146    describing source positions, as described below.
   147  
   148    Not all diagnostic messages are connected with specific portions of the
   149    configuration, so `range` will be omitted or `null` for diagnostic messages
   150    where it isn't relevant.
   151  
   152  * `snippet` (object): An optional object including an excerpt of the
   153    configuration source code that the diagnostic message relates to.
   154  
   155    The snippet information includes:
   156  
   157    * `context` (string): An optional summary of the root context of the
   158      diagnostic. For example, this might be the resource block containing the
   159      expression which triggered the diagnostic. For some diagnostics this
   160      information is not available, and then this property will be `null`.
   161  
   162    * `code` (string): A snippet of Terraform configuration including the
   163      source of the diagnostic. This can be multiple lines and may include
   164      additional configuration source code around the expression which
   165      triggered the diagnostic.
   166  
   167    * `start_line` (number): A one-based line count representing the position
   168      in the source file at which the `code` excerpt begins. This is not
   169      necessarily the same value as `range.start.line`, as it is possible for
   170      `code` to include one or more lines of context before the source of the
   171      diagnostic.
   172  
   173    * `highlight_start_offset` (number): A zero-based character offset into the
   174      `code` string, pointing at the start of the expression which triggered
   175      the diagnostic.
   176  
   177    * `highlight_end_offset` (number): A zero-based character offset into the
   178      `code` string, pointing at the end of the expression which triggered the
   179      diagnostic.
   180  
   181    * `values` (array of objects): Contains zero or more expression values
   182      which may be useful in understanding the source of a diagnostic in a
   183      complex expression. These expression value objects are described below.
   184  
   185  ### Source Position
   186  
   187  A source position object, as used in the `range` property of a diagnostic
   188  object, has the following properties:
   189  
   190  * `byte` (number): A zero-based byte offset into the indicated file.
   191  
   192  * `line` (number): A one-based line count for the line containing the relevant
   193    position in the indicated file.
   194  
   195  * `column` (number): A one-based count of _Unicode characters_ from the start
   196    of the line indicated in `line`.
   197  
   198  A `start` position is inclusive while an `end` position is exclusive. The
   199  exact positions used for particular error messages are intended for human
   200  interpretation only and subject to change in future versions of Terraform due
   201  either to improvements to the error reporting or changes in implementation
   202  details of the language parser/evaluator.
   203  
   204  ### Expression Value
   205  
   206  An expression value object gives additional information about a value which is
   207  part of the expression which triggered the diagnostic. This is especially
   208  useful when using `for_each` or similar constructs, in order to identify
   209  exactly which values are responsible for an error. The object has two properties:
   210  
   211  * `traversal` (string): An HCL-like traversal string, such as
   212    `var.instance_count`. Complex index key values may be elided, so this will
   213    not always be valid, parseable HCL. The contents of this string are intended
   214    to be human-readable and are subject to change in future versions of
   215    Terraform.
   216  
   217  * `statement` (string): A short English-language fragment describing the value
   218    of the expression when the diagnostic was triggered. The contents of this
   219    string are intended to be human-readable and are subject to change in future
   220    versions of Terraform.