Learning Terraform for AWS

Brad Simonin
2 min readOct 18, 2017

For those learning AWS/AWS CLI Terraform is a tool for building infrastructure with various technologies including AWS.

Here is a very simple document on how to use Terraform to build an AWS EC2 Linux instance.

Terraform uses the Amazon Command Line Interface (AWS CLI) so make sure you have this setup and enabled on your local Linux box

Download terraform: https://www.terraform.io/downloads.html. I downloaded the 64bit Linux version, unzipped the archive file, and copied the terraform executable into my /usr/local/bin directory. Now the terraform command is in my path.

Create a directory to store and use terraform files:

mkdir -p ~/terraform/CreateInstance

I created the two following Terraform tf files with the above directory:

CreatInstance.tf and variables.tf:

CreateInstance.tf is the actual configuration file. Notice I use variables within that file which are declared in the variables.tf file.

CreateInstance.tf:

provider "aws" {
version = "~> 2.0"
access_key = var.access_key
secret_key = var.secret_key
region = var.region
}
# create an instance
resource "aws_instance" "CreateInstance" {
ami = lookup(var.amis, var.region)
subnet_id = var.subnet
security_groups = var.securityGroups
key_name = var.keyName
instance_type = var.instanceType
# Name the instance
tags = {
Name = var.instanceName
}
# Name the volumes (will name all volumes included in the
# ami for this instance)
volume_tags = {
Name = var.instanceName
}
} # end resource

variables.tf:

variable "access_key" {
default = "<PUT IN YOUR AWS ACCESS KEY>"
}
variable "secret_key" {
default = "<PUT IN YOUR AWS SECRET KEY>"
}
variable "region" {
default = "us-east-1"
}
variable "availabilityZone" {
default = "us-east-1a"
}
variable "instanceType" {
default = "t2.micro"
}
variable "keyName" {
default = "<PUT IN THE NAME OF YOUR AWS PEM KEY>"
}
variable "subnet" {
default = "subnet-<PUT IN YOUR VPC SUBNET>"
}
variable "securityGroups" {
type = list
default = [ "sg-<PUT IN YOUR VPC SECURITY GROUP>" ]
}
variable "instanceName" {
default = "<PUT IN YOUR INSTANCE NAME>"
}
variable "amis" {
default = {
"us-east-1" = "ami-0b898040803850657"
}
}
# end of variables.tf

ami-0b898040803850657 is the free Amazon Linux 2 AMI for the us-east-1 region. Amazon Linux 2 is a downstream version of Red Hat Enterprise Linux / Fedora / CentOS. It is analogous to RHEL 7.

Once these files are created you can use the terraform commands to syntax check and prepare the deployment and then deploy the AWS instance.

Within the directory issue:

terraform init

The init argument will initialize the environment.

Then issue:

terraform plan -out CreateInstance.plan

The plan argument will syntax check the files and prepare the deployment.

Deploy the instance:

terraform apply CreateInstance.plan

To view data about the instance execute:

terraform show

To destroy the instance execute:

terraform destroy

Deploying in AWS EC2 can be pretty simple with terraform.

--

--