futuristic alien outerspace psychedelic algebra
TECHNOLOGY

Infrastructure as Code

Infrastructure as Code (IaC) is a DevOps practice that automates infrastructure provisioning through code, improving efficiency and reliability. Terraform is a popular IaC tool, and Terragrunt enhances its functionality for managing infrastructure an

January 24, 2023· 5 min read
93 score

Definition of Infrastructure as Code

Infrastructure as Code (IaC) is a type of DevOps practice that treats infrastructure as a "programmable" asset that can be managed and provisioned with customized configuration through code. This means that rather than manually configuring, managing and provisioning resources, IT and operations teams can develop scripts and employ tools that automate the entire process. The goal of IaC is to improve the efficiency and accuracy of the infrastructure provisioning process, allowing resources to be quickly and reliably configured as needed.

psychedelic interconnected servers in network futuristic abstract art with no letters

What is terraform?

Terraform is an Infrastructure as Code (IaC) tool used for building, changes, and versioning infrastructure safely and efficiently. It uses configuration files written in the HashiCorp Configuration Language (HCL) to define the desired state of the infrastructure resources. Terraform Can can be used to provision common Infrastructure resources such as storage, network, compute, databases, etc either in cloud providers like AWS, GCP and Azure, or in on-premises/in-house data centers. Terraform is also able to interact with different APIs to provision resources which cannot be provided natively.

terraforming mars with futuristic heavy machinery (psychedelic)

What are the alternatives to terraform?

The alternatives to Terraform are Ansible, Chef, Puppet and SaltStack. All these tools allow to automate the provisioning of configuration and application deployments across various cloud platforms, but differ in their approaches. Ansible and SaltStack are agent-less and use YAML based configuration scripts to provision Infrastructure, while Chef and Puppet rely on agents that can be installed on various nodes to do configuration ma nagement. - ChatGPT

Here it looks like ChatGPT is confused. Isn't terraform infrastrucuture management whereas the "alternatives" are configuration management?

Infrastructure Management versus Configuration management

I asked ChatGPT again about the differences. It started off pretty strong but then spilled out a bunch of misinformation

Terraform is a tool for Infrastructure-as-code. It manages the infrastructure, like servers, databases and networks, through the definition of a configuration file. This file describes the target infrastructure state. Terraform is different from chef, puppet, ansible, salt, etc. in the following ways.

  1. Terraform allows you to describe the entire target state configuration with one file. Chef, Ansible, Puppet, and Salt each require multiple files to define the target infrastructure.

WRONG. C'mon ChatGPT!

  1. Terraform is a standalone tool, while Chef, Ansible, Puppet, and Salt are tools that require additional systems to manage the infrastructure.

MISSED AGAIN

  1. Terraform allows for automation for the build, change and version infrastructure, while Chef, Ansible, Puppet and Salt are primarily focused on configuration management.

Hey, that's a little closer!

  1. Terraform uses a declarative language for configuration, rather than a procedural method as used in Chef, Ansible, Puppet, and Salt.

NOPE

  1. Terraform is cloud agnostic and uses Infrastructure-as-Code, while Chef, Ansible, Puppet, and Salt are tools that are designed to support specific clouds.

TOTALLY FALSE

The AI had some real problems here. Basically terraform is good at provisioning a server and the other tools may be better at configuring the server in some ways.

Terragrunt

What is terragrunt? Why would you use it?

Terragrunt is an open source tool that provides extra functionality on top of Terraform, such as remote state management, concise configuration painlessly applied across multiple environments, and DRY patterns used to describe infrastructure. It also enables controlled, auditable, version-controlled and automated deployments of infrastructure. Additionally, it provides core features such as shared libraries of components across organizations, and granular security features.

Terragrunt is useful to use in conjunction with Terraform because it provides a more organized way of managing infrastructure code. It eliminates the need to duplicate code across multiple environments, and enables organizations to create standardized components for deployments that can seamlessly be applied in multiple locations. Additionally, its ability to manage remote state data simplifies the coordination of changes and makes deployments more secure.

Hypothetical Tree of HCL

Projects├── app1│   ├── accounts│   │   ├── main.tf│   │   ├── vars.tf│   │   ├── terragrunt.hcl         # Special file for Terragrunt│   │   └── outputs.tf│   ├── app│   │   ├── main.tf│   │   ├── vars.tf│   │   ├── terragrunt.hcl         # Special file for Terragrunt│   │   └── outputs.tf│   ├── db│   │   ├── main.tf│   │   ├── vars.tf│   │   ├── terragrunt.hcl         # Special file for Terragrunt│   │   └── outputs.tf│   z ├── terragrunt.hcl           # Special file for Terragrunt│   └── terraform.tfvars           # Special file for Terraform├── app2│   ├── accounts│   │   ├── main.tf│   │   ├── vars.tf│   │   ├── terragrunt.hcl         # Special file for Terragrunt│   │   └── outputs.tf│   ├── app│   │   ├── main.tf│   │   ├── vars.tf│   │   ├── terragrunt.hcl         # Special file for Terragrunt│   │   └── outputs.tf│   ├── db│   │   ├── main.tf│   │   ├── vars.tf│   │   ├── terragrunt.hcl         # Special file for Terragrunt│   │   └── outputs.tf│   z ├── terragrunt.hcl           # Special file for Terragrunt│   └── terraform.tfvars           # Special file for Terraform└── misc    ├── main.tf    └── vars.tf

Example invocations of terragrunt

To plan a single directory with Terragrunt:

terragrunt plan-all --terragrunt-source /path/to/env/app/

To plan the whole tree together with Terragrunt:

terragrunt plan-all --terragrunt-source /path/to/env/

How would a typical terragrunt.hcl look?

Provided below is an example of how a typical Terraform.hcl file might look.

terraform {  # Required variable definitions   required_variables = {    "environment" = "production"  }   # Local workspace override   backend "s3" {    bucket = "terraform-state-myapp"  }   # Module options   source = "git::git@github.com/myorg/terraform-modules"   module "myapp-cluster-vpc" {    source  = "modules/vpc"    environment = "${var.environment}"  }   module "myapp-cluster" {    source  = "modules/cluster"    environment = "${var.environment}"  }   module "myapp-redis" {    source  = "modules/redis"    environment = "${var.environment}"  }   # Variables   locals {    name = "myapp"  }   # Outputs   output "vpc_id" {    value = "${module.myapp-cluster-vpc.vpc_id}"  }   output "prefix" {    value = "${local.name}-${var.environment}"  } }

How are the variables applied?

Terragrunt variables are used to apply settings and values to deployments and configurations, such as instance size and count, user data, tags, AMI settings, and resource settings. They can also be used to set environment-specific variables, such as environment-specific provider settings, or to configure remote state storage.

futuristic alien outerspace psychedelic algebra

Related Articles