ENع
→IaC
الدرس 13 · التسليم

Infrastructure as code

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة11 خطوة5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 13 · تفاعلي
01 / 11

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. The 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 it
  3. Write 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 it
  4. Read 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)
  5. 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.
  6. 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  small
  7. Apply: 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  small
  8. Apply: 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.
  9. 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
      = idempotent
  10. Drift, 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 console
  11. Rebuild 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

اختبار يتاح بعد الخطوة الأخيرة.