Terraform Basic Walk Through with AWS

What is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

Basic Components

Infrastructure as Code

Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and treated as you would any other code. Additionally, infrastructure can be shared and re-used.

Execution Plans

Terraform has a “planning” step where it generates an execution plan. The execution plan shows what Terraform will do when you call apply. This lets you avoid any surprises when Terraform manipulates infrastructure.

Terraform vs. Chef, Puppet, etc

Chef, Puppet and others are configuration management tools that install and manage software on an existing machine. Where Terraform focuses on the higher-level abstraction of the datacenter and associated services. In simple words, Terraform creates resources.

Lab Steps

  1. Download and setup Terraform.
  2. Configure AWS CLI credential.
  3. Write your first Terraform configuration file.
  4. Init, plan and apply.
  5. Verify result at AWS.
  6. Destroy the resources.

Download and setup Terraform

Download Terraform executable file from office website, place it where your PATH variable recognizes.

  1. For video instruction, please refer here.
  2. For Linux, please refer here.
  3. For Windows, please refer here.

For Mac

curl -o terraform.zip https://releases.hashicorp.com/terraform/0.12.17/terraform_0.12.17_darwin_amd64.zip

Unzip it, place it to /usr/local/bin/ or any other PATH variables. Verify if it works.

terraform --version

Configure AWS CLI credential

I will not cover how to setup AWS CLI and its credential. For details, please refer Installing the AWS CLI and Configuring the AWS CLI.

Write your first Terraform configuration file

.tf file is like a blueprint of the infrastructure or resources you want to provision in the cloud. For more information about the .tf file, please refer here.

For this walk through, we will use the demo configuration to provision an EC2 on AWS. create a file with extension .tf anywhere in your computer and input the code below and save it.

provider "aws" {
profile = "default"
region = "us-east-1"
resource "aws_instance" "example" {
ami = "ami-2757f631"
instance_type = "t2.micro"

Init, plan and apply

Once .tf file is ready, we first need to perform init, which initializes various local settings and data that will be used by subsequent commands. Init will automatically download and install any Provider binary for the providers in use within the configuration.

terraform init

Next we need to produce an execution plan which will list out the resources to be executed on cloud.

terraform plan

If everything looks correct, perform apply to execute the action.

terraform apply

Verify result on AWS.

What if you want Terraform to show you the public ip address after EC2 is created? Add an output value to your configuration file, save, plan(optional) and apply it again.

provider "aws" {
profile = "default"
region = "us-east-1"
resource "aws_instance" "example" {
ami = "ami-2757f631"
instance_type = "t2.micro"
output "instance_ip_addr" {
value = aws_instance.example.public_ip

Once apply is done, Terraform also wrote some data into the terraform.tfstate file. This state file is extremely important; it keeps track of the IDs of created resources so that Terraform knows what it is managing.

This file must be saved and distributed to anyone who might run Terraform. It is generally recommended to setup remote state when working with Terraform, to share the state automatically. To show the current state

terraform show

We can use state command which is used for advanced state management. In cases where a user would need to modify the state file by finding resources in the terraform.tfstate file with terraform state list. This will give us a list of resources as addresses and resource IDs that we can then modify.

terraform state list

More actions can be performed by state.

Destroy Resources

Lastly, for this simple and basic lab, let’s destroy the EC2 created. -auto-approve is used so “confirmation” is not needed during provision or destruction of resources.

terraform -destroy -auto-approve

Verify the resource is shutting down.



The Core Terraform workflow
Learn about provisioning infrastructure with HashiCorp Terraform
Manually Managing State

Configuration Language

Input Variables
Output Values





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



AWS Certified SA, SysOps & Developer Associate, Alibaba Cloud certified SA. Focusing on Azure, Prometheus w/ Grafana, ELK and K8S now.