准备工作
内容来自:腾讯云 - 云资源自动化 for Terraform 快速开始
1 创建凭证
在首次使用 Terraform 之前,请前往 云 API 密钥页面 申请安全凭证 SecretId 和 SecretKey。若已有可使用的安全凭证,则跳过该步骤。
- 登录 访问管理控制台,在左侧导航栏,选择访问密钥 > API 密钥管理。
- 在 API 密钥管理页面,单击新建密钥,即可以创建一对 SecretId/SecretKey。
2 配置凭证鉴权
2.1 静态凭证鉴权
在用户目录下创建 provider.tf
文件,输入如下内容:
my-secret-id
及 my-secret-key
请替换为 获取凭证 中的 SecretId 和 SecretKey。
provider "tencentcloud" {
secret_id = "my-secret-id"
secret_key = "my-secret-key"
}
2.2 环境变量鉴权
请将如下信息添加至环境变量配置:
YOUR_SECRET_ID
及 YOUR_SECRET_KEY
请替换为 获取凭证 中的 SecretId 和 SecretKey。
export TENCENTCLOUD_SECRET_ID=YOUR_SECRET_ID
export TENCENTCLOUD_SECRET_KEY=YOUR_SECRET_KEY
3 创建第一个 TencentCloud 资源 VPC
目的:验证是否可以通过 Terraform 创建基础设施。
- 创建
provider.tf
文件,指定 provider 配置信息。文件内容如下:
# 密钥
provider "tencentcloud" {
secret_id = "AK********GA3"
secret_key = "lK********q7"
region = "ap-guangzhou"
}
# 指定云供应商
terraform {
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud" // 第三方云供应商来源
# 通过version指定版本
version = ">=1.60.18" // 版本
}
}
}
云供应商可以通过 Terraform Registry 来查询:
查看使用方法以及最新版本:
- 创建
main.tf
文件,配置腾讯云 Provider 并创建私有网络 VPC。文件内容如下:
resource "tencentcloud_vpc" "foo" {
name = "ci-temp-test-updated" // 私有网络名字
cidr_block = "10.0.0.0/16" // 网段
dns_servers = ["119.29.29.29", "8.8.8.8"] // DNS
is_multicast = false
tags = { // 标签
"test" = "test"
}
}
- 执行以下命令,初始化工作目录并下载插件。
terraform init
- 执行以下命令,查看执行计划,显示将要创建的资源详情。
terraform plan
- 执行以下命令,创建资源。
terraform apply
- 执行以下命令销毁资源。
terraform destroy
开始正式部署第一个基础设施 VPC
1 vpc 模块
- 通过 Terraform Registry 搜索 TencentCloud,查看 Modules。
- 找到相关的模块
- 引用方法
- 使用方法
- 输入输出
2 使用模块
- 创建
modules/vpc/main.tf
调用 vpc 模块
module "tencentcloud_vpc" {
source = "terraform-tencentcloud-modules/vpc/tencentcloud" // 调用 vpc 模块
version = "1.1.0"
vpc_name = var.vpc_name // 变量
vpc_cidr = var.vpc_cidr
destination_cidrs = ["1.0.1.0/24"] // 暂未设置为变量,路由表
next_type = ["EIP"]
next_hub = ["0"]
tags = {
module = "vpc"
}
vpc_tags = {
test = "vpc"
}
}
- 创建
modules/vpc/variables.tf
设置模块变量
variable "vpc_name" {
type = string
description = "tencent cloud vpc name"
}
variable "vpc_cidr" {
type = string
description = "tencent cloud vpc cidr"
}
- 创建
mian.tf
调用 vpc 模块
# 调用 vpc 模块
module "vpc" {
source = "./modules/vpc"
vpc_name = "vpc_test"
vpc_cidr = "10.0.0.0/16"
}
- 创建
provider
配置region,认证,云商
# 密钥
provider "tencentcloud" {
secret_id = "AKI********A3"
secret_key = "lK*********q7"
region = "ap-guangzhou"
}
terraform {
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud"
# 通过version指定版本
version = ">=1.60.18"
}
}
}
- terraform 部署应用
terraform init
terraform plan
terraform apply
- 检查 vpc
- 检查 路由表
结论:
- vpc创建符合预期,期望新增 nat网关 和 子网。
- 路由表里新增 路由策略 nat网关。
3 新增子网
- 修改 vpc 模块 main 文件,新增 子网的内容
module "tencentcloud_vpc" {
source = "terraform-tencentcloud-modules/vpc/tencentcloud"
version = "1.1.0"
# vpc
vpc_name = var.vpc_name
vpc_cidr = var.vpc_cidr
# 子网
subnet_name = var.subnet_name
subnet_cidrs = var.subnet_cidrs
availability_zones = var.availability_zones // 子网可用区
# 路由表
# destination_cidrs = ["1.0.1.0/24"]
# next_type = ["EIP"]
# next_hub = ["0"]
tags = {
module = "vpc"
}
vpc_tags = {
test = "vpc"
}
subnet_tags = {
test = "subnet"
}
}
- 修改 vpc 模块 variable 文件,新增子网的变量内容
# vpc
variable "vpc_name" {
type = string
description = "tencent cloud vpc name"
}
variable "vpc_cidr" {
type = string
description = "tencent cloud vpc cidr"
}
# 子网
variable "subnet_name" {
type = string
description = "tencent cloud subnet name"
}
variable "subnet_cidrs" {
type = list(string)
description = "tencent cloud subnet cidrs"
}
variable "availability_zones" {
type = list(string)
description = "tencent cloud availability zones"
}
- 修改 main 文件,输入子网变量的值
# 调用 vpc 模块
module "vpc" {
source = "./modules/vpc"
vpc_name = "vpc_test"
vpc_cidr = "10.0.0.0/16"
subnet_name = "subnet_test"
subnet_cidrs = [ "10.0.1.0/24" ]
availability_zones = [ "ap-guangzhou-1" ]
}
- 部署应用
terraform plan
terraform apply
- 检查子网
4 新增 Nat网关
先创建 eip,Nat网关绑定 eip。
- 创建
modules/eip/mian.tf
文件,创建 eip
terraform {
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud"
# 通过version指定版本
version = ">=1.60.18"
}
}
}
resource "tencentcloud_eip" "eip" {
name = var.name // eip 名字
type = var.type
internet_max_bandwidth_out = var.internet_max_bandwidth_out
tags = {
module = "eip-test"
}
}
模块里面需要再次声明使用的云商,否则报错。
创建 eip 需要的参数可以查看 腾讯云官方文档 Tencent_eip。
根据参数引用选择需要设置和调整的参数。
- 创建
modules/eip/variables.tf
文件
variable "name" {
type = string
description = "tencent cloud eip name"
}
variable "type" {
type = string
description = "tencent cloud ip type"
}
variable "internet_max_bandwidth_out" {
type = number
description = "tencent cloud internet max bandwidth out"
}
- 创建
modules/eip/outputs.tf
文件,为了将 public_ip 传递给主配置文件
output "eip_public_ip" {
value = tencentcloud_eip.eip.public_ip
description = "tencent cloud eip address"
}
eip就创建好了。
- 修改
modules/vpc/main.tf
vpc模块里面,添加网关的配置
module "tencentcloud_vpc" {
source = "terraform-tencentcloud-modules/vpc/tencentcloud"
version = "1.1.0"
# vpc
vpc_name = var.vpc_name
vpc_cidr = var.vpc_cidr
# 子网
subnet_name = var.subnet_name
subnet_cidrs = var.subnet_cidrs // 网段
availability_zones = var.availability_zones // 可用区
# nat 网关
enable_nat_gateway = var.enable_nat_gateway // 开启网关
nat_gateway_bandwidth = var.nat_gateway_bandwidth // 带宽
nat_gateway_concurrent = var.nat_gateway_concurrent // 并发规格
nat_public_ips = var.nat_public_ips // 绑定eip
# 路由表
# destination_cidrs = ["1.0.1.0/24"]
# next_type = ["EIP"]
# next_hub = ["0"]
tags = {
module = "vpc"
}
vpc_tags = {
test = "vpc"
}
subnet_tags = {
test = "subnet"
}
nat_gateway_tags = {
test = "nat-gateway"
}
}
- 修改
modules/vpc/variables.tf
vpc模块里面,添加网关的变量
# vpc
variable "vpc_name" {
type = string
description = "tencent cloud vpc name"
}
variable "vpc_cidr" {
type = string
description = "tencent cloud vpc cidr"
}
# 子网
variable "subnet_name" {
type = string
description = "tencent cloud subnet name"
}
variable "subnet_cidrs" {
type = list(string)
description = "tencent cloud subnet cidrs"
}
variable "availability_zones" {
type = list(string)
description = "tencent cloud availability zones"
}
# nat 网关
variable "enable_nat_gateway" {
type = bool
description = "tencnet cloud enable nat gateway "
}
variable "nat_gateway_bandwidth" {
type = number
description = "tencent cloud nat gateway bandwidth"
}
variable "nat_gateway_concurrent" {
type = number
description = "tencent cloud nat gateway concurrent"
}
variable "nat_public_ips" {
type = list(string)
description = "tencent cloud nat public ips"
}
- 主
main.tf
文件调用eip,和调用vpc内容新增nat网关
# 调用 eip 模块
module "eip" {
source = "./modules/eip"
type = "EIP"
name = "eip-test"
internet_max_bandwidth_out = 100
}
# 调用 vpc 模块
module "vpc" {
source = "./modules/vpc"
vpc_name = "vpc_test"
vpc_cidr = "10.0.0.0/16"
subnet_name = "subnet_test"
subnet_cidrs = ["10.0.1.0/24"]
availability_zones = ["ap-guangzhou-1"]
enable_nat_gateway = true
nat_gateway_bandwidth = 1000
nat_gateway_concurrent = 1000000
nat_public_ips = ["${module.eip.eip_public_ip}"]
}
⚠️说明:
通过 eip 模块的 outputs 文件将 public_ip 传递给主配置,在主配置里面通过这种方式引用。 nat_public_ips = ["${module.eip.eip_public_ip}"]
。
这种方式的缺点:eip 作为一个 module,别的地方需要绑定 IP 地址,可能会出错。可以后面还需要调整一下调用方式。
这里不能使用 Data Sources,Data Sources 用于获取和引用现有资源的信息,而不是获取将要创建的资源的信息。数据源通常用于查询和引用外部或已存在的资源的属性,而不是当前的Terraform执行中创建的资源。
- 检查vpc
- 检查nat网关
- 检查eip
5 新增路由表
- vpc 模块 main文件新增
# 路由表
destination_cidrs = var.destination_cidrs
next_type = var.next_type
next_hub = var.next_hub
- vpc 模块 variables 文件新增
# 路由表
variable "destination_cidrs" {
type = list(string)
description = "tencent cloud destination cidrs"
}
variable "next_type" {
type = list(string)
description = "tencent cloud next type"
}
variable "next_hub" {
type = list(string)
description = "tencent cloud next hub"
}
- mian文件 新增
destination_cidrs = [ "0.0.0.0/0" ]
next_type = ["NAT"]
next_hub = ["0"]
这里NAT类型并没有在官方文档显示。通过全局搜索 next_type 查询到 Tencent 自己这么使用的。
- apple 查看路由表