github.com/hugorut/terraform@v1.1.3/website/docs/language/upgrade-guides/0-7.mdx (about) 1 --- 2 page_title: Upgrading to Terraform 0.7 3 description: Upgrading to Terraform v0.7 4 --- 5 6 # Upgrading to Terraform v0.7 7 8 Terraform v0.7 is a major release, and thus includes some backwards incompatibilities that you'll need to consider when upgrading. This guide is meant to help with that process. 9 10 The goal of this guide is to cover the most common upgrade concerns and issues that would benefit from more explanation and background. The exhaustive list of changes will always be the [Terraform Changelog](https://github.com/hugorut/terraform/blob/main/CHANGELOG.md). After reviewing this guide, review the Changelog to check on specific notes about the resources and providers you use. 11 12 ## Plugin Binaries 13 14 Before v0.7, Terraform's built-in plugins for providers and provisioners were each distributed as separate binaries. 15 16 ``` 17 terraform # core binary 18 terraform-provider-* # provider plugins 19 terraform-provisioner-* # provisioner plugins 20 ``` 21 22 These binaries needed to all be extracted to somewhere in your `$PATH` or in the `~/.terraform.d` directory for Terraform to work. 23 24 As of v0.7, all built-in plugins ship embedded in a single binary. This means that if you just extract the v0.7 archive into a path, you may still have the old separate binaries in your `$PATH`. You'll need to remove them manually. 25 26 For example, if you keep Terraform binaries in `/usr/local/bin` you can clear out the old external binaries like this: 27 28 ``` 29 rm /usr/local/bin/terraform-* 30 ``` 31 32 External plugin binaries continue to work using the same pattern, but due to updates to the RPC protocol, they will need to be recompiled to be compatible with Terraform v0.7.x. 33 34 ## Maps in Displayed Plans 35 36 When displaying a plan, Terraform now distinguishes attributes of type map by using a `%` character for the "length field". 37 38 Here is an example showing a diff that includes both a list and a map: 39 40 ``` 41 somelist.#: "0" => "1" 42 somelist.0: "" => "someitem" 43 somemap.%: "0" => "1" 44 somemap.foo: "" => "bar" 45 ``` 46 47 ## Interpolation Changes 48 49 There are a few changes to Terraform's interpolation language that may require updates to your configs. 50 51 ### String Concatenation 52 53 The `concat()` interpolation function used to work for both lists and strings. It now only works for lists. 54 55 ``` 56 "${concat(var.foo, "-suffix")}" # => Error! No longer supported. 57 ``` 58 59 Instead, you can use variable interpolation for string concatenation. 60 61 ``` 62 "${var.foo}-suffix" 63 ``` 64 65 ### Nested Quotes and Escaping 66 67 Escaped quotes inside of interpolations were supported to retain backwards compatibility with older versions of Terraform that allowed them. 68 69 Now, escaped quotes will no longer work in the interpolation context: 70 71 ``` 72 "${lookup(var.somemap, \"somekey\")}" # => Syntax Error! 73 ``` 74 75 Instead, treat each set of interpolation braces (`${}`) as a new quoting context: 76 77 ``` 78 "${lookup(var.somemap, "somekey")}" 79 ``` 80 81 This allows double quote characters to be expressed properly within strings inside of interpolation expressions: 82 83 ``` 84 "${upper("\"quoted\"")}" # => "QUOTED" 85 ``` 86 87 ## Safer `terraform plan` Behavior 88 89 Prior to v0.7, the `terraform plan` command had the potential to write updates to the state if changes were detected during the Refresh step (which happens by default during `plan`). Some configurations have metadata that changes with every read, so Refresh would always result in changes to the state, and therefore a write. 90 91 In collaborative environments with shared remote state, this potential side effect of `plan` would cause unnecessary contention over the state, and potentially even interfere with active `apply` operations if they were happening simultaneously elsewhere. 92 93 Terraform v0.7 addresses this by changing the Refresh process that is run during `terraform plan` to always be an in-memory only refresh. New state information detected during this step will not be persisted to permanent state storage. 94 95 If the `-out` flag is used to produce a Plan File, the updated state information _will_ be encoded into that file, so that the resulting `terraform apply` operation can detect if any changes occurred that might invalidate the plan. 96 97 For most users, this change will not affect your day-to-day usage of Terraform. For users with automation that relies on the old side effect of `plan`, you can use the `terraform refresh` command, which will still persist any changes it discovers. 98 99 ## Migrating to Data Sources 100 101 With the addition of [Data Sources](/language/data-sources), there are several resources that were acting as Data Sources that are now deprecated. Existing configurations will continue to work, but will print a deprecation warning when a data source is used as a resource. 102 103 - `atlas_artifact` 104 - `template_file` 105 - `template_cloudinit_config` 106 - `tls_cert_request` 107 108 Migrating to the equivalent Data Source is as simple as changing the `resource` keyword to `data` in your declaration and prepending `data.` to attribute references elsewhere in your config. 109 110 For example, given a config like: 111 112 ``` 113 resource "template_file" "example" { 114 template = "someconfig" 115 } 116 resource "aws_instance" "example" { 117 user_data = "${template_file.example.rendered}" 118 # ... 119 } 120 ``` 121 122 A config using the equivalent Data Source would look like this: 123 124 ``` 125 data "template_file" "example" { 126 template = "someconfig" 127 } 128 resource "aws_instance" "example" { 129 user_data = "${data.template_file.example.rendered}" 130 # ... 131 } 132 ``` 133 134 Referencing remote state outputs has also changed. The `.output` keyword is no longer required. 135 136 For example, a config like this: 137 138 ``` 139 resource "terraform_remote_state" "example" { 140 # ... 141 } 142 143 resource "aws_instance" "example" { 144 ami = "${terraform_remote_state.example.output.ami_id}" 145 # ... 146 } 147 ``` 148 149 Would now look like this: 150 151 ``` 152 data "terraform_remote_state" "example" { 153 # ... 154 } 155 156 resource "aws_instance" "example" { 157 ami = "${data.terraform_remote_state.example.ami_id}" 158 # ... 159 } 160 ``` 161 162 <a id="listmap"></a> 163 164 ## Migrating to native lists and maps 165 166 Terraform 0.7 now supports lists and maps as first-class constructs. Although the patterns commonly used in previous versions still work (excepting any compatibility notes), there are now patterns with cleaner syntax available. 167 168 For example, a common pattern for exporting a list of values from a module was to use an output with a `join()` interpolation, like this: 169 170 ``` 171 output "private_subnets" { 172 value = "${join(",", aws_subnet.private.*.id)}" 173 } 174 ``` 175 176 When using the value produced by this output in another module, a corresponding `split()` would be used to retrieve individual elements, often parameterized by `count.index`, for example: 177 178 ``` 179 subnet_id = "${element(split(",", var.private_subnets), count.index)}" 180 ``` 181 182 Using Terraform 0.7, list values can now be passed between modules directly. The above example can read like this for the output: 183 184 ``` 185 output "private_subnets" { 186 value = ["${aws_subnet.private.*.id}"] 187 } 188 ``` 189 190 And then when passed to another module as a `list` type variable, we can index directly using `[]` syntax: 191 192 ``` 193 subnet_id = "${var.private_subnets[count.index]}" 194 ``` 195 196 Note that indexing syntax does not wrap around if the extent of a list is reached - for example if you are trying to distribute 10 instances across three private subnets. For this behaviour, `element` can still be used: 197 198 ``` 199 subnet_id = "${element(var.private_subnets, count.index)}" 200 ``` 201 202 ## Map value overrides 203 204 Previously, individual elements in a map could be overridden by using a dot notation. For example, if the following variable was declared: 205 206 ``` 207 variable "amis" { 208 type = "map" 209 default = { 210 us-east-1 = "ami-123456" 211 us-west-2 = "ami-456789" 212 eu-west-1 = "ami-789123" 213 } 214 } 215 ``` 216 217 The key "us-west-2" could be overridden using `-var "amis.us-west-2=overridden_value"` (or equivalent in an environment variable or `tfvars` file). The syntax for this has now changed - instead maps from the command line will be merged with the default value, with maps from flags taking precedence. The syntax for overriding individual values is now: 218 219 ``` 220 -var 'amis = { us-west-2 = "overridden_value" }' 221 ``` 222 223 This will give the map the effective value: 224 225 ``` 226 { 227 us-east-1 = "ami-123456" 228 us-west-2 = "overridden_value" 229 eu-west-1 = "ami-789123" 230 } 231 ``` 232 233 It's also possible to override the values in a variables file, either in any `terraform.tfvars` file, an `.auto.tfvars` file, or specified using the `-var-file` flag.