Manage infrastructure with Terraform
Manage infrastructure with Terraform
Hashicorp's Terraform is a tool designed for creating, managing, updating, and versioning reproducible application infrastructure. Application infrastructure is composed of all physical and virtual resources (including compute resources and upstack services) which support the flow, storage, processing, and analysis of data.
Terraform can manage all three types of Triton compute resources as first order objects:
Bare metal Docker containers. These run the Docker images you expect, but without complication of having to run them in a virtual machine or prepare the infrastructure first.
Infrastructure containers. These work like hardware virtual machines, but perform like the bare metal containers they are.
Hardware virtual machines. These allow flexibility to run Windows or other non-Linux operating systems.
Follow Terraform's installation instructions.
Verify the installation by opening a new terminal session. Execute terraform
and you should see a help output similar to this:
If you receive an error that terraform
could not be found, PATH was not properly set up.
Open your terminal and run the following command:
You can also symlink to terraform
:
Go to: Control Panel -> System -> Advanced System settings* -> Environment Variables.
Scroll down in system variables until you find PATH. Click edit and change accordingly. You will need to launch a new console for the settings to take effect.
To ensure your application is shareable and version controlled, it's important to include input variables in your Terraform configuration.
Create a variables file. This file can be named anything, since Terraform loads all files ending in .tf in a directory. The most common file name is variables.tf
.
Variables may include login credentials or secret keys. Variables can also include image names, package names, version numbers, counts, and more.
Here is an example of variables including descriptions for each:
Modules are self-contained packages of Terraform configuration. Modules can be used to create reusable components and to organize code. Think of modules like functions: modules have input variables and output variables.
The only required piece of information in a module is the source
, which tells Terraform where to download the data sources and resources which in turn tell Terraform what to use.
For example, here is a module for deploying an application to us-sw-1
:
The confirmation file will declare the provider, data sources, resources, and outputs after running Terraform. The minimum version of Terraform required is 0.10.x.
To ensure the correct version is being used, include the following at the top of your configuration file:
Providers are the underlying platforms which support Terraform. Providers are responsible for managing the lifecycle of a resource: create, read, update, delete. Triton is a Terraform provider.
The "triton"
provider uses Triton environment variables including your Triton username, SSH fingerprint, and the CloudAPI endpoint.
NOTE: Though it is possible to proceed without setting up environment variables by replacing the contents with the corresponding information, we do not advise you do so. It is a best practice to store all important keys locally instead of tying it to your application files.
Data sources allow data to be fetched or computed for use within Terraform configuration, allowing Terraform to build infrastructure based on information from outside of Terraform (or from a separate Terraform configuration file). Providers are responsible for defining and implementing data sources, which present read-only views of pre-existing data or compute new values on the fly.
Common data sources for Terraform include triton_image
and triton_network
. Below is an example of data sources which refer to Terraform variables.
Resource blocks define components of your infrastructure. This could be a VM or container on Triton, or it could be an email provider, DNS record, or database provider.
Below us an example using variables and data sources to build a Triton machine:
Let's break down this block further:
The name of our container will be
my_new_application
The package assigned to our container is
g4-highcpu-128M
The image uses the ID of the previously defined data source,
my_image
The networks include the IDs taken from the data source
service_networks
We've added tags for Triton CNS, so our DNS names will start with our Triton CNS service name
Terraform creates and stores attribute values for all created resources. Not all of this information may be immediately valuable. Some may have immediate importance such as IP addresses or domain names. Outputs are a way for Terraform to query and display the necessary values after implementing application infrastructure.
Here are some examples for getting the primaryIP
address and Triton CNS powered domain names:
Downloading the Terraform provider is a critical step in determining how Terraform will work going forward. Execute terraform init
to download the Triton provider in the background into the local application directory.
This output informs us that version 0.4 of Triton has been installed. If you require a different version of a provider, you can specify it within the configuration file.
Below are some important terms going forward.
Plan: the plan is the first of two steps required for Terraform to make changes to infrastructure. Using terraform plan
determines what changes need to be made and outputs what will be done before it's done.
Apply: the second of two steps required to make changes to the infrastructure. With terraform apply
, Terraform communicates with external APIs (i.e. the providers) to make changes.
State: the Terraform state is the state of your infrastructure stored from the last time Terraform was run or applied. By default, this is stored in a local file named terraform.tfstate
.
Run terraform plan -out application.plan
to review what Terraform will be building based on your configuration file. The -out
parameter saves the plan to applican.plan
to ensure you know exactly what's going to happen when you're ready to deploy.
The result should look similar the following:
If there have been any errors, you may have to go back and modify the configuration file before proceeding.
Once you know what Terraform will do, use terraform apply
to build your infrastructure.
If you've included outputs in your configuration file, you will see those at the end.
The Terraform mailing list and IRC channel (
#terraform-tool
on Freenode) are great for general questions about Terraform.
We have several demos available on Joyent's blog for using Terraform.
Step-by-step walkthrough building simple application infrastructure
Last updated