Reader small image

You're reading from  The Linux DevOps Handbook

Product typeBook
Published inNov 2023
PublisherPackt
ISBN-139781803245669
Edition1st Edition
Concepts
Right arrow
Authors (2):
Damian Wojsław
Damian Wojsław
author image
Damian Wojsław

Damian Wojsław has been working in the IT industry since 2001. He specializes in administration and troubleshooting of Linux servers. Being a system operator and support engineer he has found DevOps philosophy a natural evolution of the way sysops work with developers and other members of the software team.
Read more about Damian Wojsław

Grzegorz Adamowicz
Grzegorz Adamowicz
author image
Grzegorz Adamowicz

Grzegorz Adamowicz has been working in the IT industry since 2006 in a number of positions, including Systems Administrator, Backend Developer (PHP, Python), Systems Architect and Site Reliability Engineer. Professionally was focused on building tools and automations inside projects he is involved in. He's also engaged with the professional community by organizing events like conferences and workshops. Grzegorz worked in many industries including Oil & Gas, Hotel, Fintech, DeFI, Automotive, Space and many more.
Read more about Grzegorz Adamowicz

View More author details
Right arrow

Leveraging Infrastructure as Code

In today’s digital landscape, managing and deploying infrastructure is a complex and time-consuming process. Traditionally, infrastructure deployment involves manually configuring each server, network, and storage device. This process is not only time-consuming but also prone to errors and inconsistencies. Infrastructure as Code (IaC) solutions provide an automated way to manage and deploy infrastructure. IaC solutions allow developers to treat infrastructure as code, enabling them to define, manage, and provision infrastructure in the same way they do with code.

In this chapter, we will explore IaC solutions, with a focus on Terraform. Terraform is an open source IaC tool that enables developers to define, manage, and provision infrastructure across multiple cloud providers and on-premises data centers. HashiCorp, the owner of Terraform and many other automation tools, changed the license from Mozilla Public License (MPL) version 2.0 to...

Technical requirements

For this chapter, you will need a system capable of running Terraform. Terraform is a single binary program written in the Go programming language. Its installation is straightforward and is explained on the HashiCorp Terraform project page (https://developer.hashicorp.com/terraform/downloads). HashiCorp is the company behind Terraform and other cloud management tools that have become de facto standards in the DevOps world. You will also need an AWS account. AWS provides a Free Tier of services for a limited time. We are using services that have free tiers at the time of writing this book. Before you run the examples, please consult the AWS Free Tier listing to avoid unnecessary costs.

What is IaC?

IaC is a software development practice that involves defining and managing infrastructure through code. In essence, it means that infrastructure is treated as if it were a piece of software, and is managed through the same processes and tools. IaC solutions enable developers to define, provision, and manage infrastructure using code, instead of manually configuring servers, networks, and storage devices. This approach to infrastructure management is highly automated, scalable, and efficient, allowing organizations to reduce deployment times and improve consistency and reliability.

IaC solutions come in different forms, including configuration management tools, provisioning tools, and cloud orchestration tools. Configuration management tools, such as Ansible and Chef, are used to manage the configuration of individual servers or groups of servers. Provisioning tools, such as Terraform and CloudFormation, are used to provision and configure infrastructure resources. Cloud...

IaC versus Configuration as Code

You might be wondering, didn’t we just cover this in Chapter 11, when we spoke about Ansible? The answer is no, we didn’t. There’s a very distinctive difference between IaC and Configuration as Code (CaC). IaC tools are concerned with exactly that: infrastructure. This means networking, DNS names, routes, and servers (VM or physical) up to the installation of the operating system. CaC is concerned with what lives inside the operating system. People try to use one tool for everything, so you’ll see modules for Ansible that can configure switches and routers, but the tool shines best where it is intended to be used. Nobody is going to die if you mix these two, but your life will become more difficult.

IaC projects worth knowing

Since the rise of the public cloud, especially AWS, the need for a repeatable and reliable way of setting up an infrastructure and configuring cloud services started to grow as well. Since then, a lot of tools have come to be and more of them are being developed. In this section, we will review the most popular and innovative tools out there.

AWS CloudFormation

AWS CloudFormation is a popular IaC tool offered by Amazon Web Services (AWS) to automate the provisioning of AWS resources. It was first released in 2011 and has since become a widely used tool for managing infrastructure in the cloud.

CloudFormation allows you to define the infrastructure in a declarative language, such as YAML or JSON, and then create, update, or delete stacks of resources based on those definitions. This allows for consistent and reproducible infrastructure deployments, as well as easy rollback and version control. It’s not all sparkles and rainbows, though –...

Terraform

In this section, we are going to introduce Terraform, one of the most widely used IaC solutions in the wild.

Terraform is an IaC tool developed by HashiCorp. The rationale behind using it is similar to using Ansible to configure your systems: infrastructure configuration is kept in text files. They are not YAML, as with Ansible; instead, they are written in a special configuration language developed by HashiCorp: HashiCorp Configuration Language (HCL). Text files are easily versioned, which means that infrastructure changes can be stored in a version control system such as Git.

Actions performed by Terraform are more complicated than those you’ve seen in Ansible. A single HCL statement can mean setting up a whole bunch of virtual servers and routes between them. So, while Terraform is also declarative like Ansible, it is higher level than other tools. Also, contrary to Ansible, Terraform is state-aware. Ansible has a list of actions to perform and on each run...

HCL in depth

HCL is a configuration language that’s used by several HashiCorp tools, including Terraform, to define and manage IaC.

HCL is designed to be easy to read and write for both humans and machines. It uses a simple syntax that is similar to JSON but with a more relaxed structure and support for comments. HCL files typically have an .hcl or .tf file extension.

HCL uses curly braces to define blocks of code, and each block has a label that identifies its type. Within each block, we define attributes using a key-value syntax, where the key is the attribute name and the value is the attribute value. We can also define objects using curly braces, as shown in the example with the tags object.

Variables

In HCL, variables are defined using the variable block. Here’s an example of how to define a variable in HCL:

variable "region" {
  type = string
  default = "eu-central-1"
}

In this example, we define a variable...

Terraform examples with AWS

In this section, we will create two sample modules to demonstrate how you would go about creating one and what you will need to consider when choosing the way it is supposed to create resources. The module we are going to create will be able to create one or more EC2 instances, a security group attached to it, and other needed resources, such as an instance profile. It will do almost everything we went through in Chapter 10, but with the use of the AWS CLI.

EC2 instance module

Let’s create a module that will be able to create EC2 instances. Consider the following directory structure:

├── aws
│   └── eu-central-1
└── modules

The modules directory is where we will put all our modules, aws is where we will keep our AWS infrastructure, and eu-central-1 is the code of the infrastructure for the Frankfurt AWS region. So, let’s go ahead and start with...

Summary

In this chapter, we introduced the concept of IaC. We explained why it is an important method of managing and developing your infrastructure. We also introduced some tools that are quite popular in this way of working. As a tool of choice, we explained Terraform – probably the most widely used one.

In the next chapter, we are going to show you how you can leverage some online tools and automation to build pipelines for CI and CD.

Exercises

Try out the following exercises to test what you’ve learned in this chapter:

  1. Create a module that will create an S3 bucket with enabled server-side encryption.
  2. Add an instance profile to the module we’ve created using the same IAM policy that we used in Chapter 10.
  3. Use the count meta-argument to create two instances.
lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Linux DevOps Handbook
Published in: Nov 2023Publisher: PacktISBN-13: 9781803245669
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (2)

author image
Damian Wojsław

Damian Wojsław has been working in the IT industry since 2001. He specializes in administration and troubleshooting of Linux servers. Being a system operator and support engineer he has found DevOps philosophy a natural evolution of the way sysops work with developers and other members of the software team.
Read more about Damian Wojsław

author image
Grzegorz Adamowicz

Grzegorz Adamowicz has been working in the IT industry since 2006 in a number of positions, including Systems Administrator, Backend Developer (PHP, Python), Systems Architect and Site Reliability Engineer. Professionally was focused on building tools and automations inside projects he is involved in. He's also engaged with the professional community by organizing events like conferences and workshops. Grzegorz worked in many industries including Oil & Gas, Hotel, Fintech, DeFI, Automotive, Space and many more.
Read more about Grzegorz Adamowicz