目录
2.Terraform 使用 provider 自定义提供者 (Resource)
3.Terraform 使用 provider 自定义提供者 (Module)
(1)主机
表1-1 主机
主机 | 系统 | 软件 | 工具 | 备注 |
jia | Windows? | Terraform 1.6.6 | VS Code、?PowerShell、?Chocolatey |
(1)查看项目
(2)主配置文件
main.tf ,资源里直接定义名称??provider ? = alicloud.NJ
# Configure the Alicloud Provider 默认供应商
provider "alicloud" {
access_key = var.access_key
secret_key = var.secret_key
region = "cn-hangzhou"
}
provider "alicloud" {
alias = "NJ"
access_key = var.access_key
secret_key = var.secret_key
region = "cn-nanjing"
}
//VPC 专有网络
resource "alicloud_vpc" "vpc" {
vpc_name = "myvpc1"
cidr_block = "172.79.0.0/16"
}
resource "alicloud_vpc" "vpc2" {
provider = alicloud.NJ
vpc_name = "myvpc2"
cidr_block = "172.79.0.0/16"
}
(3) 版本配置文件
versions.tf
terraform {
required_version = "1.6.6"
required_providers {
alicloud = {
source = "hashicorp/alicloud"
version = "1.214.1"
}
}
}
(4)变量配置文件
variables.tf
variable "access_key" {
description = "access_key"
}
variable "secret_key" {
description = "secret_key"
}
(5) 密钥配置文件
terraform.tfvars
(6)初始化
terraform init
(7)格式化代码
terraform fmt
(8)验证代码
terraform validate
(9)计划与预览
terraform plan
(10)申请资源
terraform apply
(11)展示资源
terraform show
(12)登录阿里云系统查看VPC
VPC已新增1个 (cn-hangzhou)
VPC已新增1个 (cn-nanjing)
(18)销毁资源
terraform destroy
(1)查看项目
(2) moudule模块配置文件?
主配置文件main.tf
//VPC 专有网络
resource "alicloud_vpc" "vpc" {
vpc_name = var.vpc_name
cidr_block = var.vpc_cidr_block
}
版本配置文件versions.tf
terraform {
required_version = "1.6.6"
required_providers {
alicloud = {
source = "hashicorp/alicloud"
version = "1.214.1"
}
}
}
变量配置文件variables.tf
variable "vpc_name" {
}
variable "vpc_cidr_block" {
}
(3)主配置文件
main.tf ,模块引用为map类型的键值对格式? providers = {?alicloud = alicloud.NJ ??}
# Configure the Alicloud Provider 默认供应商
provider "alicloud" {
access_key = var.access_key
secret_key = var.secret_key
region = "cn-hangzhou"
}
provider "alicloud" {
alias = "NJ"
access_key = var.access_key
secret_key = var.secret_key
region = "cn-nanjing"
}
module "myvpc" {
source = "./vpc"
vpc_name = "myvpc1"
vpc_cidr_block = "172.78.0.0/16"
providers = {
alicloud = alicloud.NJ
}
}
(4) 版本配置文件
versions.tf
terraform {
required_version = "1.6.6"
required_providers {
alicloud = {
source = "hashicorp/alicloud"
version = "1.214.1"
}
}
}
(5)变量配置文件
variables.tf
variable "access_key" {
description = "access_key"
}
variable "secret_key" {
description = "secret_key"
}
(6) 密钥配置文件
terraform.tfvars
(7)初始化
terraform init
(8)格式化代码
terraform fmt
(9)验证代码
terraform validate
(10)计划与预览
terraform plan
(11)申请资源
terraform apply
(12)展示资源
terraform show
(13)登录阿里云系统查看VPC
VPC已新增1个 (cn-nanjing)
(14) 销毁资源
terraform destroy