Terraform 快速參考

常用 Terraform 指令和操作方法的快速參考。

常用的 Terraform 指令

terraform init

初始化包含 Terraform 設定檔的工作目錄。此命令下載並安裝必要的提供者外掛程式,並設定用於儲存狀態檔案的後端。

terraform plan

產生執行計劃,顯示 Terraform 將採取哪些操作來實現設定檔中定義的基礎架構的所需狀態。這是一次試運行,不會改變任何實際資源。

terraform apply

套用達到所需配置狀態所需的變更。該命令執行 terraform plan 產生的計劃並對基礎設施進行必要的更改。

terraform destroy

破壞 Terraform 管理的基礎設施。此命令用於刪除設定檔中定義的所有資源。

terraform show

顯示目前狀態或已儲存的計劃。此命令對於檢查基礎設施的當前狀態或先前產生的計劃很有用。

terraform output

顯示配置中定義的輸出值。這對於檢索已部署基礎架構的資訊很有用。

如何定義基本 Terraform 配置

基本的 Terraform 設定檔通常由幾個關鍵元件組成:提供程式配置、資源定義以及可選的變數和輸出。下面是一個提供 AWS EC2 執行個體的 Terraform 設定檔的簡單範例。

# Define the provider
provider "aws" {
  region = "ap-northest-1a"
}

# Define a variable for the AMI ID
variable "ami_id" {
  description = "The AMI ID to use for the instance"
  default     = "ami-xxx"
}

# Define a variable for the instance type
variable "instance_type" {
  description = "The type of instance to use"
  default     = "t2.micro"
}

# Define the EC2 instance resource
resource "aws_instance" "app_server" {
  ami           = var.ami_id
  instance_type = var.instance_type

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

# Output the public IP of the instance
output "instance_ip" {
  description = "The public IP address of the instance"
  value       = aws_instance.app_server.public_ip
}

如何使用模組定義 Terraform 配置

  • structure
.
├── main.tf
└── modules
    └── ec2_instance
        ├── main.tf
        ├── variables.tf
        └── outputs.tf
  • main.tf
# Define the provider
provider "aws" {
  region = "ap-northeast-1a"
}

# Define a variable for the AMI ID
variable "ami_id" {
  description = "The AMI ID to use for the instance"
  default     = "ami-xxx"
}

# Define a variable for the instance type
variable "instance_type" {
  description = "The type of instance to use"
  default     = "t2.micro"
}

# Call the local module
# The label immediately after the module keyword is a local name, 
# which the calling module can use to refer to this instance of the module.
module "app_server" {
  source        = "./modules/ec2_instance"
  instance_type = var.instance_type
  ami_id        = var.ami_id
}

# Output the public IP of the instance
output "instance_ip" {
  description = "The public IP address of the instance"
  value       = module.app_server.instance_ip
}
  • modules/ec2_instance/main.tf
# Define the EC2 instance resource
resource "aws_instance" "this" {
  ami           = var.ami_id
  instance_type = var.instance_type

  tags = {
    Name = "ExampleAppServerInstance"
  }
}
  • modules/ec2_instance/variables.tf
# Define a variable for the AMI ID
variable "ami_id" {
  description = "The AMI ID to use for the instance"
  type        = string
}

# Define a variable for the instance type
variable "instance_type" {
  description = "The type of instance to use"
  type        = string
}
  • modules/ec2_instance/outputs.tf
# Output the public IP of the instance
output "instance_ip" {
  description = "The public IP address of the instance"
  value       = aws_instance.this.public_ip
}

Terraform定義塊的用途是什麼

Terraform 設定檔中的 terraform 區塊用於定義配置 Terraform 本身行為的設定。該區塊可以包括各種設置,例如指定所需的 Terraform 版本、配置狀態儲存的後端、定義所需的提供者以及啟用實驗性功能。

以下是結合了這些設定的一個例子:

terraform {
  required_version = ">= 1.1.0"

  backend "s3" {
    bucket = "my-terraform-state"
    key    = "terraform.tfstate"
    region = "ap-northest-1a"
  }

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 3.71.0"
    }
  }

  experiments = ["example"]

  provider_meta "aws" {
    example_key = "example_value"
  }
}

參考文獻

terraform