github.com/muratcelep/terraform@v1.1.0-beta2-not-internal-4/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 The output includes a `format_version` key, which as of Terraform 1.1.0 has 61 value `"1.0"`. The semantics of this version are: 62 63 - We will increment the minor version, e.g. `"1.1"`, for backward-compatible 64 changes or additions. Ignore any object properties with unrecognized names to 65 remain forward-compatible with future minor versions. 66 - We will increment the major version, e.g. `"2.0"`, for changes that are not 67 backward-compatible. Reject any input which reports an unsupported major 68 version. 69 70 We will introduce new major versions only within the bounds of 71 [the Terraform 1.0 Compatibility Promises](https://www.terraform.io/docs/language/v1-compatibility-promises.html). 72 73 In the normal case, Terraform will print a JSON object to the standard output 74 stream. The top-level JSON object will have the following properties: 75 76 * `valid` (boolean): Summarizes the overall validation result, by indicating 77 `true` if Terraform considers the current configuration to be valid or 78 `false` if it detected any errors. 79 80 * `error_count` (number): A zero or positive whole number giving the count 81 of errors Terraform detected. If `valid` is `true` then `error_count` will 82 always be zero, because it is the presence of errors that indicates that 83 a configuration is invalid. 84 85 * `warning_count` (number): A zero or positive whole number giving the count 86 of warnings Terraform detected. Warnings do not cause Terraform to consider 87 a configuration to be invalid, but they do indicate potential caveats that 88 a user should consider and possibly resolve. 89 90 * `diagnostics` (array of objects): A JSON array of nested objects that each 91 describe an error or warning from Terraform. 92 93 The nested objects in `diagnostics` have the following properties: 94 95 * `severity` (string): A string keyword, currently either `"error"` or 96 `"warning"`, indicating the diagnostic severity. 97 98 The presence of errors causes Terraform to consider a configuration to be 99 invalid, while warnings are just advice or caveats to the user which do not 100 block working with the configuration. Later versions of Terraform may 101 introduce new severity keywords, so consumers should be prepared to accept 102 and ignore severity values they don't understand. 103 104 * `summary` (string): A short description of the nature of the problem that 105 the diagnostic is reporting. 106 107 In Terraform's usual human-oriented diagnostic messages, the summary serves 108 as a sort of "heading" for the diagnostic, printed after the "Error:" or 109 "Warning:" indicator. 110 111 Summaries are typically short, single sentences, but can sometimes be longer 112 as a result of returning errors from subsystems that are not designed to 113 return full diagnostics, where the entire error message therefore becomes the 114 summary. In those cases, the summary might include newline characters which 115 a renderer should honor when presenting the message visually to a user. 116 117 * `detail` (string): An optional additional message giving more detail about 118 the problem. 119 120 In Terraform's usual human-oriented diagnostic messages, the detail provides 121 the paragraphs of text that appear after the heading and the source location 122 reference. 123 124 Detail messages are often multiple paragraphs and possibly interspersed with 125 non-paragraph lines, so tools which aim to present detail messages to the 126 user should distinguish between lines without leading spaces, treating them 127 as paragraphs, and lines with leading spaces, treating them as preformatted 128 text. Renderers should then soft-wrap the paragraphs to fit the width of the 129 rendering container, but leave the preformatted lines unwrapped. 130 131 Some Terraform detail messages currently contain an approximation of bullet 132 lists using ASCII characters to mark the bullets. This is not currently a 133 contractural formatting convention and so renderers should avoid depending on 134 it and should instead treat those lines as either paragraphs or preformatted 135 text per the rules above. A future version of this format may define some 136 additional rules for processing other text conventions, but will do so within 137 the bounds of the rules above to achieve backward-compatibility. 138 139 * `range` (object): An optional object referencing a portion of the configuration 140 source code that the diagnostic message relates to. For errors, this will 141 typically indicate the bounds of the specific block header, attribute, or 142 expression which was detected as invalid. 143 144 A source range is an object with a property `filename` which gives the 145 filename as a relative path from the current working directory, and then 146 two properties `start` and `end` which are both themselves objects 147 describing source positions, as described below. 148 149 Not all diagnostic messages are connected with specific portions of the 150 configuration, so `range` will be omitted or `null` for diagnostic messages 151 where it isn't relevant. 152 153 * `snippet` (object): An optional object including an excerpt of the 154 configuration source code that the diagnostic message relates to. 155 156 The snippet information includes: 157 158 * `context` (string): An optional summary of the root context of the 159 diagnostic. For example, this might be the resource block containing the 160 expression which triggered the diagnostic. For some diagnostics this 161 information is not available, and then this property will be `null`. 162 163 * `code` (string): A snippet of Terraform configuration including the 164 source of the diagnostic. This can be multiple lines and may include 165 additional configuration source code around the expression which 166 triggered the diagnostic. 167 168 * `start_line` (number): A one-based line count representing the position 169 in the source file at which the `code` excerpt begins. This is not 170 necessarily the same value as `range.start.line`, as it is possible for 171 `code` to include one or more lines of context before the source of the 172 diagnostic. 173 174 * `highlight_start_offset` (number): A zero-based character offset into the 175 `code` string, pointing at the start of the expression which triggered 176 the diagnostic. 177 178 * `highlight_end_offset` (number): A zero-based character offset into the 179 `code` string, pointing at the end of the expression which triggered the 180 diagnostic. 181 182 * `values` (array of objects): Contains zero or more expression values 183 which may be useful in understanding the source of a diagnostic in a 184 complex expression. These expression value objects are described below. 185 186 ### Source Position 187 188 A source position object, as used in the `range` property of a diagnostic 189 object, has the following properties: 190 191 * `byte` (number): A zero-based byte offset into the indicated file. 192 193 * `line` (number): A one-based line count for the line containing the relevant 194 position in the indicated file. 195 196 * `column` (number): A one-based count of _Unicode characters_ from the start 197 of the line indicated in `line`. 198 199 A `start` position is inclusive while an `end` position is exclusive. The 200 exact positions used for particular error messages are intended for human 201 interpretation only and subject to change in future versions of Terraform due 202 either to improvements to the error reporting or changes in implementation 203 details of the language parser/evaluator. 204 205 ### Expression Value 206 207 An expression value object gives additional information about a value which is 208 part of the expression which triggered the diagnostic. This is especially 209 useful when using `for_each` or similar constructs, in order to identify 210 exactly which values are responsible for an error. The object has two properties: 211 212 * `traversal` (string): An HCL-like traversal string, such as 213 `var.instance_count`. Complex index key values may be elided, so this will 214 not always be valid, parseable HCL. The contents of this string are intended 215 to be human-readable and are subject to change in future versions of 216 Terraform. 217 218 * `statement` (string): A short English-language fragment describing the value 219 of the expression when the diagnostic was triggered. The contents of this 220 string are intended to be human-readable and are subject to change in future 221 versions of Terraform.