Terraform is an alternative to cloud-specific resource management tools. CloudFormation and Azure’s Resource Manager template maintenance are boons for automation.

I work with Amazon Web Services, so I tried the AWS Cloud Development Kit (CDK). The CDK sounds like a great idea on paper, but the documentation is abysmal. While sifting through GitHub repositories is fun, it is not a fun full-time job. Most of my time spent using the CDK required digging for examples rather than writing code.

It was time to start looking for alternatives. In the past, I had evaluated Terraform. I decided to avoid it when it was less mature. I’m not a fan of learning new syntax if learning that language doesn’t increase my productivity.

One of the strengths of the tool is the support for several providers. In addition, learning the Terraform language syntax is now worth the time, given its broad applications.

Terraform language uses a declarative approach. This approach provides a current state view of deployed resources in the code. This is in opposition to tools like Ansible and Chef that use a procedural approach.

So far, my experience with Terraform has been impressive. First, I was able to get an EC2 instance spun up with a small amount of markup.

locals {
  key_name = "YourKeyName" // Update to the name of your key pair
  security_groups = ["default"] // Update to your security group
}

provider "aws" {
  profile    = "default"
  region     = "us-east-1"
}

resource "aws_instance" "docker_container_runner" {
  // Get the latest Amazon Linux ami id
  // aws ec2 describe-images --region us-east-1 --owners amazon --filters 'Name=name,Values=amzn-ami-hvm-????.??.?.????????-x86_64-gp2' 'Name=state,Values=available' --query 'reverse(sort_by(Images, &CreationDate))[:1].ImageId' --output text
  ami             = "ami-123"
  instance_type   = "t2.micro"
  key_name        = local.key_name
  security_groups = local.security_groups
  user_data       = file("user_data.sh")
}

Place this script in a user_data.sh file in the same directory as your template.

#!/bin/bash -ex
sudo yum install -y docker vim
sudo chkconfig docker on
sudo service docker start
sudo usermod -aG docker ec2-user

This template will provide an EC2 instance with Docker at the ready. It’s quite concise and should need minimal work to maintain in the future. I will be continuing my Terraform learning; at this point, it’s a sound time investment.

Learning Terraform - Beyond the Basics


Other Learning