1

Infrastructure is one of the core tenets of the software development process - it is directly responsible for the stable operation of software applications. This infrastructure ranges from servers, load balancers, firewalls and databases all the way to complex container clusters.

Considerations for infrastructure should not only apply to production environments, as they pervade the entire development process, but also tools and platforms such as CI/CD platforms, staging environments, and testing tools. As software products increase in complexity, so too do these infrastructure considerations. To meet the demands of the modern, rapid software development cycle of DevOps, the traditional approach of manually managing infrastructure quickly becomes an unscalable solution. This is why IaC has become the de facto solution in development today.

What is Infrastructure as Code?

IaC, Infrastructure as Code, is the process of provisioning and managing infrastructure through code rather than manually defined. IaC takes most of the provisioning work from developers, who can execute scripts to get the infrastructure ready. This way, application deployments are not blocked by waiting for infrastructure, and system administrators do not need to manage time-consuming manual processes.

Here are the step-by-step instructions on how creating an IaC environment works:

  • Developers define configuration parameters in a domain-specific language (DSL).
  • Instruction files are sent to the main server, management API or code repository.
  • The IaC platform creates and configures the infrastructure as directed by the developer.

By treating infrastructure as code, users don't have to configure the environment every time they develop, test, or deploy software. All infrastructure parameters are saved in files called manifests.

Like all code files, manifests are easy to reuse, edit, copy, and share, making building, testing, preparing, and deploying infrastructure faster and more consistent.

Developers encode configuration files and store them in version control. If someone edits a file, pull requests and code review workflows can check the correctness of the changes.

IaC Best Practices

The implementation of infrastructure automation will require extensive changes and refactoring, which can be quite a burdensome process for an organization. Follow the best practices below if you want to avoid most limitations and make transitions as smooth as possible!

Using CI/CD and QC for IaC's repositories

This will help you keep your code in good quality and get a quick feedback loop (after applying changes) from your DevOps team or developers. Fortunately, there are testing frameworks, such as Terratest for Terraform, that allow us to write actual tests. The sooner you try to cover everything with it, the more you benefit from it, and the fewer unexpected problems occur with your infrastructure. To be sure, you can't predict application errors here, but you can at least have more confidence in your own infrastructure.

Make your infrastructure modular code

Microservice architecture is a popular trend in software development that builds software by developing smaller, modular units of code that can be deployed independently of other components of the product.

The same concept applies to IaC. Infrastructure can be broken down into individual modules or stacks and combined in an automated fashion.

This method has the following advantages:

  • First, there is flexibility in controlling access to parts of the infrastructure code. For example, junior engineers may not be familiar with or have expertise in certain parts of the infrastructure configuration, and by modularizing the infrastructure code, junior engineers can be denied access to these components until they can catch up.
  • Furthermore, modular infrastructure naturally limits the number of changes that can be made to the configuration. Smaller changes make bugs easier to detect and make your team more flexible.

If IaC is used to support a microservices architecture, configuration templates should be used to ensure consistency as the infrastructure scales to large server clusters. This will ultimately be very valuable for configuring servers and specifying how they should interact.

Continuous testing, integration and deployment

A continuous process of testing, integration, and deployment is a great way to manage all changes that may be made to your infrastructure code.
Testing should be strictly applied to your infrastructure configuration to ensure there are no post-deployment issues. Depending on your needs, a series of tests should be performed. Automated tests can be set up to run every time the configuration code is changed.

The security of the infrastructure should also be continuously monitored and tested. DevSecOps is an emerging practice in which security professionals work with developers to continuously integrate threat detection and security testing throughout the software development lifecycle, rather than just investing at the end. By increasing collaboration between testing, security, and development teams, bugs and threats can be identified early in the development lifecycle, thereby minimizing them when they go live.

With a good continuous integration process, these configuration templates can be used multiple times in different environments such as development, testing, and QA. Developers can then use consistent infrastructure configurations across these environments. This continuous integration will reduce the risk of bugs that could be detrimental to the application when deploying the infrastructure into production.

maintain version control

These configuration files will be under version control. Because all configuration details are written in code, any changes to the codebase can be managed, tracked, and coordinated.

As with application code, source control tools such as Git, Mercurial, Subversion, etc. should be used to maintain versions of the IaC codebase. This not only provides an audit trail for code changes, but also enables collaboration, peer review and testing of IaC code before it goes live.

Code branching and merging best practices should also be used to further increase developer collaboration and ensure that updates to IaC code are properly managed.

If you're just getting started with IaC, don't try to automate everything from the start. The reason is the speed of change. Once your platform becomes more or less stable, you will be able to automate its provisioning and maintenance.

Challenges of IaC

The benefits of IaC are many, but this model does present some challenges that need to be addressed, which can be understood and properly addressed prior to the implementation process.

Configuration drift

Regardless of how consistently or how often you configure your servers, there can be configuration drift in the long run. This is why, after establishing an IaC workflow, it is important to ensure that there are no external interferences. Every time you need to modify your infrastructure, you must ensure that you follow a pre-established maintenance workflow. This principle is known as infrastructure immutability - that infrastructure should behave exactly as specified, and if changes are required, a whole new set of infrastructure is provided and outdated infrastructure is completely replaced.

If you still end up making inconsistent changes to similar groups of systems, some of those changes will inevitably differ from others, which can lead to changes in configuration.

false potential duplication

Although the implementation process and machine creation of IaC relies heavily on automation, there are still some parts of the whole process that need to be done manually. Writing parent code is one of those processes, and when it comes to manual work, there is always the potential for error. Even in an environment where QA checks are regular and consistent, people can make mistakes or overlook critical things.

As a side effect of automation, these errors can occur on multiple machines and can create as many security holes as possible. Remember that almost all cloud vulnerabilities come from misconfiguration. To ensure safety from start to finish, it is recommended to carefully examine the code that generates the IaC architecture, which can be achieved through rigorous and extremely consistent testing and a thorough review process. Of course, these additional processes often increase the corresponding expenses.

Infrastructure as code is slowly but surely becoming the norm for organizations looking to automate and deliver faster. Applications can be developed faster only through simplified workflows and an improved development environment.


陈哥聊测试
158 声望3.3k 粉丝

资深敏捷测试顾问,国内知名项目管理软件禅道团队成员。