Lesson 3 of 6 Infrastructure as Code

Implementing a Configuration Management Strategy

This lesson explains where configuration lives, how it varies, and how drift is handled.

Learning outcomes

After completing this lesson, you will be able to:

  • Separate code, configuration and secrets.
  • Manage differences between environments.
  • Explain how to prevent, detect and correct drift.
  • Compare mutable and immutable infrastructure.

Where configuration lives

A sound strategy starts from a clear separation. Code stays the same across environments. Configuration varies by environment, and it should be stored outside the build artifact, so that one artifact can run anywhere. Secrets are a special kind of configuration that belong in Key Vault. This separation is one of the principles of a twelve-factor application.

Non-secret configuration should be in source control, whether in parameter files, Ansible inventories or a key-value file imported to App Configuration. This gives the same benefits as code review: a person can see that a production setting changed, who changed it and why.

Sort into groups

Code, configuration or secret?

Exam skills 3.5.2

Decide where each item belongs in a twelve-factor style separation.

Log in to try this activity Interactive activities are free with an account.

Managing differences between environments

Environments should differ as little as possible. The aim is one shared definition with a small file of values per environment, and not separate copies that will drift apart.

infra/
  main.bicep                # the same definition everywhere
  environments/
    test.bicepparam         # small differences only
    staging.bicepparam
    production.bicepparam

Labels in App Configuration provide the same pattern for application settings, with a key such as Api:Timeout having a test label and a production label. Promotion then means copying values from one label to the next, ideally from a pipeline.

Drift

Configuration drift occurs when the real state moves away from the defined state, usually because someone made a manual change during an incident. Drift undermines all the benefits of IaC: a redeployment may overwrite an important manual fix, or a test environment may no longer reflect production.

A strategy needs three responses. Prevent drift by limiting direct changes, for example by giving people read-only access to production, and by applying deny settings on deployment stacks. Detect it through Azure Policy compliance, what-if in a scheduled pipeline, or terraform plan. Correct it automatically through Machine Configuration, or by redeploying. When a manual change was truly needed, the fix belongs in the definition too.

The following diagram compares a cycle of configuration drift caused by portal changes with the recommended process.

Breaking the drift cycle. Portal changes made during incidents were overwritten by the next Bicep deployment, causing repeat incidents. Engineers get read access with just-in-time elevation, every emergency change also goes into the Bicep files through a pull request, and a scheduled what-if reports differences.
JIT stands for just-in-time.
Sort into groups

Prevent, detect or correct drift?

Exam skills 3.5.2

Place each measure in the drift response it provides.

Log in to try this activity Interactive activities are free with an account.

Mutable and immutable infrastructure

With mutable infrastructure, servers are updated in place. Over time, each one accumulates a slightly different history. With immutable infrastructure, a server is never changed after deployment: a new image is built and replaces it. Containers and golden images follow this model, and they remove most drift, since nothing is changed in place. Rollback means redeploying the old image.

Mutable
UpdatingIn place
DriftCommon, and needs correction
RollbackReverse the change
Speed of a small changeFast
Choose when You have long-lived servers that are hard to replace.
Immutable
UpdatingReplace from a new image
DriftRare
RollbackRedeploy the previous image
Speed of a small changeSlower, as an image must be built
Choose when You run stateless workloads on images or containers.
True or false

Mutable and immutable

Exam skills 3.5.2

Check how the two models update, drift and roll back.

Log in to try this activity Interactive activities are free with an account.
Worked example

Stopping drift

Engineers often change settings on production web apps in the portal during incidents, and later Bicep deployments overwrite those changes, causing repeat incidents. What should you recommend?

The fix has two parts. The team should reduce direct changes by giving engineers read access, with just-in-time elevation for emergencies, and require that any emergency change also goes into the Bicep files through a pull request. Scheduled what-if can then report differences. Stopping Bicep deployments would make drift permanent, and copying portal changes into a wiki would not keep the definitions correct.

Check yourself

How does immutable infrastructure reduce drift?

Show answer

Nothing is changed in place. Machines are replaced from a new image, so no individual history accumulates.

Exam Tips

Remember for the exam

  • Keep one artifact, and supply configuration by environment.
  • One definition plus small parameter files beats separate copies.
  • Prevent, detect and correct drift.
  • Immutable infrastructure replaces machines rather than changing them.
  • An emergency manual change must also go into the source definition.