IaC 101: A Quick Dive into Infrastructure as Code

Pasindu Kavinda
5 min readDec 9, 2023

--

Overview

We use cloud services every day, and sometimes it’s a bit tricky. Luckily, Infrastructure as Code (IaC) comes to the rescue, making things smoother. It works with various cloud services, like IaaS and PaaS, automating the setup and management of our infrastructure components.

Usually, Three ways to manage Resources.

  • Manual
  • Ad-hoc Automation
  • Infrastructure as Code

Manual means hands-on, human-driven resource management, which may lead to errors and take time. Consider this when provisioning and configuring infrastructure manually — how do we handle it, and how can we collaborate?

At the simplest level of infrastructure automation, Ad-hoc integrations use a mix of old and new technology. This level is chaotic and fragmented, with different processes across the organization. Also, These custom integrations and scripts may lack visibility, limiting their reuse within the organization. What’s Next?

Here comes Infrastructure as Code into play. IaC solves those headaches and fills out missing parts in the DevOps workflow.

Writing Infrastructure as Code

  • JSON
  • DSL (Domain-Specific Language)
  • Common Programming Languages (Pulumi with Python, Ts, etc.)

Popular IaC tools

Different cloud companies have their special tools for managing infrastructure:

  • AWS uses CloudFormation.
  • Azure uses Resource Manager.
  • GCP uses Deployment Manager.

But here’s the catch: these tools only work in their clouds. For example, CloudFormation only works on AWS, and Deployment Manager only works on GCP. People usually write the instructions for these tools in either JSON or YAML.

Also, We can use widely used languages such as Python and JavaScript to write Infrastructure as Code.

Many IaC tools can be used to write Infrastructure as Code (IaC)

  • Terraform is a super popular IaC tool by HashiCorp. It’s free for everyone and can handle many different cloud services including AWS, Azure, GCP, DigitalOcean, GitHub, Cloudflare, and many others. It uses HCL (HashiCorp Configuration Language).
  • Pulumi is a special IaC tool. It’s different from other tools because it can work with many programming languages like Python, JavaScript, C#, Go, and TypeScript. Since it supports more languages, we can access more tools and ways to build and test our infrastructure.
  • Ansible is Red Hat’s orchestration and configuration management tool. We can use Ansible right away because it comes with a strong default setup that doesn’t require extra configuration.
  • Chef is a Policy-Based configuration management tool. It automates configuration management, enabling DevOps teams to define policies that are repeatable, consistent, and reusable.
  • Puppet is a configuration management tool for IaC and uses a server-agent architecture. It uses Puppet’s Domain-Specific Language (DSL).
  • Vagrant is an open-source IaC tool by HashiCorp. Vagrant allows us to create and set up lightweight, reproducible, and portable development environments.
  • Saltstack is an open-source configuration management tool built on Python, that empowers us to swiftly provision, deploy, and configure infrastructure on any platform.

Also, we can categorize IaC tools into four categories.

Provisioning Tools

Include Terraform, AWS CloudFormation, and Google Cloud Deployment Manager.

Configuration Management Tools

Include Puppet, Chef, Ansible, and SaltStack.

Server Templating Tools

Include Docker, Vagrant, and Packer.

Orchestration Tools

Include Kubernetes, Docker Swarm, and Red Hat OpenShift.

Declarative vs imperative approaches to IaC

There are two approaches to working with Infrastructure as Code.

In the declarative approach, we specify the desired state of the infrastructure without detailing the step-by-step process to reach that state (State Management). We define the name and properties of the infrastructure resources we want to create, and the IaC tool automatically determines the steps to achieve that desired outcome. The benefit of the declarative approach is that it is more idempotent. It is like doing a task that gives the same result no matter how many times we do it. In the declarative approach, where we just say the final state, we always get the same result, no matter where we begin. We don’t have to handle the complicated details of each step. The declarative approach is the popular approach, and it ensures consistency and reliability. Terraform is a declarative IaC tool.

On the other hand, the imperative approach is usually simpler and flexible. imperative IaC is step-by-step, concentrating on the process of achieving the desired state. But this can make things more complicated. Tools like Ansible use the imperative approach and don’t keep track of the state, this can sometimes cause repetitive tasks. We can automate detailed and complex setups by using multiple layers of commands together. This approach is more efficient and easier to optimize for a specific purpose than the declarative approach.

Have Benefits of IaC?

  • Increase in speed of deployments: IaC speeds up deployments by automating infrastructure setup, ensuring consistency, and promoting code reuse. This results in faster and more reliable deployment processes for software applications and services.
  • Cost reduction: IaC is crucial for making resource allocation and cost management more effective in using cloud services.
  • Reduce errors: IaC’s automated processes enhance the reliability of infrastructure setups, minimizing the risk of errors caused by humans.
  • Version Control & Collaboration: Infrastructure code can be versioned like software source code, keeping track of changes, enabling rollbacks, and promoting collaboration.
  • Scalability & Reproducibility: IaC makes it simple to adjust the size of infrastructure as required, deploying resources to match changes in demand. Also, IaC helps easily recreate environments for testing and debugging, ensuring what works in one setting will work in another.

Why does IaC matter for DevOps?

IaC is crucial for DevOps and CI/CD. It makes things easier for developers by letting them use scripts to quickly set up infrastructure, reducing delays in deploying applications. This also frees sysadmins from handling time-consuming manual tasks.

CI/CD needs constant automation and monitoring throughout the application life cycle, demanding a steady environment. IaC aligns development and operations, cutting errors and inconsistencies. With a shared deployment description, IaC supports to DevOps approach, encouraging teamwork.

IaC keeps things consistent in all environments, even in production, by creating identical setups every time it’s used. This removes the need for unique configurations, ensuring reproducibility and consistency in the production environment.

DevOps best practices go into infrastructure using IaC, putting it through the same CI/CD pipeline. Testing and version control are added to the infrastructure code, improving efficiency and reliability in development and deployment.

So, How do we start?

IaC is an iterative step-by-step process. As our project grows, we might have to update our infrastructure code to fit new needs. It’s smart to begin small and slowly use more IaC as we go. There are various resources to getting started in IaC. Here are the key steps:

  1. Define Infrastructure Needs
  2. Choose IaC Tools
  3. Write Infrastructure Code & Version Control
  4. CI/CD Integration
  5. Documentation
  6. Deploy and Monitor

📌 I hope to write practical articles about IaC. Please check them out.

Also, please check out the Pasindu Kavinda’s HashNode blog.

--

--

Pasindu Kavinda

Hey, Medium community! I'm a software engineer passionate about new technologies. Join me on this journey as I navigate the ever-changing tech landscape.