Find a course

Module 5: Operating and Moving to the CloudLesson 11 of 1212 min

Infrastructure as Code, CI/CD and Monitoring

Define cloud resources in code with tools like Terraform, ship changes safely through CI/CD pipelines, and observe systems with metrics, logs and traces.

Step 1 of 13Learn

Click-ops vs. code

Two engineers each build a staging environment: a network, three servers, a database and a load balancer. Engineer A clicks through the cloud console for an afternoon. Engineer B writes a 200-line configuration file and runs one command.

Six months later, the company needs an identical environment in another region for a new customer. Engineer A tries to remember every setting, misses a firewall rule and spends two days debugging. Engineer B changes one variable, the region, and runs the command again. Twenty minutes later it's done, identical down to the last setting.

That's infrastructure as code (IaC): describing cloud resources in text files that tools read to create, update and delete the real infrastructure. Popular tools include Terraform and its open-source fork OpenTofu (which work across many providers), each provider's own tools such as AWS CloudFormation, Azure Bicep and Google Cloud Deployment Manager, and Pulumi, which uses general-purpose programming languages.

Most IaC tools are declarative: you describe the end state you want ("three servers of this size in these subnets"), and the tool works out what to create, change or remove to get there. Benefits:

  • Repeatability: the same code builds identical environments every time.
  • Version control: infrastructure changes go through Git, with history, review and the ability to roll back.
  • Documentation: the code itself shows exactly what exists and why.
  • Speed and recovery: whole environments can be rebuilt after a disaster.

Step 2 of 13Practice · Select all that apply

Which are real benefits of infrastructure as code? Select all that apply.

Select all that apply.

Step 3 of 13Compare

Click-ops vs infrastructure as code

AspectClick-opsInfrastructure as code
How it is builtClicking through the consoleConfig files run by a tool
Repeat itAnother afternoon of clickingRun it again in minutes
Review changesHard: nobody sees the clicksCode review, history, rollback
ConsistencySmall differences creep inIdentical every time

Step 4 of 13Learn

Drift and the IaC workflow

IaC only works if the code stays the source of truth. A typical Terraform-style workflow:

  1. Write or change the configuration in a branch.
  2. Plan: the tool compares the code with what actually exists and shows exactly what it will add, change or destroy. Reviewers read this plan carefully; a line saying a production database will be "destroyed and recreated" is a stop sign.
  3. Review and merge through a pull request, like application code.
  4. Apply: the tool makes the changes, ideally run by an automated pipeline rather than from someone's laptop.

The tool keeps a state record of the resources it manages. That state must be stored safely and shared, usually in remote storage with locking so two people can't apply changes at once.

Drift happens when someone changes resources by hand in the console, outside the code. The next plan shows unexpected differences, or worse, the next apply silently reverts an emergency fix. Teams avoid drift by restricting console write access in production, detecting drift with scheduled plans, and if an emergency change is made by hand, putting it into the code immediately afterward.

A related idea is immutable infrastructure: instead of patching running servers, build a new image with the change, deploy new servers from it and remove the old ones. Every server then matches a known, tested image.

Step 5 of 13Practice · Put in order

Put the steps of a safe infrastructure-as-code change in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Open a pull request so a teammate reviews the code and the plan
  2. Edit the configuration files in a new branch
  3. Run a plan to preview what will be created, changed or destroyed
  4. Let the pipeline apply the change and record the new state
  5. Merge the change after approval

Step 6 of 13Practice · Diagram

The IaC change loop

Tap the step that shows what will be destroyed before anything happens.

Tap the shape that answers the question, then press Check.

The IaC change loopCycle diagram: Write the change → Plan → Review → Apply → Detect drift.Code = source oftruthWrite thechangePlanReviewApplyDetect drift
Write the change

In a branch.

Plan

Preview exactly what will be created, changed or destroyed.

Review

A teammate checks the code and the plan.

Apply

The tool makes the real changes.

Detect drift

Find manual changes and bring them back into code.

Step 7 of 13Learn

CI/CD: shipping changes safely

Continuous integration (CI) means developers merge small changes often, and every change automatically triggers a build and tests. Problems surface within minutes of being introduced, while they're small and easy to fix.

Continuous delivery and deployment (CD) extend the pipeline to releasing: every change that passes the tests is packaged, deployed to a staging environment, checked, and then either released to production automatically (continuous deployment) or with one approval click (continuous delivery). Common tools include GitHub Actions, GitLab CI, Jenkins, Azure Pipelines and the providers' own build services.

A pipeline for a containerized web app might: run unit tests, scan the code and dependencies for known vulnerabilities, build a container image, push it to a registry, apply IaC changes, deploy to staging, run smoke tests, then deploy to production.

Release strategies reduce the risk of each deployment:

  • Rolling deployment: replace instances a few at a time.
  • Blue-green deployment: run the new version (green) alongside the old (blue), then switch traffic at once, with instant rollback by switching back.
  • Canary release: send a small share of traffic, say 5%, to the new version, watch error rates and latency, and increase gradually if all is well.

The DevOps culture behind all this, developers and operations sharing responsibility for building and running software, is what lets organizations deploy many times a day with fewer failures than when they deployed a few times a year.

Step 8 of 13Diagram

A CI/CD pipeline

A CI/CD pipelineProcess diagram: Commit → Build → Automated tests → Deploy to staging → Canary in production → Full rollout.CommitBuildAutomated testsDeploy to stagingCanary in productionFull rollout

Tap a shape with to see more.

Commit
Build
Automated tests

A failure stops the pipeline.

Deploy to staging
Canary in production

A small share of users first; watch the metrics.

Full rollout

Step 9 of 13Practice · Match the pairs

Match each release strategy to its description.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Rolling deployment
  2. Blue-green deployment
  3. Canary release
  4. Rollback

Step 10 of 13Learn

Observability: metrics, logs and traces

Once systems run in production, you need to know how they're doing, ideally before users tell you. Observability rests on three kinds of data:

  • Metrics: numbers over time, such as CPU use, request rate, error rate, latency and queue length. Cheap to store and ideal for dashboards and alerts.
  • Logs: timestamped records of events, such as "user 4182 failed login" or a stack trace. Essential for figuring out why something happened. Centralize them so you can search across all servers at once.
  • Traces: the path of one request through many services, showing how long each step took. In a microservices app, traces reveal which of 15 services made a page slow. OpenTelemetry is the common open standard for collecting all three.

Every provider has built-in tools, such as Amazon CloudWatch, Azure Monitor and Google Cloud Monitoring and Logging, and many teams use third-party platforms as well.

Good alerts are about symptoms users feel, such as error rate above 2% or checkout latency above one second for five minutes, not every CPU spike. Too many noisy alerts train people to ignore them. Many teams define service level objectives (SLOs), internal targets such as "99.9% of requests succeed over 30 days", and alert when they're burning through their error budget too fast.

A popular starting set of metrics for any service is the four golden signals: latency, traffic, errors and saturation.

Step 11 of 13Practice · Sort into groups

Sort each piece of data into metrics, logs or traces.

Tap an item, then tap its group (or drag it there).

Step 12 of 13Practice · True or false

A declarative infrastructure-as-code tool needs you to write every step in order, such as "first create the network, then the subnet, then the server".

Step 13 of 13Practice · What would you do?

What would you do?

A team gets 300 alerts a day, mostly brief CPU spikes, and has started ignoring them. Last week a real outage went unnoticed for 40 minutes. What should they change?