Terraform provides an elegant user experience for operators to safely and predictably make changes to infrastructure. Terraform is distributed as a binary package for many supported platforms and architectures.
Installing Terraform
To install Terraform, after downloading the appropriate version of Terraform, unzip the package. Terraform runs as a single binary named terraform
.
The final step is to make sure that the binaryterraform
is available on the environment path. See thisfor instructions on setting the PATH on Linux and Mac.
Verifying the Installation
After installing Terraform, verify the installation worked by opening a new terminal session and checking that terraform
is available. By executing terraform
you should see help output similar to this:
$ terraform
Usage: terraform [--version] [--help] <command> [args]
The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
Common commands:
apply Builds or changes infrastructure
console Interactive console for Terraform interpolations
# ...
If you get an error terraform
PATH
PATH
Building Infrastructure
Terraform can manage many providers, including multiple providers in a single configuration. Let’s start creating an EC2 instance on AWS.
Caution! If you’re not using an account that qualifies under the AWS free-tier, you may be charged to run
Configuration
We’re going to write our first configuration now to launch a single AWS EC2 instance. The files used to describe any infrastructure in Terraform is simply known as a Terraform configuration.
Create a text file and save the contents to a file example.tf
*.tf
provider "aws" {
access_key = "ACCESS_KEY_HERE"
secret_key = "SECRET_KEY_HERE"
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-2757f631"
instance_type = "t2.micro"
}
Note: If you use a region other us-east-1
ACCESS_KEY_HERE
SECRET_KEY_HERE
Provider
block is used to configure the named provider, in our case “
Resource
block defines a resource that exists within the infrastructure. A resource might be a physical component such as an EC2 instance, or it can be a logical resource such as a Heroku application.
The resource block has two strings before opening the block: the resource type and the resource name. In our example, the resource type is “aws_instance” and the name is “example.” The prefix of the type maps to the provider. In our case “aws_instance” automatically tells Terraform that it is managed by the “aws” provider.
Within the resource
Initialization
The first command to run for a new configuration — or after checking out an existing configuration from version control — is terraform init
, which initializes various local settings and data that will be used by subsequent commands.
$ terraform init
Initializing the backend...
Initializing provider plugins...
- downloading plugin for provider "aws"...
The following providers do not have any version constraints in configuration,
so the latest version was installed.
To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.
* provider.aws: version = "~> 1.0"
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your environment. If you forget, other
commands will detect it and remind you to do so if necessary.
Apply Changes
In the same directory as the example.tf
file you created, run terraform apply
. You should see output similar to below, though we’ve truncated some of the output to save space:
$ terraform apply
# ...
+ aws_instance.example
ami: "ami-2757f631"
availability_zone: "<computed>"
ebs_block_device.#: "<computed>"
ephemeral_block_device.#: "<computed>"
instance_state: "<computed>"
instance_type: "t2.micro"
key_name: "<computed>"
placement_group: "<computed>"
private_dns: "<computed>"
private_ip: "<computed>"
public_dns: "<computed>"
public_ip: "<computed>"
root_block_device.#: "<computed>"
security_groups.#: "<computed>"
source_dest_check: "true"
subnet_id: "<computed>"
tenancy: "<computed>"
vpc_security_group_ids.#: "<computed>"
This output shows the execution plan, describing which actions Terraform will take in order to change real infrastructure to match the configuration. The output format is similar to the diff format generated by tools such as Git. The output has a +
next aws_instance.example
If terraform apply
failed with an error, read the error message and fix the error that occurred. At this stage, it is likely to be a syntax error in the configuration.
If the plan was created successfully, Terraform will now pause and wait for approval before proceeding. If anything in the plan seems incorrect or dangerous, it is safe to abort here with no changes made to your infrastructure. In this case the plan looks acceptable, so type yes
at the confirmation prompt to proceed.
Executing the plan will take a few minutes since Terraform waits for the EC2 instance to become available:
# ...
aws_instance.example: Creating...
ami: "" => "ami-2757f631"
instance_type: "" => "t2.micro"
[...]
aws_instance.example: Still creating... (10s elapsed)
aws_instance.example: Creation complete
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
# ...
After this, Terraform is all done! You can go to the EC2 console to see the created EC2 instance. Terraform also wrote some data into the terraform.tfstate
file. This state file is extremely important; it keeps track of the IDs of created resources so that Terraform knows what it is managing. This file must be saved and distributed to anyone who might run Terraform. It is generally recommended to setup remote state when working with Terraform, to share the state automatically, but this is not necessary for simple situations like this Getting Started guide.
You can inspect the current state using terraform show
:
$ terraform show
aws_instance.example:
id = i-32cf65a8
ami = ami-2757f631
availability_zone = us-east-1a
instance_state = running
instance_type = t2.micro
private_ip = 172.31.30.244
public_dns = ec2-52-90-212-55.compute-1.amazonaws.com
public_ip = 52.90.212.55
subnet_id = subnet-1497024d
vpc_security_group_ids.# = 1
vpc_security_group_ids.3348721628 = sg-67652003
While creating our resource, we’ve also gathered some information about it which will be referenced to configure other resources or outputs.
With this you have successfully installed and configured terraform to provision a EC2 instance on AWS.