The Magic of Terraform: Redefining Infrastructure Deployment Like Building Lego Blocks

by Zvonimir Tomesic, CEO

In the formative days of our digital venture, Amazon Web Services (AWS) was our go-to platform for infrastructure deployment. Over time, we realized that while AWS was a reliable partner, there was considerable overhead we encountered during the initial phases of research and system infrastructure definitions. It wasn't the tool but the process which necessitated a change. And voila, there it was – Terraform! An open-source infrastructure-as-code tool, Terraform enabled us to provision and manage infrastructure in any cloud with a predictability that was refreshing.

Let's delve into how Terraform reshaped our approach to infrastructure, making it akin to assembling Lego blocks.

The Power of Modular Approach

Terraform revolutionized our infrastructure strategy by bringing a modular approach into play. We could now create each part of the infrastructure as a self-reliant, testable, and repeatable piece of code. This was particularly beneficial since many of our steps for clients were similar or nearly identical, offering us a scope to streamline and simplify the process.

But how does Terraform accomplish this?

Its language is declarative, focusing on the intended goal rather than the steps to reach it. The ordering of blocks and the files they reside in are not significant. Terraform only considers implicit and explicit relationships between resources when determining an order of operations.

Launching Your Infrastructure

Now, let's say we want to launch an EC2 instance on AWS using Terraform. To kickstart this process, we would first define a provider and then specify the resource we wish to deploy:

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

The above block tells Terraform that we will be using AWS as our provider, and we plan to deploy our infrastructure in the eu-central-1 region.

Next, we define the resource – in this case, an EC2 instance:

resource "aws_instance" "example" {
 ami = "<AMI_INSTANCE>"
 instance_type = "<INSTANCE_TYPE>"
}

With the provider and resource defined, we can initialize the process using:

terraform init

This command is important as it helps Terraform scan the code, determine the providers we’re using, and download the necessary code. Once this is done, we can run:

terraform plan

This command gives a preview of what Terraform intends to do without making any actual changes. Once we're confident about the changes, we can create the Instance by running:

terraform apply

This command applies the infrastructure to the specified cloud provider. The output of this command will be a terraform state file tf.state which contains the current state of the system post-application.

Leveraging Reusable Modules

Our application of Terraform has evolved to incorporate reusable modules. Each module typically follows a similar structure:

├── Makefile
├── README.md
├── examples
│   └── vpc_standard
│       ├── README.md
│       ├── main.tf
│       ├── outputs.tf
│       ├── terraform.tfvars
│       ├── variables.tf
│       └── versions.tf
├── main.tf
├── outputs.tf
├── tests
│   ├── Makefile
│   ├── go.mod
│   ├── go.sum
│   └── vpc_standard_test.go
├── variables.tf
└── versions.tf

Each file has its unique role:

  • main.tf contains the main building blocks for the deployment
  • outputs.tf contains output values that can be used in other modules
  • variables.tf contains input variables that can alter the build of our modules
  • versions.tf contains all the providers and their versions
  • examples/ houses an example implementation of the module used in our tests
  • tests/ holds the test scenario for our module

For a specific client infrastructure deployment, we create a terraform-standard-deployment module which combines small modules into one representative of the entire infrastructure. This approach optimizes reusability, reduces overhead, and speeds up the transition to the project's development phase.

With Terraform, our infrastructure deployment process is no longer a tedious task. Instead, it resembles a fun session of building with Lego blocks - modular, simple, and engaging.

P.S.

I higly recommend using terraform docs for generating readme files (you can even inject them into your readme files):

terraform-docs markdown table --output-file README.md --output-mode inject .

More articles

Number System: Binary, Decimal, Octal, Hexadecimal

Dive into the world of number systems, exploring the intricacies and practical applications of binary, decimal, octal, and hexadecimal conversions in computing and digital technology.

Read more

Linux Schedulers - Ensuring Optimal System Performance

Evolution and functionality of Linux schedulers, from simple round-robin to the Completely Fair Scheduler and real-time alternatives, emphasizing their critical role in balancing system responsiveness, fairness, and efficiency across various computing environments.

Read more

Tell us about your project

Our offices

  • Zagreb
    Bozidara Magovca 14
    10000, Zagreb, Croatia