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
- Download and setup Terraform.
- Configure AWS CLI credential.
- Write your first Terraform configuration file.
- Init, plan and apply.
- Verify result at AWS.
- Destroy the resources.
Download and setup Terraform
Download Terraform executable file from office website, place it where your PATH variable recognizes.
- For video instruction, please refer here.
- For Linux, please refer here.
- 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.
Referances:
General
The Core Terraform workflow
Learn about provisioning infrastructure with HashiCorp Terraform
Manually Managing State
Configuration Language
Provides
Resources
Input Variables
Output Values