Terraform: Building our Build Tools

The day has finally come! Today was are going to begin our Infrastructure-as-Code journey. Our tool of choice: Terraform by Hashicorp. In my opinion, Terraform is the best tool for creation and management of cloud resources. It is stateful, fails gracefully, and has plugins for all major cloud vendors. Plus, Terraform is super simple to use and has ridiculously good set of documentation. Today’s tutorial will be small, so we’ll only have a few parts: install, configuration, and validation. This tutorial will be primarily geared towards using AWS (obviously).

Installing

MacOS

https://gist.github.com/HammoTime/dc39b76e2579023679b3f52784fab546

Linux (Red Hat)

https://gist.github.com/HammoTime/4f8573f8a4a15e194fe84604c3173030

Windows

https://gist.github.com/HammoTime/e403a114581daa42784683b01882cbcc

Verifying the Install

Now that we’ve installed Terraform (assuming the above scripts ran without error), let’s check to see if it was installed by running:

MacBookPro:~ hammo$ terraform -v
Terraform v0.11.13

If we run the command and get the version output, Terraform is installed successfully.

Configuration

As we are using AWS for all of our examples, we will be covering how to setup the AWS provider for Terraform. However, if you are using a different provider, feel free to check the Providers Documentation.

  1. Log into the AWS Management Console.
  2. Open the IAM Management Console.
  3. Click Users.
  4. Find your own username, click on it.
  5. Click the Security Credentials tab.
  6. Click Create access key.
  7. Make a note of the Access key ID and Secret access key.
  8. Open up a command shell. Enter the following commands:
export AWS_ACCESS_KEY_ID=AKIAAWSACCESSKEY
export AWS_SECRET_ACCESS_KEY=AKIAAWSSECRETACCESSKEYFORYOURACCOUNT

Congratulations, that’s it! Your Access and Secret keys have been setup successfully.

Validation

Now for our final step, we’re going to run a very simple Terraform script which is going to simply view the data related to the IAM user you’re using. If it can connect to AWS and retrieve the data successfully, then you’ve setup your environment successfully!

First create a new file called example.tf and paste the following in the contents:

data "aws_iam_user" "example" {
    user_name = "<YOUR_IAM_USERNAME>"
}

Once you’ve created this and replaced <YOUR_IAM_USER_NAME> with your IAM Username, run the following commands:

terraform init
terraform apply --auto-approve

If all of these run without error, congratulations: you now have Terraform up and running!

2 thoughts on “Terraform: Building our Build Tools”

Leave a Reply

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