Terraform provisions and manages cloud resources across providers
Think Ansible, but for cloud resources rather than just servers
And a whooole lot more...
Terraform currently supports ~300 different resources for AWS alone
Configurations are written in HCL, a human friendly configuration language that transpiles to JSON
Let's start by configuring a provider:
provider "aws" {
region = "us-east-1"
}
Terraform will automatically read our AWS credentials from the environment so there's no need to specify anything else
Let's grab the latest Ubuntu 14.04 AMI from Canonical:
data "aws_ami" "ubuntu" {
most_recent = true
owners = [
"099720109477", # Canonical; look ma, comments!
]
filter {
name = "name"
values = [
"ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*",
]
}
}
Now, spin up a t2.micro
instance:
resource "aws_instance" "hello_world" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
tags {
Name = "HelloWorld"
}
}
Finally, let's go ahead and bootstrap the instance:
resource "aws_instance" "hello-world" {
...
provisioner "file" {
source = "bootstrap.sh"
destination = "/tmp/bootstrap.sh"
}
provisioner "remote-exec" {
inline = [
"chmod +x /tmp/bootstrap.sh",
"/tmp/bootstrap.sh --id ${aws_instance.hello_world.id}",
]
}
}
With the configuration in place, we can spin up the infrastructure
The first step is to create an execution plan by running terraform plan
:
+ aws_instance.hello_world
ami: "ami-49c9295f"
instance_type: "t2.micro"
source_dest_check: "true"
tags.%: "1"
tags.Name: "HelloWorld"
associate_public_ip_address: "<computed>"
...
Plan: 1 to add, 0 to change, 0 to destroy.
The plan will tell exactly what Terraform intends to do
When happy with the execution plan, apply it using terraform apply
Once applied, Terraform will keep track of the current infrastructure state in a .tfstate
file
This file can be checked into version control to allow collaborating on the state
When you want to change your infrastructure, just rinse and repeat the plan and apply steps
Infrastructure configuration can be version controlled and code reviewed
Plan your changes, review the execution plan, and finally apply it
All configuration is encapsulated in code and can easily be applied to a clean environment
Shared configuration can be separated into modules and re-used across projects
Terraform is provider agnostic and integrates with as many or as few providers as needed