HashiCorp Terraform 0.4

HashiCorp Terraform 0.4

Apr 02 2015 Mitchell Hashimoto

We are releasing Terraform 0.4. Terraform is a tool for safely and efficiently building, combining, and launching infrastructure.

Terraform 0.4 is our biggest release ever. We're shipping with multiple major features as well as a vast improvement to support for AWS. Terraform 0.4 had the most community involvement ever with 80 contributors, including new core committers Paul Hinze and Clint Shryock.

Feature highlights for Terraform 0.4:

resource "docker_image" "ubuntu" { name = "ubuntu:latest" }

The major features of configuring Docker containers are supported with this release, but many of the tunables are not yet supported. We'll be adding these in upcoming releases.

The Docker provider is fully documented here.

New Provider: OpenStack

» Create a web server

resource "openstack_compute_instance_v2" "test-server" { ... }

The OpenStack provider is fully documented here.

Feature: Targeted Operations

Targeted operations is a new feature in Terraform to enhance the workflow and give operators more control to ensure that only changes they want to happen will happen. Targeted operations allow users to now target one or more resources with a refresh, plan, apply, or destroy. This means that only the resources targeted will be modified, even if others would need to be modified to reach your complete desired state.

As an example use case: an operator may make major changes to the Terraform configuration, but you only want to roll out the web server updates now. You can now do this with targeted operations, delaying the other changes until a future time.

The syntax is shown below:

$ terraform apply -target=aws_instance.web ...

Targeted operations are available on refresh, plan, apply, and destroy.

Feature: Remote Modules

resource "aws_instance" "foo" {

# ...
subnet_id = "${terraform_state.vpc.output.subnet_id}"

}

As you can see from the above example, this allows separate teams to maintain different parts of the infrastructure, and for other teams to access these.

Command: Taint

Terraform 0.4 introduces a new top-level CLI command: terraform taint.

The taint command enables you to explicitly taint a resource. Tainted resources are marked for destroy/recreate on the next plan or apply. This lets you force recreate existing resources that Terraform might have otherwise not have changed.

There are a few use cases for this. One use case is to force the re-running of provisioners, which only run on creation time within Terraform. Another is to simply recycle the resource, such as potentially getting better hardware on Amazon EC2.

Prior to the taint command, you'd have to remove the resource from the config, then re-add it. This is now all possible from the command-line. Syntax example is shown below:

$ terraform taint aws_instance.web ...

The taint command documentation is here.

Updates to Terraform Configuration

The Terraform configuration received some major improvements in Terraform 0.4: math operations are now available, a handful of new functions have been introduced, and a new type of variable known as a "self" variable exists.

Math operations let you do math within configuration interpolations. For example, count.index is typically zero-indexed, but if you wanted to name your instances starting with a one-indexed number, it is now trivial:

resource "aws_instance" "web" { count = 5 tags { Name = "web-${count.index+1}" } }

Math operations aren't limited to literal numbers. You can also do things like this: ${count.index+aws_instance.tags.number}. This will result in a runtime error if the value can't be converted to a number.

In addition to math operations, three new functions are now available in the configuration: format, replace, and split. Each probably does what you expect and is documented. split is a notable adition to the configuration, because along with join (introduced in 0.3), it is now possible to transfer lists of data between modules and as variable inputs to a Terraform configuration.

We'd still like to add first-class support for lists at some point but this is a strong workaround in the meantime.

Finally, self variables are new to Terraform configurations. Self variables let you reference the attributes of your own resource. These are currently only available within provisioners. Example:

resource "aws_instance" "web" { provisioner "local-exec" { command = "echo ${self.private_ip_address}" } }

Self variables solve the problem of referencing your own resource in provisioners in both single and multi-count cases without creating a cycle in the Terraform graph.

AWS Improvements and Features

For the duration of Terraform 0.4 development, one core comitter has worked full time on improving AWS support. This effort has been very successful, with a number of impactful changes:

  • Tagging support for AWS resources in Terraform.

  • Focus on bug fixes and features to make sure the load balancer, autoscale group, launch configuration workflow works really well.

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×