Learning Terraform for AWS

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:

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.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store