Infrastructure as code
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Infrastructure as code means writing down the servers you want in a file, keeping that file in version control, and letting a tool make the real cloud match it. Follow one small example: servers the tool already manages, an edited file that now wants something different, and a tool that checks what it manages against the cloud, prints a plan of create, update and destroy actions, and applies it. Then run it again, catch a change made by hand, and rebuild everything from the same file.
Servers described in a file
Infrastructure as code (IaC) means describing servers, networks and other cloud parts in text files. A tool reads the file and makes the real cloud match it. The file lives in version control, like any other code.
file (what you want) → tool → cloud (what exists)
kept in version control (Git)
reviewed, repeatable, rebuildable
tools: Terraform, OpenTofu, Pulumi,
CloudFormation, kubectl applyاقرأ الدرس خطوة بخطوة
Servers described in a file
Infrastructure as code (IaC) means describing servers, networks and other cloud parts in text files. A tool reads the file and makes the real cloud match it. The file lives in version control, like any other code.
file (what you want) → tool → cloud (what exists) kept in version control (Git) reviewed, repeatable, rebuildable tools: Terraform, OpenTofu, Pulumi, CloudFormation, kubectl applyThe old way: by hand
Without IaC, people build servers by clicking in the cloud's web console. Nothing records exactly what they did, so servers slowly drift apart. Hand-made servers that are each a little different are called snowflakes.
clicked in the console: no record of who did what, or why each server a little different = "snowflake" servers an IaC tool cannot see these until they are imported into itWrite what you want
An earlier version of the file built web-1 (tiny) and old-1, so the tool already manages both. Now you edit the desired state: web-1 small, old-1 removed, web-2 added. This is declarative: you say what should exist, not how.
version 1 (applied earlier): want = {"web-1": "tiny", "old-1": "small"} version 2 (your edit, a Python dict): want = { "web-1": "small", "web-2": "small", } what should exist, not how to make itRead what exists
The tool checks the servers it manages, listed in its state, against the cloud provider: web-1 is tiny, old-1 is small, web-2 does not exist yet. This is the actual state. Servers made outside the tool stay invisible to it.
state (what the tool manages): web-1, old-1 actual state (checked with the cloud): web-1 tiny web-2 — old-1 small not in state = not seen (import it first to manage it)Plan: compare the two
The tool compares wanted with actual and prints a plan: update web-1 (~), create web-2 (+), destroy old-1 (-). Nothing has changed yet. A person reads the plan before anything happens.
>>> plan(want, have) [('~', 'web-1', 'small'), ('+', 'web-2', 'small'), ('-', 'old-1', 'small')] ~ web-1 tiny → small update + web-2 small create - old-1 small destroy Plan: 1 to add, 1 to change, 1 to destroy.Apply: update web-1
Now apply carries out the plan, one action at a time. First it resizes web-1 from tiny to small. The server keeps its name; only its size changes, so this is an update in place.
~ web-1 tiny → small web-1 small web-2 — old-1 smallApply: create web-2
web-2 is in the file but not in the cloud, so apply creates it, small, as written. Nobody had to remember the size or click through a form: the file is the record.
+ web-2 small web-1 small web-2 small old-1 smallApply: destroy old-1
old-1 is in the tool's state but no longer in the file, so apply destroys it. The cloud now matches the file exactly. Be careful: deleting a line from the file deletes the real server too.
- old-1 small web-1 small web-2 small old-1 — Applied: 1 to add, 1 to change, 1 to destroy.Run it again: nothing to do
Run plan again and the tool finds no differences: "No changes. Your infrastructure matches the configuration." Running it twice gives the same result as running it once. That property is called idempotence.
>>> plan(want, have) [] No changes. Your infrastructure matches the configuration. run once or ten times: same cloud = idempotentDrift, and putting it back
Someone resizes web-1 to large by hand in the console. The cloud no longer matches the file: that is drift. The next plan shows ~ web-1 large → small, and apply puts it back.
by hand: web-1 small → large >>> plan(want, have) [('~', 'web-1', 'small')] ~ web-1 large → small apply → web-1 small again fix it in the file, not the consoleRebuild from the file
Because the file holds the whole design, the same tool can build an identical copy from nothing: a test copy, or a fresh start after a disaster. Changes go through review in a pull request, like any code.
empty cloud + the same file >>> plan(want, {}) [('+', 'web-1', 'small'), ('+', 'web-2', 'small')] web-1 small web-2 small old-1 — same file → same servers, every time
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01An earlier version of the file built web-1 (tiny) and old-1 (small), so the tool manages both. You edit the file to want web-1 and web-2, both small. What does the plan do?
The tool compares wanted with the servers in its state: web-1 exists with the wrong size (update ~), web-2 is missing (create +), and old-1 is managed but no longer in the file (destroy -). A server made by hand would be left alone: the tool cannot see it until it is imported.
02What does it mean that an IaC tool is declarative?
You say what should exist (two small servers). The tool compares that with what does exist and decides whether to create, update or destroy.
03You run plan straight after a successful apply, without changing anything. What should it show?
Running it again gives the same result as running it once. This is idempotence; Terraform prints "No changes. Your infrastructure matches the configuration."
04Someone resizes web-1 to large by hand in the web console. What is this called, and what happens on the next plan?
A change made outside the tool makes the real cloud drift from the file. The next plan spots the difference and apply restores what the file says. If the change was wanted, edit the file instead.
05The tool built old-1 from the file. You delete the old-1 line and apply. What happens to the real old-1 server?
The file is the source of truth. Anything in the tool's state that is no longer in the file gets destroyed, so read the plan before you apply. Servers the tool never managed are not touched.
المسرد
- Infrastructure as code (IaC)
- Describing servers, networks and other infrastructure in text files kept in version control, so a tool can create and change them the same way every time.
- Desired state
- What should exist, as written in the file: for example two small servers called web-1 and web-2.
- Actual state
- What really exists right now. Before it plans, the tool checks the resources in its state (its record of what it manages) with the cloud provider. Resources made outside the tool are not seen until they are imported.
- Declarative
- Describing the end result you want rather than the commands to get there. The tool works out the steps.
- Plan
- The list of actions the tool would take to make actual match desired: create (+), update in place (~) and destroy (-). Nothing changes until you apply.
- Apply
- Carrying out a plan: the tool calls the cloud provider's API to create, update or destroy the resources.
- Idempotent
- Giving the same result whether you run it once or many times. A second apply with nothing changed does nothing.
- Drift
- A difference between the real infrastructure and the file, usually from a change made by hand outside the tool.
الملاحظات والمصادر
- The Python program is a toy that shows the idea; its have dict stands for the servers the tool manages, as they really are. Real tools (Terraform, OpenTofu, Pulumi, AWS CloudFormation) call the cloud provider's API to read and change real resources, handle dependencies between them, and save a state file.
- In Terraform the same wish is written in HCL, for example resource "aws_instance" "web_1" { instance_type = "t3.small" ... }. terraform plan prints the actions with + (create), ~ (update in place) and - (destroy), then a summary such as "Plan: 1 to add, 1 to change, 1 to destroy."
- Terraform remembers what it manages in a state file. Before planning it refreshes that record from the provider, which is how it notices drift: changes made outside Terraform to resources it manages.
- An IaC tool only plans for resources recorded in its state (Terraform, OpenTofu) or its stack (CloudFormation); it does not list everything in your account. A server made by hand is invisible to it until you import it (in Terraform, with an import block), and Terraform never destroys what it does not track. kubectl apply deletes objects missing from the file only when you pass --prune.
- Some changes cannot be made in place; the tool then destroys the server and creates a new one (Terraform marks this -/+). Resizing a real virtual machine may also need a restart.
- Declarative tools (Terraform, CloudFormation, kubectl apply) take the end state you want. Imperative scripts list commands to run; running them twice may create two of everything.
- HashiCorp: What is Terraform?
- HashiCorp: terraform plan command
- HashiCorp: terraform apply command
- HashiCorp: State
- HashiCorp: Import existing resources
- HashiCorp tutorial: Manage resource drift
- HashiCorp tutorial: Create a Terraform plan
- Kubernetes: Declarative management of objects using configuration files
- AWS whitepaper: Introduction to DevOps on AWS, Infrastructure as code
- Google Cloud: Infrastructure as code overview
- Google SRE book, ch. 8: Release Engineering
- Martin Fowler: Infrastructure as Code
- OpenTofu: What is OpenTofu?
اختبار يتاح بعد الخطوة الأخيرة.