Terraform enables you to create, change and improve infrastructure reliably and predictably. It is open source and lets you create declarative configuration files that can be treated as code, (infrastructure as code). In this article, we are going to step through the process to use create a Terraform EC2 instance.

This blog post has a companion video created by TechSnips contributor, Nick Rimmer. Feel free to have a watch or, if you prefer text, read on!

Installing Terraform

The first step is to install Terraform. This is a very easy process and can be followed at https://www.terraform.io/intro/getting-started/install.html. We'll assume you can run through the instructions there.

Create an AWS IAM Account

Next, you need to create an IAM account in AWS. This will be needed so that we can use it within the Terraform code, but not actually within the code. That would be reckless! You'll see.

Create a local AWS Profile

Create a local AWS profile in PowerShell which will let Terraform read those credentials, but not include them in the actual code so that the code can be stored and shared safely. Have a look at this video below by Bryce McDonald for step-by-step instructions on how to get that set up.

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.

Initializing Terraform

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.

Applying Terraform Configuration

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.

Building the EC2 instance with Terraform

Verifying EC2 Instance Creation

If you switch over to the Amazon console, you can now see the instance. This 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.

terraform show

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.

Removing the EC2 instance

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!

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!