Terraform

Terraform is an “infrastructure as code” tool similar to AWS CloudFormation by Hashicorp (who also created Vagrant). Features include:

  • Execution Plans: during Terraform’s planning step it generates an execution plan which shows what will be changed
  • Resource Graph: by using a graph of resources, Terraform can parallelize changes where there are no dependencies
  • Automation of changesets: Terraform automates the process of infrastructure changes

Here are some examples of how you’d use Terraform:

  • Setting up networking stacks – e.g. AWS VPC’s – aka Software Defined Networking (SDN). Note, because Terraform uses config files, we can put the SDN implementation under version control – pretty powerful stuff
  • Deploying across different Cloud systems – the downside of CloudFormation is that it’s tied to AWS. By using Terraform you can deploy to AWS and Azure increasing fault-tolerance
  • Setting up Multi-Tier applications

Installing Terraform

The entire piece of Terraform software is a single binary. Download and place in your PATH.

Simple example

Let’s spin up an EC2 instance. Here’s an example. Create an ec2_instance.tf file with:

provider "aws" {
  access_key = "ACCESS_KEY_HERE"
  secret_key = "SECRET_KEY_HERE"
  region     = "us-east-1"
}

resource "aws_instance" "example" {
  ami           = "ami-0d729a60"
  instance_type = "t2.micro"
}

Update your details and make sure it’s the only .tf file as Terraform loads all the .tf files in a directory.

Then to see what changes would take place use:

terraform plan

+ => a resource will be created

To apply these use:

terraform apply

Note: if things don’t work as expected you can debug by setting the TF_LOG environment variable.

For example, if a T2 instance must be launched in a VPC in which case you will need to specify a subnet_id using

  subnet_id = "<your subnet id>"

References:

https://www.terraform.io/

https://aws.amazon.com/blogs/apn/terraform-beyond-the-basics-with-aws/

https://www.terraform.io/docs/internals/debugging.html

https://www.terraform.io/docs/providers/aws/r/instance.html

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/t2-instances.html

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *