Reading Time: 2 minutes
Terraform

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 out 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 syntax doesn’t increase my productivity.

One of the strengths of the tool is the support for several providers. 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. I was able to get an EC2 instance spun up with a small amount of markup.

locals {
  key_name = "<Your Key Name" // 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-00eb20669e0990cb4"
  instance_type   = "t2.micro"
  key_name        = local.key_name
  security_groups = local.security_groups
  user_data       = <<EOF
#!/bin/bash -ex
sudo yum install -y docker vim
sudo chkconfig docker on
sudo service docker start
sudo usermod -aG docker ec2-user
  EOF
}

This template will provide an EC2 instance with docker at the ready. It’s quite succinct 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.

Terraform Learning Resources

Terraform: Up and Running: Writing Infrastructure as Code
Terraform – Getting Started
Learning Terraform

Sharing is caring!

Leave a comment