DAY_43/90 => Infrastructure as Code(IaC)

Faisal Kuzhan
11 min readMar 8, 2024

--

In this blog, we will see about Infrastructure as Code🏗️ and about Configuration Management🧰 which is one of the type of IaC in detail.

With IaC, a descriptive model is used for infrastructure management. To name a few examples of infrastructure: networks, virtual computers, and load balancers. Using an IaC model always results in the same setting.

Throughout the lifecycle of a product, Configuration Management (CM) ensures that the performance, functional and physical inputs, requirements, design, and operations of that product remain consistent.

Infrastructure as Code(IaC)

What is Infrastructure as Code?

Infrastructure as code, also referred to as IaC, is an IT practice that codifies and manages underlying IT infrastructure as software. The purpose of infrastructure as code is to enable developers or operations teams to automatically manage, monitor and provision resources, rather than manually configure discrete hardware devices, operating systems (OSes), applications and services.

The infrastructure can be networks, virtual machines, load balancers, and connection topologies.

IaC uses higher level or descriptive language to code more versatile and adaptive provisioning and deployment processes. This code is typically written in YAML or JSON.

Version control is an important part of IaC, and your configuration files should be under source control just like any other software source code file. Deploying your infrastructure as code also means that you can divide your infrastructure into modular components that can then be combined in different ways through automation.

Benefits of using IaC

There are many benefits to using IaC, including:

  1. Cost Reduction: Time-consuming infrastructure configuration, are automated by IaC, the engineers or IT experts can finish these tasks in no time and focus on other mission-critical tasks. This would help minimize costs from necessary tasks and improve other functions according to their demands.
  2. Reduced Errors: IaC solves issues as automation reduces the risks of human-made mistakes by cutting down long processes.
  3. Improved Security Strategies: With IaC, computing, storage, and networking services are all provisioned with code and deployed the same way, often using a private or public cloud. This can also be the case for security standards. They can be easily created and deployed. This would enhance other strategies as this is a way to avoid security gatekeeper review and approval for almost every security change. This would prove most beneficial for infrastructures requiring tight security, resulting in multiple security passes.
  4. Increased Efficiency: There are many processes in building, monitoring, and managing infrastructure structures, but as IaC has made it possible to automate almost every process, the work can be two times faster. Automation stretches from significant procedures like virtualization to user account management, databases, network management, and even minor operations like adding environments and resources when needed or shutting down when it is not the case. This automation feature promises faster and simpler procedures.
  5. Improved Consistency: IaC improves consistency during the activities like deployment and configuration by preventing waste of valuable resources, unwanted downtime, and setbacks that can cause inconsistencies in the configuration. These inconsistencies can be challenging to remove when discovered.
  6. Self Documentation: IaC creates detailed reports and documentation of every process. For example, When employees leave a company or a person is setting up the code model for another person, IaC documents every action, process, and change. IaC also tracks and tests every configuration like a code. This documentation can be a yardstick for a new employee or configuration manager.
  7. Eliminate Configuration Drift: This implies that one can deploy the code many times, with the first deployment being the actual deployment and subsequent deployments having no essential effect.

Common Tools Used for IaC

  1. Terraform
  2. AWS CloudFormation
  3. Azure Resource Manager (ARM)
  4. Google Cloud Deployment Manager

Some Common Applications of IaC

  1. With IaC, you can define the entire infrastructure setup, including server configurations, networking, and security rules, using tools like Terraform or AWS CloudFormation.
  2. You can use IaC to create isolated testing environments, deploy the application, run tests, and tear down the resources after testing is complete.
  3. You can define scaling policies and load balancers, which will automatically adjust the infrastructure based on traffic patterns.
  4. You can use tools like Helm (Kubernetes package manager) to define the entire application stack, including deployments, services, and ingress rules, in a single configuration file.
  5. IaC can be used to define network infrastructure, such as virtual private clouds (VPCs), subnets, security groups, and route tables. This allows you to create and manage complex network topologies, ensuring secure and reliable communication between different components of your application.
  6. In disaster recovery scenarios, IaC can be used to quickly recreate the entire infrastructure stack in a different region or cloud provider if the primary infrastructure becomes unavailable. This allows for better resilience and ensures minimal downtime in case of a disaster.

How does infrastructure as code work?

With the imperative method, the developer states the steps the IaC is to follow to achieve desired solutions. This method is organized around how a human being thinks. The user controls how the automation happens, making this method more accessible and efficient. Its primary advantage is that it allows you to automate every detail and layer of command and is most efficient for specific purposes.

On the other hand, in the declarative method, the user simply describes the solution they need, how it should look, and everything that would be in the state of the final solution, leaving the process for the IaC to decide.

With either method, three defining steps are the working principles of IaC in every use case. These steps are:

  1. A developer writes the specifications for the infrastructure in a domain-specific language: Writing these specifications is the first step and is mainly done manually. These specifications would later serve as a configuration template that would be used to create other versions and identify drifts and configurations in changes that one would make later. Some tools like Terraform and Amazon Web Services (AWS) CloudFormation use declarative method languages like SQL, HashiCorp Configuration Language (HCL), etc.
  2. The files containing the specifications are sent to a master server, code repository, or a management API: In conversing with the server, two different methods bring two different outcomes — the Push and Pull methods. The controlling server pushes the configuration to the destination system in the Push method. This implies that instructions would be sent from your server to a central server. But in the pull method, the server that needs configuration acquires it from the controlling server. This implies that your system would pull instructions from the central server.
  3. With these two methods, those specifications were written and communicated with a server, code repository, or management API so that implementation can be done.
  4. With those specifications, the platform creates and configures the computer’s resources with some steps: The last step of IaC is the implementation of specifications to create the environment components and computer resources. As declarative tools are used more than imperative tools, the process of implementation is automatic and these declarative tools can add beneficial modifications to the solution state you requested. But if the imperative method is used, the specifications are implemented according to the steps given by the user. This method is prone to errors as the user might not get the intended solution.

Types of IaC

There are four different types of infrastructure as code, and each is used according to the needed situation:

  1. Scripts: Scripting is a very direct form of IaC. This type of IaC is usually used for simple or one-off tasks and is not advised for complex tasks.
  2. Configuration Management Tools: These tools implement automation by installing and configuring servers. This type of IaC is designed for complex tasks as they are specialized tools built to manage software. They are the most common type of IaC; examples are Ansible, chef, etc.
  3. Provisioning Tools: This type of IaC has more advantages in complex tasks. These tools implement automation by creating infrastructure. Developers use this type of IaC to create infrastructure components. Common examples of some provisioning tools are OpenStack heat and AWS CloudFormation.
  4. Containers and Templating Tools: These tools formulate templates and images pre-loaded with all the libraries and components required to run an application. Data distributed with these tools are easy to manage and have a lower overhead compared to running an entire server. However, container security may be a concern.

Configuration Management

What is Configuration Management?

Configuration Management(CM) is the process of maintaining systems, such as computer hardware and software, in a desired state. It is also a method of ensuring that systems perform in a manner consistent with expectations over time.

In other words, Configuration management (CM) is the practice of maintaining computer systems in a consistent and desirable state. The CM team tracks and manages your organization's software and hardware assets to ensure they are working optimally and are up to date.

Benefits of using Configuration Management

Here are some of the benefits of using configuration management:

  1. Increased Consistency: CM ensures that development, test, and production environments are consistent in your organization. It helps ensure that deployed applications behave in the manner that is expected of them.
  2. By maintaining consistency, organizations can reduce the risk of errors and ensure that applications are deployed in a known and predictable manner.
  3. Increased security: CM can help to identify and track security vulnerabilities. This can help to prevent unauthorized access to systems and data.
  4. Increased compliance: CM can help organizations to comply with regulatory requirements. This can help to avoid fines and penalties.
  5. Greater Efficiency: Configuration management helps automate the deployment process, reducing the time and effort required to deploy software.
  6. Improved Collaboration: Configuration management tools help bring together development and operations teams, enabling them to work collaboratively and deliver software more efficiently.
  7. Disaster Recovery: If the worst does happen, configuration management ensures that our assets are easily recoverable. The same applies to rollbacks. Configuration management makes it so that when we’ve put out bad code, we can go back to the state of our software before the change.
  8. Easier Scaling: Provisioning is the act of adding more resources (usually servers) to our running application. Configuration management ensures that we know what a good state of our service is.
  9. Reduced costs: CM can help to reduce costs by automating tasks and preventing errors.

Common Tools Used for Configuration Management

  1. Ansible
  2. Chef
  3. Puppet

Some Common Applications of Configuration Management

  1. Configuration management tools like Puppet, Chef, and Ansible are commonly used to manage the configuration of servers. These tools allow you to define the desired state of servers, including software packages, users, firewall rules, and other settings.
  2. Configuration management tools can help in managing application configuration files, environment-specific settings, and application parameters.
  3. Configuration management tools can automate the provisioning and configuration of databases, ensuring that databases are set up consistently across different environments.
  4. Tools like Ansible can be used to define and apply network configurations, such as VLANs, access control lists (ACLs), and routing protocols, across the entire network infrastructure.

How does Configuration Management Works?

There are basically four steps involved: -

  1. Identification: The first action towards configuration management is information gathering. Configuration data should be aggregated and compiled from different application environments, development, staging, and production for all the components and services in use. Any secret data like passwords and keys should be identified and securely encrypted and stored. At this point configuration data should be organized into data files that can be pointed to as a central source of truth.
  2. Baseline: After configuration data has been aggregated and organized a baseline can be established. A baseline configuration is a known state of configuration that will successfully operate the dependent software without error. This baseline is usually created by reviewing the configuration of a functioning production environment and committing those configuration settings.
  3. Version Control: Your development project should use a version control system. If not, install Git, initialize a repository for the project, and add the configuration data files to the repository. A word of caution before adding configuration data to a repository: make sure that any secret data like passwords or keys are encrypted with an external key. Secret data accidentally committed to a repository is a huge risk. It needs to be scrubbed from the repositories history or it will be at risk of being exploited.
  4. Auditing: Having configuration data organized and added to a repository enables collaboration and visibility into the system’s configuration. The popular pull request workflow that software teams use to review and edit code can then be applied to configuration data files. This helps build out an audit and accounting system. Any changes applied to the configuration must be reviewed and accepted by the team. This adds accountability and visibility into configuration changes.

IaC vs Configuration Management

Below is the screenshot of difference between IaC and CM: -

Making the Right Choice

The choice of whether to use Infrastructure as Code (IaC) or Configuration Management (CM) depends on the specific needs and requirements of your project and infrastructure management practices. The best approach for your project will depend on several factors, including the size and complexity of your infrastructure, the specific requirements of your project, and your current infrastructure management practices.

IaC is a good option if you need to automate the provisioning of infrastructure and improve the consistency and repeatability of your infrastructure.

CM is a good option if you need to improve the reliability, security, and compliance of your IT systems.

A small tabular form to choose which one of the above two: -

Conclusion

In Conclusion, Infrastructure as Code (IaC) and Configuration Management are integral components of DevOps, streamlining and enhancing the efficiency of software development and operations.

IaC involves representing and managing infrastructure in a declarative manner, allowing for automated provisioning and deployment whereas, Configuration Management complements IaC by ensuring the consistency and desired state of infrastructure and software configurations.

Together, IaC and Configuration Management bring several benefits to DevOps practices. They promote scalability and repeatability, enabling teams to quickly replicate environments and deploy applications consistently across different stages of development and production. This consistency reduces the risk of configuration drift and minimizes errors that may arise from manual intervention.

Additionally, IaC and Configuration Management enhance collaboration between development and operations teams by providing a standardized and version-controlled approach to managing infrastructure and configurations. This facilitates better communication, transparency, and the ability to roll back changes if needed.

In summary, IaC and Configuration Management in DevOps accelerates the delivery of high-quality software, improves collaboration between teams, and ensures the reliability and scalability of infrastructure, ultimately contributing to the overall success of software development and operations processes.

*Suggestions are always welcome.*

~Faisal Kuzhan

--

--

Faisal Kuzhan

DevOps Automation Engineer /* 7x Azure, AWS, GCP */ Terraform Certified | Docker | Terraform | Terraspace | Kubernetes | CI/CD |