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"
}
}
参考资料