The Startup
Published in

The Startup

A Comprehensive Guide to Creating Your First Terraform Configuration

So you heard about Hashicorp Terraform and decided you want to try it out? Here is a comprehensive guide to getting started.

Photo by Gia Oris on Unsplash

Before we can even get started using Terraform we need to first create an account for a cloud service to use it with. You can use AWS, Azure, or GCP to get started. I’ll provide some instructions for AWS.

Create an AWS Account

Go to the AWS Console and click Create an AWS Account.

You may need to click Create a New AWS Account as seen in the image below.

Next, you will be prompted to create your account and name it.

Now you need to choose the appropriate account type

Fill out the remaining information

After clicking create account and continue you will be prompted to enter some billing information. So we will stop here.

One last thing… Please setup MFA on your account. Here is a guide to do it!

Install the Terraform Binaries

First things first, you need to install the Terraform binary files that will allow you to run Terraform plans. You will frequently see these files referred to as Terraform Open Source.

For installing on OSX, Terraform recently published an official Brew tap. You just need to run his command

brew install terraform

Pretty easy!

For Windows you can most easily install Terraform using Chocolatey. The command is

choco install terraform

If you do not use brew or chocolatey I would highly suggest you check them out. They make managing your packages and installations way easier!

Let’s make sure we have it installed now. Just type the below command and hit enter

terraform

You should see something like this

Easy peasy!

Create IAM User and Access Keys

If you logged out of AWS get logged back in! It is time to create our IAM user and access keys.

Once you are logged in click services and type IAM, then click IAM.

Next, you will click users.

Select add user.

Enter a username and select programmatic access.

For my use case I am using Terraform to manage all of my AWS environment, so I am giving it admin access. Let’s just select attach existing policies directly and choose administrator access.

Add any tags you wish. Then create your user!

Make sure to download the csv or at least save your access key and secret key for now. We will use those in just a second.

Install and Configure the AWS CLI

You can install the AWS cli using documentation from AWS here.

Here are the commands you need to install it on macOS.

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"sudo installer -pkg AWSCLIV2.pkg -target /

Next run aws configure to configure the cli like this.

aws configure

You will then be prompted to enter your Access Key ID and Secret Access Key for your AWS user which you saved.

You will also set your default region and output format, but it is not required.

Create a Plan and Run it

Now it is time to create a Terraform configuration! Finally!

Open a text editor and paste in the following block of code (AWS).

provider "aws" {
region = "YOUR REGION"
}
resource "aws_s3_bucket" "test" {
bucket_prefix = "test-"
acl = "private"
}

Replace YOUR REGION with the region you wish to use. Then save your file wherever you wish, but name it main.tf.

Now open your command line and make sure you change to the directory you saved the file to. For example:

cd Desktop/

Run this Terraform command.

terraform init

This downloads all of the files our providers need, in this case it’s the AWS provider. It will provide you with some output, if it’s green you are good to go!

Time for the big one! Run this command!

terraform apply

You will see an output of what Terraform is going to do. In this case it is going to create an S3 bucket.

At the very bottom you will see a summary of the plan.

Just below that Terraform wants us to confirm that we do want it to create an S3 bucket. Type yes.

There is another output that verifies the apply was successful (or unsuccessful).

Finally, we can login to AWS and see if we have a bucket that begins with the name test-. To see it just login to AWS, click services, and search for S3.

For our code it will only have the prefix of test-, but everything after that is a random string of numbers. Here is what mine looks like:

That is it! You created your first resources in Terraform!

Now what if you don’t want to keep them? Let’s go over running a terraform destroy.

Destroy Your Resources

Let’s go ahead and destroy that bucket we created. Just make sure your cli is in the same directory as the main.tf file again.

Now run this command:

terraform destroy

It will give a similar output to a terraform apply. However, this time it notes that it is destroying the resources.

Go ahead and type yes. Hit enter… and BOOM!

Your resources are destroyed! Don’t believe me? Go login to AWS and check for yourself!

You Did It!

I hope you enjoyed this and found it helpful! If you are interested in learning more about Terraform I have a Free Terraform Course and a course to help you study for your HashiCorp Certified: Terraform Associate.

I also highly suggest checking out Terraform Up & Running by Yevgeniy Brikman.

Happy learning!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store