Leksjon 6 av 7 · 8 min
Infrastructure as code and policy as code
When servers, networks and permissions are defined in files, they can be reviewed and tested like software. Learn how Terraform and CloudFormation work, and how policy as code catches misconfigurations before deployment.
Denne siden er foreløpig på engelsk.
Not long ago, setting up infrastructure meant clicking through consoles or running commands by hand. Nobody could say for certain how a server had been configured, and two “identical” environments rarely were. Infrastructure as code (IaC) replaced this with files that describe the desired infrastructure: networks, virtual machines, storage buckets, firewall rules and permissions. A tool reads the files and makes reality match them.
Two common IaC tools
| Tool | Made by | How it works |
|---|---|---|
| Terraform | HashiCorp | Declarative configuration files; works with many cloud and service providers through plug-in “providers”; keeps a state file recording what it manages |
| AWS CloudFormation | Amazon Web Services | Templates in JSON or YAML describing AWS resources, deployed together as a “stack” |
Both are declarative: you describe what should exist, and the tool works out the steps. Both also let you preview changes before applying them, which is where security reviews fit naturally.
Why IaC is a security opportunity
- Every change is visible in version control, with an author, a review and a history.
- The same reviewed template produces the same environment every time, removing hand-made differences.
- Mistakes can be caught automatically before anything is deployed, which is the heart of shifting left.
- Recovery is faster: a damaged environment can be rebuilt from known-good code.
It is also a risk. A single insecure template, such as a storage bucket open to the public or a firewall rule allowing the whole internet in, is copied into every environment built from it. And IaC files, state files and pipeline variables can contain secrets or reveal the layout of your infrastructure, so they need the same protection as source code.
Policy as code
Policy as code means writing security rules in a machine-readable form so a tool can check them automatically. Instead of a document saying “storage must never be public”, you have a rule that fails the pipeline when a template makes storage public. Open Policy Agent (OPA), a graduated project of the Cloud Native Computing Foundation, is a general-purpose policy engine with its own policy language, Rego, used for IaC checks, Kubernetes admission control and more. Scanners such as Checkov come with many ready-made checks for Terraform, CloudFormation and Kubernetes files.
| Example policy | Misconfiguration it prevents |
|---|---|
| Storage buckets must block public access | Accidental exposure of private files |
| No security group may allow administrative ports from anywhere | Remote administration exposed to the internet |
| Encryption at rest must be enabled for databases and volumes | Readable data if storage is copied |
| IAM policies may not use wildcard actions on all resources | Over-permissioned identities |
| Every resource must carry an owner tag | Orphaned resources nobody maintains |
Where to run the checks
- In the editor, so developers see problems while writing.
- In the pull request, blocking merge on high-severity rule violations.
- On the planned change, just before it is applied.
- At deploy time in the platform, for example Kubernetes admission control, as a last gate.
- Continuously against the live environment, because not every change goes through code. That is the subject of the next lesson.
Where FireAI fits
FireAI works on a single Mac rather than in cloud infrastructure, but it shares one habit with IaC: configuration you can keep as a file. Its rules can be exported to back them up or move them to another Mac, and imported back in safely.
Det viktigste
- IaC describes infrastructure in reviewable, repeatable files; Terraform and CloudFormation are common tools.
- One insecure template spreads its mistake everywhere, so templates need review and testing.
- Policy as code turns security rules into automated checks, with tools such as OPA and Checkov.
- Check early in the editor and pull request, and again at deploy time and in the live environment.
Test deg selv
1. What does “declarative” mean for IaC tools like Terraform?
- You list every command to run in order
- You describe the desired end state and the tool works out the steps — Riktig.
- The tool guesses what you want
- It only works on one cloud
Declarative tools compare the desired state to reality and plan the changes themselves.
2. What is policy as code?
- Writing laws in a programming language
- Expressing security rules in machine-readable form so tools can check them automatically — Riktig.
- Encrypting configuration files
- Signing Terraform files
Policy as code turns written rules into automated checks that can block unsafe changes.
3. Why can a single insecure IaC template be especially harmful?
- It makes the pipeline slower
- Its mistake is copied into every environment built from it — Riktig.
- It deletes the state file
- It cannot be reviewed
Repeatability cuts both ways: a flaw is reproduced just as reliably as a good configuration.
Prøv det med FireAI
Sett denne leksjonen ut i praksis på din egen Mac.
- Regler: app, nettsted, domene, IP eller et område, for alltid eller til du starter på nytt — Skriv en regel like presis som én adresse eller like bred som et helt domene.
- Importer og eksporter reglene dine — Flytt reglene dine til en ny Mac, eller sikkerhetskopier dem, med et par klikk.
- Hvordan FireAI overvåker Mac-ens tilkoblinger — Vit hvilken app som snakker med internett, i vanlige ord, uten å installere noe som kjører som en skjult bakgrunnstjeneste.
- Trussellister (valgfritt) — Sjekk trafikken din mot offentlige trusseldata uten å sende den noe sted.
- Verdenskartet — Se hvor dataene dine faktisk går, ikke bare et vertsnavn du måtte slå opp selv.
Kilder
- HashiCorp: What is Terraform?
- AWS: What is CloudFormation?
- Open Policy Agent
- Checkov
- NIST SP 800-53 Rev. 5: Security and Privacy Controls
- NIST SP 800-204D: Software supply chain security in DevSecOps CI/CD pipelines
Ta det i bruk på din Mac
Prøv alle funksjonene gratis i 17 dager, uten kort.