Terraform

Introduction:

Terraform are cloud agnostic unlike CloudFormation which is cloud native with AWS and AzureResourceManager which is native with Azure cloud provider. Terraform helps to maintain single codebase to maintain infrastructure related code to provision resources across multiple cloud providers.

Setup:

To install Terraform, need to download the binary from “https://www.terraform.io/downloads.html” and set it in PATH variable. Run ‘sudo mv terraform /usr/local/bin’.

To verify the installation, please run the command terraform — version

IAC ( Infrastructure as Code )

  1. Adhoc Scripts
  2. Configuration Management Tools
  3. Service Template Tools
  4. Orchestration Tools
  5. Service Provisioning Tools

AdHoc Scripts

  1. General purpose programming language to write scripts
  2. Good for small tasks and it’s not good for Complicated Tasks
  3. Code can be messy, if we need to manage more servers, loadbalancers, here comes the advantage of IAC tools which provides features like IdemPotency and API support

“sudo apt-get update && sudo apt-get install apache2 -y && echo ‘<!doctype html><html><body><h1>Adhoc Script to install Apache server !</h1></body></html>’ | sudo tee /var/www/html/index.html”

Configuration Management Tools

  1. Chef, Puppet, Ansible and SaltStack — Designed to install and manage softwares on existing servers
  2. Enforce Coding Conventions and support for Idempotence
  3. Good for managing large number of remote servers

Server Templating Tools

  1. Docker, Packer and Vagrant
  2. Create an image of a Server that captures everything ( OS, Software and files ) and use the Configuration Management tool like Ansible to install the image into multiple servers
  3. Shift to Immutable infrastructure component

Orchestration Tools

  1. Kubernetes,Nomad,Marathon/Mesos
  2. Container Orchestration

Provisioning Tools

  1. Terraform, CloudFormation (Specific to AWS), OpenStack Heat

Add alt text

Approaches

  1. Declarative vs Imperative
  2. Immutable infrastructure vs Mutable Infrastructure

Terraform Files:

To start with Terraform, we need to specify the providers for Infrastructure. It can be any cloud service providers GCP or AWS.

The file can be named as provider.tf and it may have the following content.

provider.tf

Add alt text

variables.tf:

Variables can be declared inside variables.tf file and can be refereed in resources or main.tf file. It can be created using keyword variable. It can have the default value and description key to it.

Add alt text

Terraform Commands:

  1. terraform init ( To initialize the terraform )
  2. terraform plan ( To plan or preview the changes )
  3. terraform deploy ( To deploy or install the changes )
  4. terraform destroy ( To destroy the resources once finished )
  5. terraform fmt ( To do formatting )
  6. terraform refresh ( to refresh the state )
  7. terraform show ( to view the terraform.tfstate file )

Happy Learning !

Bharathy Poovalingam

#GCP #Terraform #Service Template #Learning