Terraform Tutorial for Beginners
Terraform is an open-source infrastructure as code software tool created by Hashicorp. Used to define and provision infrastructure using a declaritive configuration language know as Hashicorp Configuration Language (HCL) or JSON – https://en.wikipedia.org/wiki/Terraform_(software)
Infrastructure as Code
Infrastructure as code is the concept of managing your infrastructure in a single file or files that define how you want your infrastructure to look like. i.e if you want to provision 50 servers in AWS with certain sizes and functions and different networking, firewalls and load balancers etc. You could go in to the AWS console and provision these one at a time by hand, or you could define everything in a single file which via Terraform (or another cloud provisioning tool) push these changes to AWS.
Having all the infrastructure defined in this one file means you can enforce state and ensure your infrastructure is always configured correctly.
This file or files can now be version controlled with something like Git so you can roll back any changes and you have full visibility of anything that is happening to your infrastructure.
This is the basic concept of Infrastructure as Code or IaC.
Terraform’s Purpose
Terraform is a multi platform tool that can take these files and make the connections to different infrastructure and push the changes requested or deletions.
It is mainly focused on cloud environments, but can also provision networking resources like Cisco ACI F5 load balancers or ASAv firewalls.
Check out my post – Terraform vs Ansible – a comparison which is commonly made
Terraform Providers
Terraform works on the concept of Providers.
A provider is code that understands how to interact with a certain infrastructure. i.e if you wanted to provision an EC2 resource in AWS you don’t need to know how to code the API to interact with the AWS console, you simply add the AWS provider into your file, Terraform downloads the correct code when you set everything up and then you just enter your access credentials.
A full list of Terraform providers can be found on the Registry.
https://registry.terraform.io/browse/providers
Terraform Resources
Resources are the most important part of the Terraform language, they describe each and every part of an infrastructure.
A resource block declares a resource with a local name.
In the example below the resource is aws_instance and the local name is web.
resource "aws_instance" "web" {
ami = "ami-a1b2c3d4"
instance_type = "t2.micro"
}
Terraform Modules
Modules allow you to group multiple resources together.
So you can put multiple resources together and package them into a module and then call that module.
It’s the same concept as packages in Python.
Terraform Variables
Input variables allow you to feed in parameters to a Terraform module.
Terraform Workspaces
Workspaces function like separate working directories and a workspace contains everything Terraform needs to manage a collection of infrastructure.
Terraform Cloud manages infrastructure collections with workspaces instead of directories.
Terraform Enterprise
Enterprise is a self-hosted version of Terraform Cloud. It allows enterprises to run a private instance of the Terraform Cloud application.
The main benefit is security, audit logging and no resource limits
Terraform Certification
Once you have got the basics under your belt you might want to get HashiCorp certified to prove your knowledge of the product.
The Terraform Associate exam is the starting point: https://www.hashicorp.com/certification/terraform-associate
There are some great free learning resources here: https://www.hashicorp.com/certification/terraform-associate
If you just want to learn more about Terraform there are many tutorials here: https://learn.hashicorp.com/terraform
Terraform Books
One of the best books to learn Terraform is “Terraform up and running” by Yevgeniy Brikma, Second Edition published by Oreily
So get started and download Terraform and start your journey into Infrastructure as Code with Terraform!
Leave a Reply