What is Infrastructure as code (IaC)?

A method of describing the configuration of infrastructure such as servers, networks and storage in code (definition files) rather than by hand, and creating and changing it automatically with tools. With declarative tools you write the desired configuration, and the tool applies only the difference from the actual state, so applying the same definition any number of times gives the same result (idempotence). Managing the definitions in Git or similar makes change history, review and reproducing identical environments easy.

Japanese name: IaC(Infrastructure as Code)

Infrastructure as code (IaC) appears in 1 practice question across 1 topic, including Cloud and Container Basics.

Field: Cloud and Container Basics

Declarative versus procedural

In a declarative style you write the result, such as "two virtual machines", and the tool compares it with the current state to decide what to do. In a procedural style you write operations in order, such as "create one virtual machine", so you must take the current state into account; running it twice could create four machines. Declarative methods, which make idempotence easy, are widely used for infrastructure configuration.

Configuration drift

When someone changes a setting by hand, for example through a management console, the actual state drifts away from the code. A declarative tool shows the difference before applying, so such drift can be found. If every change is made through the code, the state of the environment keeps matching the code.

Review and reproducibility

Managing definition files in Git like programs records who changed what and when, and lets changes be reviewed before they are applied. Creating staging and production from the same definitions also reduces problems caused by differences between environments. Confidential values such as passwords are not written directly into definition files but managed with dedicated mechanisms.

Try it: questions about Infrastructure as code (IaC)

An environment was created by applying, with a declarative IaC (Infrastructure as Code) tool, a file that defines a network and two virtual machines. Immediately afterwards, the same file is applied again without any changes. Which of the following is what happens?

  1. AThere is no difference between the definition and the actual state, so nothing is changed
  2. BAnother identical set is created, giving four virtual machines
  3. CBecause the environment already exists, the second apply stops with an error
Show answer and explanation

Answer: A. There is no difference between the definition and the actual state, so nothing is changed

With declarative IaC, the code describes the desired configuration rather than the steps, and the tool compares it with the actual state and creates, changes or deletes only what differs. Applying the same definition any number of times therefore gives the same result (idempotence), and the second apply changes nothing. If someone changes a setting by hand and the actual state drifts from the definition (configuration drift), the next apply brings it back to the defined state. Managing the definitions in Git also allows history checks and reviews in the same way as for programs.

FAQ

Does IaC eliminate manual configuration?

Much of the work can be turned into code, but some manual steps, such as the initial authentication setup, remain. What matters is not to leave manual changes as they are but to bring them into the code, keeping the code and the actual state consistent.

How is IaC related to containers?

IaC is used to build foundations such as networks, virtual machines and clusters, and the applications running on them are often delivered as container images. Kubernetes manifests follow the same idea as IaC in that they declare a desired state.

Infrastructure as code (IaC) in the explanations

Questions that mention Infrastructure as code (IaC) (1)

  1. An environment was created by applying, with a declarative IaC (Infrastructure as Code) tool, a file that defines a network and two virtual machines. Immediately afterwards, the same file is applied again without any changes. Which of the following is what happens?

Books on Cloud and Container Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Git Autoscaling Availability zone Blue-green deployment Canary release Container Container image Docker Immutable infrastructure Kubernetes

Back to the glossary