Creating your first Terraform infrastructure on AWS

Mark Stein
Slalom Technology
Published in
4 min readJan 25, 2019

--

Terraform AWS

Terraform is an agnostic cloud-provisioning tool created by Hashicorp. Terraform allows you to create, manage, and update your infrastructure in a safe and efficient manner. Terraform’s configuration is all done using a language called the HashiCorp Configuration Language (HCL). Apart from other tools out there, Terraform is not constrained to any specific platform and supports many leading cloud providers out there.

Click here to see a list of Terraform’s up-to-date providers.

In this example, we will show you how to build a simple EC2 instance using Terraform on AWS.

First Step — Setup AWS Account

Let's assume all our work will be done in the region us-east-1

  1. Create a new user in the IAM Section on AWS here.
  2. Select Programmatic access below and enter your user details.

3. Click next and select the admin group.

4. Continue with the steps until you reach the Create User section and confirm the user has been created. Once the user is created you will get an Access key ID and Secret access key. Store these in a safe location as you will need these later. See below for an example.

Install Terraform

Download Terraform here and follow the guide here on how to install Terraform on your specific system.

Once you have successfully installed Terraform, continue to the next section.

Build and Destroy your first instance using Terraform

Terraform configuration is deployed using the (HCL) HashiCorp Configuration Language.

For more information on HCL click here.

Now that we have created our AWS account and created an IAM user, let’s spin up our first EC2 instance using Terraform.

Let's create a file called instance.tf with the following code.

instance.tf

Here we explicitly state that we are using the AWS provider plugin from Terraform. We provide the access key/secret from the user in IAM that we created. We also supply the region in which we want to make all changes. In this example, we are using “us-east-1".

We then use the resource identifier “aws_instance” to state that we are trying to bring up an EC2 instance followed by the name identifier “example”. This can be anything you desire.

We supply the AMI type on AWS. An AMI is an identifier specific to AWS for the image you wish to install on the instance (Ubuntu/Windows 32/64bit etc..). You can find the specific AMI you wish to deploy per region here.

We also supply the “instance_type”. In this example, we will use a t2.micro instance type as it is supported by the AWS Free Tier package.

Now we will run the “terraform init” command where we created our instance.tf file to download and initialize the appropriate provider plugins. In this case, we are downloading the AWS provider plugin we specified in our instance.tf file.

Once this is complete, let's run the “terraform plan” command. This will let us see what Terraform will do before we decide to apply it.

Now to create the instance, we run the “terraform apply” command.

If we go to our EC2 section on the AWS console you can see that a t2.micro instance was created successfully!

Congratulations You just created your first Terraform based infrastructure using AWS!

Now if we wish to destroy the Terraform infrastructure we created, we can simply run “terraform destroy”. This will destroy all the resources we have created in our Terraform infrastructure.

If you are creating or deleting multiple resources, Terraform is able to handle these in Parallel giving us a faster delivery/destruction of our infrastructure.

Why use Terraform

Terraform makes it easy to describe and understand the infrastructure you wish to create. If you decide to leverage Terraform’s Enterprise solution, you can take advantage of its powerful features.

Here are a couple of features Terraform Enterprise adds to your workflow:

  1. GUI Workspace Management
  2. A private module registry
  3. Sentinel policies
  4. Team Management
  5. Audit Logging

--

--