What is Infrastructure as Code (IaC)?

Infrastructure as Code is the use of code or configuration files to define, provision and manage infrastructure - typically in cloud native environments - as opposed to manual infrastructure provisioning.

By utilising IaC, organisations are better able to codify and document infrastructure architectures and changes, making deployments replicable, scalable and more fault tolerant. These benefits, coupled with the overt benefits of cloud computing, often find IaC principles at the core of mature, cloud native architectures and a crucial part of any DevOps workflow.

One popular method of defining infrastructure as code is HashiCorp Terraform, a declarative configuration language that effortlessly converts code into live deployed environments in the back end.

Following some initial configuration and secret management, Terraform can deploy entire internet-scale cloud networks from short text files. This simplified workflow allows DevOps engineers to codify deployments, then utilise CI/CD pipelines in conjunction with application development lifecycles to manage deployments. Tools like Terraform also allow engineers to version control configurations, using git or any other locally employed VCS, and utilise Terraform state management capabilities to track and monitor resources during and post deployment.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.20"
    }
  }

  required_version = ">= 1.3.2"
}

provider "aws" {
  region  = "eu-west-1"
}

resource "aws_instance" "prod_server" {
  ami           = "ami-xxxxxxx"
  instance_type = "t3.micro"

  tags = {
    Name = "Prod Logic Server"
  }
}

In the above snippet, first some metadata about the IaC environment is declared, such as the version of the Terraform software and respective cloud service provider 'providers' is defined.

Then, in the second block, the appropriate provider metadata is defined - here, simply the desired region.

Finally, in the third block is the core IaC logic: an AWS EC2 instance - or cloud virtual machine - is defined, an operating system image (ami-image being an AWS centric term for this) is stated, and the appropriate specifications (instance type) are outlined. Also, a tag to be used both by Terraform and within the AWS console is added.

terraform init // initialises the terraform workspace on engineer machine
terraform plan // performs 'dry run', confirming changes required
terraform apply // deploys outlined environment on connected AWS account

Finally, following the execution of two Terraform commands locally by an engineer, this cloud environment will be created and deployed, live.