Top

Why Infrastructure as Code (IaC) can improve app security

IaC: Infrastructure is one of the fundamental principles of a software development process: it is directly responsible for the stable operation of an application. This infrastructure can range from servers, load-balancing systems, firewalls and databases to complex container clusters. Infrastructure considerations apply beyond production environments as they extend to the entire development process. They include tools and platforms like CI/CD, staging environments, and testing tools. These elements increase as the level of complexity of the software increases.

The traditional approach for manual infrastructure management becomes an unscalable solution to meet the needs of modern DevOps-based rapid software development cycles. And that is how Infrastructure as Code (IaC) has become the de facto solution in development. IaC enables the growing needs for infrastructure changes to be met in a scalable and traceable manner. It is important to note that IaC is not a derivative of infrastructure as a service (IaaS) since they are two different concepts. IaC is not limited to cloud-based resources, like IaaS, but applies to various environments, including on-premises.

Infrastructure as Code or IaC is the provisioning and managing of infrastructure defined through code rather than through a manual process. Because infrastructure is defined as code, it allows users to modify and deploy configurations while ensuring the desired state easily. This means that reproducible infrastructure configurations can be created. In addition, defining infrastructure as code makes it possible to – Easily integrate infrastructure into control mechanisms to create traceable and auditable changes. – Give the ability to introduce extensive automation for infrastructure management. All this leads to integrating IaC into CI/CD pipelines as an integral part of the SDLC. – Eliminate the need for manual provisioning and management of infrastructure to keep all environments within the defined configuration.

Mutable and immutable

There are two types of IaC: changeable and immutable. Mutable infrastructures change and transform easily, while immutable infrastructures are incapable of change. As IaC emerges as the new standard for IT best practices, infrastructures are shifting from traditional modes of operation to more immutable ones. This is because IT departments are striving to get to continuous release, with versioning and test automation built into the DevOps process. The goal of all this is to enable IT to deploy a package and its dependencies consistently, with identical environments each time. Related solutions, products or services must be constantly updated to continue to meet evolving needs. IT professionals must deal with each server and switch individually, a time-consuming activity to identify problems and generate solutions.

There are two types of IaC: changeable and immutable
There are two types of IaC: changeable and immutable

Immutable IaC represents a simplified scenario under which each component must follow exact specifications, with no exceptions. When a change is needed, the infrastructure provisioning is done according to the new requirements by replacing the old IaC. This uniformity of the basic infrastructure makes it possible to develop and deploy applications more stably and quickly. When dealing with IaC tools, there are two main differentiating approaches to writing code: declarative and imperative. An imperative approach allows users to specify the exact steps for a change; the system does not deviate from the specified steps. A declarative approach only asks users to define the final requirement: the specific tool or platform handles the steps to achieve the defined requirement. The declarative approach is preferred in most infrastructure management use cases because it offers greater flexibility during management.

Different kinds of programming

In imperative programming, you specify a list of steps the IaC must follow to provide a new resource. Simply ask the tool how to create each environment using a sequence of command imperatives. Declarative programming is a popular approach to infrastructure as code. You have to define the desired end state of the final configuration, and the IaC solution determines how to get there. Declarative programming is repeatable, meaning it can execute the IaC commands repeatedly and still get the same result. The declarative paradigm also fits well with configuration drift (the inevitable slow changes to your infrastructure over time) because the provisioning steps of the IaC tool are not explicitly defined. The biggest disadvantage of the declarative approach is that you give up a lot of control over the individual steps in the provisioning process.

It is also not the best choice for small fixes and updates that can be handled by a simple CLI (command-line interface) script: declarative programming can overcomplicate things and slow down the process. Imperative requires more scripting knowledge, however, because you need to write commands for each provisioning step. This gives you control over how you execute infrastructure tasks, which is ideal when there is a need to make small changes, optimize processes for a specific purpose, or account for software changes. The biggest initial challenge for the imperative approach is that it requires a high level of skill with the programming language, which infrastructure teams in the early stages of the DevOps journey may still need to gain.

Imperative IaC scripts are often less idempotent: predefined steps can lead to different results depending on the environment. In addition, imperative IaC scripts are so consequential that an error with one step can cause the entire process to fail.

How to choose

Overall, many organizations seeking to automate and orchestrate their DevOps infrastructure fully prefer the declarative approach. One can use declarative IaC tools to create highly repeatable and adaptable configuration scripts without years of coding experience. However, for those new to infrastructure automation or who do not need a full orchestration platform, the imperative approach is often more straightforward to manage. Comparing declarative and imperative programming for IaC is only one step in the DevOps infrastructure automation process. Infrastructure-as-code tools impose configuration from the model via push or pull methods. In the push method, a centralized server sends the desired configuration to one or more specific systems.

The pull method is initiated by a request to a centralized server from one or more systems in the infrastructure. Tools are typically designed by default for push or pull code distribution, but they can be configured for specific instances. These tools should also be able to restore code changes, such as in the event of unexpected problems from an update. Examples of infrastructure tools as code include AWS CloudFormation, Red Hat Ansible, Chef, Puppet, SaltStack, and HashiCorp Terraform. Some are based on a domain-specific language (DSL), while others use a standard format, such as YAML and JSON. Therefore, when selecting a tool, organizations should consider the target deployment. For example, AWS CloudFormation is designed to provision and manage infrastructure on AWS and works with other AWS offerings. Alternatively, Chef works with local servers and Infrastructure-as-a-Service solutions from multiple cloud service providers.

Antonino Caffo has been involved in journalism, particularly technology, for fifteen years. He is interested in topics related to the world of IT security but also consumer electronics. Antonino writes for the most important Italian generalist and trade publications. You can see him, sometimes, on television explaining how technology works, which is not as trivial for everyone as it seems.