This tutorial is adapted from the Web Age course Automation with Terraform and AWS Elastic Kubernetes Service Training.
What is Terraform?
Terraform is an agnostic provisioner, written in GO by Hashicorp to provision, change, and version infrastructure resources and objects on a variety of platforms. It is designed for repeatable, automated templated provisioning by teams. It works with popular service providers such as AWS, Azure, Google Cloud Platform and so much more. It can do low-level components like Virtual Machines or high-level components like SaaS features. It is extended via Providers which can be official or they can be community created. It works well as a builder of infrastructure but needs other tools for sophisticated Configuration Management (like Ansible) or for Image Management (like Packer).
Key Features of Terraform
- Infrastructure as Code via Templates
- Automation via single command execution
- Extensibility via Providers
- State Consistency via local or remote state files
- Reusability via Modules
- Preview Changes before they are executed
- Idempotency
- Parallelization of Operations
- Dependency Management
A Sample Terraform Workflow
- Write a Terraform file (.tf) that “declares” your desired infrastructure
- terraform init in order to have Terraform scan your local directory, and download your Providers (like AWS or Azure)
-
terraform planto have Terraform show you what it is going to do
-
terraform apply to have Terraform execute and launch resources based on what it showed you in the planning stages
-
terraform destroy to have Terraform destroy and move any infrastructure once you are done.
A Sample Terraform ec2.tf file
provider “aws” {
region = “us-east-2”
}
resource “aws_instance” “example” {
ami = “ami-0c55b159cbfafe1f0”
instance_type = “t2.micro”
}
What are Providers?
Providers are plugins that Terraform uses to manage platforms like AWS, Azure, GCP, etc. The AWS Provider allows us to call the AWS API via Terraform and create AWS objects like EC2 instances and Load Balancers. Providers (the code itself) is downloaded when we run terraform init in a directory the first time. Subsequent “init” commands will update the plugin, but only if it the provider version is not pinned and there is an update. Provider code is separate from main Terraform and is maintained separately and deployed independently.
What Providers are currently supported?
- https://registry.terraform.io/ for the full list
- plus you can run your own private registry!
Some considerations when comparing other IaC Tools
- Configuration management versus image management versus provisioning
- Mutable infrastructure versus immutable infrastructure (Arch Model)
- Procedural language versus declarative language (Language)
- Centralized server versus decentralized servers (Centralization)
- Agent’ed (local client) versus agentless (no local client) – (Management)
- Large community versus small community (Adoption)
- Time when first released (Maturity)
- Integration with other tools (Interoperability)
The IaC Tools Communities as of May 2019
How does Terraform compare with other IaC tools?
Maturity of IaC Tools
Most Common IaC Tools and their use
Installing Terraform – The typical process
Set up or create your Provider account and/or authentication
- Example – AWS IAM user, AWS CLI profile, or EC2 assigned role
- Example – Artifactory Account with API Key
Install Terraform
- homebrew, chocolatey, apt, yum, dnf, snap, etc
- manually via downloads page at https://www.terraform.io/downloads.html
- Written in Go; it runs pretty much anywhere it is compiled for
- Windows, MacOS, Linux, Solaris, BSD
- make sure to check permissions on any Linux/Unix
- make sure it is in your PATH
- terraform version to verify installation
- Move on to creating files, init, plan, and apply
Summary
In this tutorial, we learnt about the fundamentals of Terraform.