Infrastructure as Code 入門

DevOps 2025-01-02T07:00:00.000Z

Infrastructure as Code 入門

Infrastructure as Code(IaC)讓基礎設施管理變得可重複、可版本控制。

核心概念

  • 宣告式:描述期望狀態,工具負責達成
  • 冪等性:多次執行結果一致
  • 版本控制:基礎設施變更可追溯

Terraform 範例

provider "aws" {
  region = "ap-northeast-1"
}

resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"

  tags = {
    Name = "web-server"
  }
}

Ansible 範例

- name: Setup web server
  hosts: webservers
  become: yes
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present

    - name: Start nginx
      service:
        name: nginx
        state: started
        enabled: yes

工具比較

工具 類型 適用場景
Terraform 宣告式 雲端基礎設施
Ansible 程序式 伺服器設定
Pulumi 程式碼化 開發者導向