Infrastructure as Code with terraform

Infrastructure as Code(IaC) is approach for automation of IT infrastructure as software. It enables teams to manage, maintain and provision resources.

When using cloud platforms to manage all the changes and repetitive tasks for provisioning resources, IaC does it well by acting as a source of truth and by using version control can be used across teams.

For example, to provision network in cloud including subnets, security lists, route table, gateways it takes lot of clicks and configuration edit if done manually. No doubt if it's one time task then it should not matter but let say someone comes in and changes configuration and that change is not documented, it makes everyone's task difficult.

Using IaC tool like terraform we can write code and provision above infrastructure and whenever the change is required we make it in terraform and apply again. This give us standard configuration file which can be shared across team and if any change is made outside we can catch that using drift detection.

Benefits

  • Consistency- manageable configuration using version control shared across teams
  • Part of Devops- Can be used as part of CI/CD.
  • Easy to maintain and deploy- version control and declarative files to maintain and bare minimum command lines to deploy.

Terraform

  • Terraform is most popular tool for Infrastructure as Code.
  • Written in human readable file format called HCL(HashiCorp Configuration Language)
  • Write code -> plan -> apply.

Example - Provision Virtual Cloud Network using Terraform in Oracle Cloud this is what configuration looks like.

Note: I am using Oracle Cloud but terraform can be used with other cloud vendors like AWS,Azure,GCP.

Prerequisite

  • Terraform
  • OCI config file
terraform {
  required_providers {
    oci = {
      source = "hashicorp/oci"
    }
  }
}

provider "oci" {
  region              = "us-ashburn-1"
}

resource "oci_core_vcn" "internal" {
  dns_label      = "internal"
  cidr_block     = "172.16.0.0/16"
  compartment_id = "<your_compartment_OCID_here>"
  display_name   = "Terraform VCN"
}
main.tf

To deploy this run following commands

terraform init
terraform plan
terraform apply

Resources to get started.

Show Comments