WhatsUpGold Blogs

How to Automate Building AWS EC2 Instances with Terraform

Adam Bertram | Posted on | Cloud Computing

If you need a quick and efficient way to automate building infrastructure in AWS, look no further than Hashicorp’s Terraform. Terraform is an infrastructure-as-code tool that allows you to create, change and keep infrastructure in compliance.

Built from an open-source model, Terraform lets you create declarative configuration files that can then be invoked to provision infrastructure of all kinds.

To demonstrate Terraform’s abilities, you’re going to learn how to set up Terraform and use it to create an AWS EC2 instance from scratch.

Prerequisites

To follow along with the examples in this tutorial, please be sure you have the following:

          An AWS IAM Account with permission to create and destroy an EC2 instance

          The AWS CLI Installed and Authenticated. 

Installing Terraform

Before you can do anything, you have to get Terraform installed. If you don’t already have Terraform installed, be sure to follow along with the installation guide. 

Building the Terraform Configuration File

We now need to look at the configuration file that will create your EC2 instance. This is called a Terraform configuration file, it has an extension .tf.

These files are made up of providers, and resources. Populate the providers section with the configuration information used to define our AWS environment (our provider).

provider "aws" {
region = "eu-west-2"
shared_credentials_file = "c:/Users/admin/.aws/credentials"
profile = "TechSNIPS"
}

Next, define the Amazon marketplace image (AMI) that you will use. Please check the ID for your region as this can differ from region to region. If you follow along with this code, there will be no need to update. We have selected a Windows 2016 image to use in this case.

resource "aws_instance" "example" {
ami = "ami-0c09927662c939f41"
instance_type = "t2.micro"
tags { name = "TESTVM"}
}

Initializing Terraform

At this stage we are ready to apply the configuration, however, Terraform will need the AWS plugin and will also need to initialize the Terraform environment. We use the command terraform init.

Now you can see from the screenshot, we have the AWS plugin and some more information regarding the environment.


Creating the EC2 Instance with Terraform

So now we are ready to execute the configuration and create our instance. Terraform will use the command apply to execute this. During execution, you are advised on what actual configuration will be executed.

At this point, you have not actually run anything. (In earlier versions you would have used Terraform plan to view the configuration that is to be implemented).

By typing yes, this configuration will now be sent to AWS, you can see it’s now creating.


Verifying EC2 Instance Creation

If you switch over to the Amazon console, you can now see the instance. These few lines of code you came up with demonstrate how powerful and easily infrastructure can be created using Terraform.

Search by the tag you set in the Terraform configuration file and it should return the EC2 instance you just created.


Use terraform show to view the configuration changes. This is a very rich output that gives you detail on all aspects of the resources you have created.

Removing the EC2 Instance with Terraform

It is also just as easy to remove your configuration using the terraform destroy command. You must be careful with this command as it will analyze any Terraform scripts it finds in the same directory as candidates for removal.

Let’s run terraform destroy.

After typing yes , Terraform will begin tearing down the EC2 instance.


Back in the AWS console, we can see that the instance has been terminated.

Summary

I hope this article has given you some insight into how powerful Terraform is and how you can create a Terraform EC2 instance. Terraform is a great tool to add to your DevOps tool belt!

Get Started with WhatsUpGold

Free Trial Price Quote Live Demo

Subscribe to our mailing list

Get our latest blog posts delivered in a weekly email.

Subscribe