chapter two

2 Writing infrastructure as code

 

This chapter covers

  • How the current infrastructure state affects the reproducibility of infrastructure
  • Detecting and remediating infrastructure drift due to mutable changes
  • Implementing best practices for writing reproducible infrastructure as code

Imagine you’ve created a development environment for a “hello-world” application. You built it organically, adding new components as you needed them. Eventually, you need to reproduce the configuration for production use, which people can publicly access. You also need to scale production across three geographic regions for high availability.

To do this, you must create and update firewalls, load balancers, servers, and databases in new networks for the production environment. Figure 2.1 shows the complexity of the development environment with the firewall, load balancer, server, and database and the components you need to reproduce in production. However, the figure outlines the differences between development and production. The production configuration needs three servers for high availability, expanded firewall rules to allow all HTTP traffic, and stricter firewall rules for the servers to connect to the database. After reviewing all of the differences, you might have a lot of questions about the best and easiest way to make the changes!

2.1 Expressing infrastructure change

2.2 Understanding immutability

2.2.1 Remediating out-of-band changes

2.2.2 Migrating to infrastructure as code

2.3 Clean infrastructure as code

2.3.1 Version control communicates context

2.3.2 Linting & formatting

2.3.3 Naming resources

2.3.4 Variables & constants

2.3.5 Parametrize dependencies

2.3.6 Keeping it a Secret

2.4 Exercises and Solutions

2.5 Summary