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