Following on from my previous blog post which looked at installing Terraform, I am now going to look at using Terraform to deploy an AWS EC2 instance.
Terraform uses Infrastructure as Code (IaC) and so it needs a terraform file containing the details of what the expected infrastructure should be.
Note: To follow along with this post you will need to have the AWS CLI installed and configured so that some AWS credentials are stored within the ~/.aws/ directory. If you need help setting up AWS CLI then consider my blog post on setting it up on a Mac, or my post installing it on a Raspberry Pi (Linux).
A Directory For The Terraform Project
The first step is to create a new directory / folder to hold the Terraform details. Each Terraform project should be given its own directory as during the various stages of using Terraform several additional files/directories (folders) are created by Terraform that are unique to the infrastructure:
- .terraform
A directory Terraform uses to store temp information and provider details - *.tfstate
A file Terraform uses to store state information about the infrastructure in - *.tfstate.backup
A file Terraform uses to store state information about the infrastructure in
If you are following along on Linux then the mkdir directory_name command will create a directory for the work, then cd directory_name command will put you into the directory.
I’m going to call my directory “tf_aws_1“.
Terraform uses files that end in the extension .tf and these can be created via a text editor (e.g. create a text file and change the extension to .tf) or via the touch filename.tf command.
I created a file called main.tf and then entered the following information:

provider "aws" { profile = "default" region = "eu-west-2" }
This tells Terraform that I want to use AWS as the provider, to read my default AWS credentials (stored in ~/.aws/) and that my default region is eu-west-2.
resource "aws_instance" "geektechstuff_example" { ami = "ami-04122be15033aa7ec" instance_type = "t2.micro"
This tells Terraform that I want to create a resource using an AWS instance and that I want to refer to this resource within Terraform as geektechstuff_example (i.e. it’s name in Terraform is geektechstuff_example).
The ami is an AMI (Amazon Machine Image) reference from AWS referring to the Amazon Linux 2 ami. The instance type refers to an AWS instance type, in this case the t2.micro (I’m still trying to use the free tier as much as possible).
tags = { Name = "geektechstuff_ec2_test" Purpose = "terraform testing" } }
This tells Terraform to add some tags to the resource within AWS. This step is not mandatory but highly recommended as tagging makes it easier to find and identify resources in AWS. I’m using the tags “Name” and “Purpose”. The Name tag will give the resource a name in AWS (i.e. I’ve called this EC2 instance geektechstuff_ec2_test). The AWS name and Terraform name do not have to be the same.
In total the main.tf file looks like:
provider "aws" { profile = "default" region = "eu-west-2" } resource "aws_instance" "geektechstuff_example" { ami = "ami-04122be15033aa7ec" instance_type = "t2.micro" tags = { Name = "geektechstuff_ec2_test" Purpose = "terraform testing" } }
Which is short and sweet to launch the EC2 instance.
terraform init

With the main.tf file in place it is time to run a terraform command:
terraform init
The Terraform init command asks Terraform to read through any .tf files within the directory and to then download the relevant provider plugins. This is basically Terraform seeing the provider information in the .tf file and configuring itself so that it can talk to the provider.
The terraform init command can be run multiple times if needed.
terraform plan

The command:
terraform plan
will read the .tf file and then output what Terraform has detected needs to be carried out. In my example it has detected that an EC2 instance needs creating and tagging within AWS.
terraform apply
If the terraform plan matches what you are expecting then Terraform can be told to build the infrastructure using the command:
terraform apply
which again will advise what changes are going to take place and then ask for confirmation before carrying out the work.

After confirmation Terraform will begin the work and post updates on the command line until the work is complete.

The EC2 instance is now completed and if check the AWS CLI (or use the AWS EC2 web console) it will be showing.
terraform destroy
Terraform can also destroy the infrastructure it has created using the command,
terraform destroy

Terraform will output a plan showing what actions will be carried out (i.e. what resources will be destroyed) and as with the terraform apply command, Terraform will ask for confirmation before carrying out the work.

One thought on “Terraform – Introduction: TF files / First Run”
Comments are closed.