文档:https://www.terraform.io/docs
支持 AWS、Azure、GCP、阿里云、华为云、腾讯云等多种云和 Saas 平台,实现从新建、修改到销毁资源整个流程的管理,底层自动实现了版本化,具体支持的供应商列表:https://registry.terraform.io/browse/providers
下载地址,页面已包含各种系统安装说明:https://www.terraform.io/downloads
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
terraform -install-autocomplete
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform
terraform -install-autocomplete
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repoRHEL/hashicorp.repo
sudo yum -y install terraform
terraform -install-autocomplete
编写资源定义配置文件。
执行计划,预览资源变更,可跳过此步骤,直接下一步。
应用变更,执行第2步计划的操作。
详细说明:https://www.terraform.io/cli
注意事项:命令后[]及包含的内容代表可选项
格式化配置文件,空路径即为默认当前目录,具体说明:https://www.terraform.io/cli/commands/fmt
terraform fmt [参数选项] [文件或目录路径]
检验当前目录下配置文件是否有效,具体说明:https://www.terraform.io/cli/commands/validate
terraform validate [参数选项]
初始化工作目录,具体说明:https://www.terraform.io/cli/commands/init
terraform init
执行计划,预览资源变更,亦工作流第二步,具体说明:https://www.terraform.io/cli/commands/plan
terraform plan
应用变更,执行工作流第2步计划的操作,对应工作流第3步,具体说明:https://www.terraform.io/cli/commands/apply
terraform apply
销毁资源,具体说明:https://www.terraform.io/cli/commands/destroy
terraform destroy
等同于 terraform apply -destroy
检查状态,具体说明:https://www.terraform.io/cli/commands/show
terraform show [参数选项] [文件路径]
列出资源,具体说明:https://www.terraform.io/cli/commands/state/list
terraform state list [参数选项] [资源寻址]
资源寻址格式说明:https://www.terraform.io/cli/state/resource-addressing
显示指定寻址资源的属性,具体说明:https://www.terraform.io/cli/commands/state/show
terraform state show [参数选项] 资源寻址
详细文档:https://registry.terraform.io/providers/hashicorp/aws/latest/docs
示例说明:https://learn.hashicorp.com/collections/terraform/aws-get-started
详细文档:https://registry.terraform.io/providers/hashicorp/google/latest/docs
示例说明:https://learn.hashicorp.com/collections/terraform/gcp-get-started
详细文档:https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs
示例说明:https://support.huaweicloud.com/productdesc-terraform/index.html
本次使用 AWS SGT 子账号下 EC2 作为示例说明。
先决条件:
设置环境变量关联 AWS 程序密钥或者运行命令 aws configure
直接永久保存密钥配置:
export AWS_ACCESS_KEY_ID=申请的AK
export AWS_SECRET_ACCESS_KEY=申请的SK
1.1 新建目录和主配置文件:
mkdir -p code/terraform/aws
cd code/terraform/aws
touch main.tf
1.2 文件 main.tf
添加内容:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.16"
}
}
required_version = ">= 1.2.0"
}
provider "aws" {
region = "ap-southeast-1"
}
resource "aws_instance" "app_server" {
ami = "ami-0625a212c50aa6656"
instance_type = "t3.micro"
vpc_security_group_ids = ["sg-075c60ee59cac166f"]
subnet_id = "subnet-08cb572fc9fb0e6ca"
tags = {
Name = "SGT-dongsong-test"
}
}
1.3 初始化目录:
terraform init
1.4 格式化配置:
terraform fmt
1.5 检查语法:
terraform validate
1.6 应用配置,新建资源,输出确认提示后输入 yes
:
terraform apply
1.7 检查状态:
terraform show
1.8 列出资源:
terraform state list
1.9 显示资源属性:
terraform state show aws_instance.app_server
2.1 修改 main.tf
实例镜像为 ami-0fa301794cb89d351
:
ami = "ami-0fa301794cb89d351"
2.2 应用变更,输出确认提示后输入 yes
:
terraform apply
2.3 后续检查资源状态和属性和新建资源无区别,不再重述。
3.1 销毁资源,输出确认提示后输入 yes
:
terraform destroy
详细说明:https://learn.hashicorp.com/tutorials/terraform/variables?in=terraform/configuration-language
4.1 修改 main.tf
文件 tags
块 Name
字段内容为:
Name = var.instance_name
4.2 Terraform 会加载所有当前目录下以 .tf
为后缀的文件,新建 variables.tf
文件指定主机名:
variable "instance_name" {
description = "Value of the Name tag for the EC2 instance"
type = string
default = "SGT-dongsong-tmp"
}
4.3 变更资源
terraform apply
详细说明:https://learn.hashicorp.com/tutorials/terraform/outputs?in=terraform/configuration-language
5.1 新建文件 outputs.tf
,输出显示 EC2 实例 ID 和 IP 地址:
output "instance_id" {
description = "ID of the EC2 instance"
value = aws_instance.app_server.id
}
output "instance_public_ip" {
description = "Public IP address of the EC2 instance"
value = aws_instance.app_server.public_ip
}
5.2 变更资源
terraform apply
5.3 输出自定义内容
terraform output
执行命令前加入 TF_LOG=TRACE