腾讯云terraform学习教程

以下是将 Terraform 示例从 AWS 改为 腾讯云(Tencent Cloud) 的完整配置指南,适用于零基础学习者。


一、准备工作

1. 安装腾讯云 Terraform Provider

确保已安装 Terraform 并下载腾讯云 Provider 插件:

bash 复制代码
# 下载腾讯云 Provider v1.28.1(以版本 1.28.1 为例)
mkdir -p ~/.terraform.d/plugins/registry.cn-qingdao.aliyuncs.com/tencentcloud/tencentcloud/1.28.1
wget -O ~/.terraform.d/plugins/registry.cn-qingdao.aliyuncs.com/tencentcloud/tencentcloud/1.28.1/terraform-provider-tencentcloud \
     https://github.com/tencentcloud/tencentcloud-terraform-provider/releases/download/v1.28.1/terraform-provider-tencentcloud
2. 配置腾讯云凭证
  • 登录 腾讯云控制台 获取 SecretId 和 SecretKey。

  • 设置环境变量:

    bash 复制代码
    export TENCENTCLOUD_SECRET_ID="YOUR_SECRET_ID"
    export TENCENTCLOUD_SECRET_KEY="YOUR_SECRET_KEY"

二、Terraform 配置文件

1. 项目结构
复制代码
my-tencent-cloud-project/
├── main.tf
├── variables.tf
└── outputs.tf
2. main.tf
hcl 复制代码
provider "tencentcloud" {
  region = "ap-beijing"  # 北京地区
}

resource "tencentcloud_instance" "example" {
  instance_name   = "MyFirstCVM"
  project_id      = 0
  availability_zone = "ap-beijing-1"
  image_id        = "img-8xr35l5w"  # CentOS 7.6 公共镜像 ID
  instance_type   = "S1.SMALL1"     # 小型实例
  system_disk_type = "CLOUD_PREMIUM"
  system_disk_size = 50
  vpc_id          = tencentcloud_vpc.example.id
  subnet_id       = tencentcloud_subnet.example.id
  security_group_ids = [tencentcloud_security_group.example.id]
  key_pair = "my-key-pair"  # 确保已在腾讯云控制台创建密钥对
  tags = {
    Environment = "dev"
  }
}

resource "tencentcloud_vpc" "example" {
  name       = "MyVPC"
  cidr_block = "10.0.0.0/16"
}

resource "tencentcloud_subnet" "example" {
  name              = "MySubnet"
  vpc_id            = tencentcloud_vpc.example.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "ap-beijing-1"
}

resource "tencentcloud_security_group" "example" {
  name        = "MySG"
  description = "Allow SSH and HTTP"
}

resource "tencentcloud_security_group_rule" "allow_ssh" {
  security_group_id = tencentcloud_security_group.example.id
  direction         = "INGRESS"
  action            = "ACCEPT"
  protocol          = "tcp"
  port_range        = "22"
  cidr_block        = "0.0.0.0/0"
}

resource "tencentcloud_security_group_rule" "allow_http" {
  security_group_id = tencentcloud_security_group.example.id
  direction         = "INGRESS"
  action            = "ACCEPT"
  protocol          = "tcp"
  port_range        = "80"
  cidr_block        = "0.0.0.0/0"
}
3. variables.tf
hcl 复制代码
variable "region" {
  description = "Tencent Cloud region"
  default     = "ap-beijing"
}
4. outputs.tf
hcl 复制代码
output "instance_id" {
  value = tencentcloud_instance.example.instance_id
}

output "public_ip" {
  value = tencentcloud_instance.example.public_ip_address
}

三、执行流程

1. 初始化项目
bash 复制代码
terraform init
2. 预览变更
bash 复制代码
terraform plan
3. 创建资源
bash 复制代码
terraform apply
# 输入 yes 确认
4. 查看输出
bash 复制代码
terraform output
5. 销毁资源
bash 复制代码
terraform destroy

四、关键注意事项

项目 说明
Region 使用 ap-beijing(北京)或其他支持的区域。
镜像 ID 替换为腾讯云公共镜像 ID,可通过 API 查询。
实例类型 S1.SMALL1 为小型实例,适合测试。生产环境需选择更高性能类型。
密钥对 确保已在腾讯云控制台创建密钥对并绑定到实例。
安全组规则 可根据需求添加更多规则(如 HTTPS、数据库端口等)。

五、常见问题

1. 认证失败
  • 检查 SecretId 和 SecretKey 是否正确。
  • 确保环境变量已导出或在 provider 中显式配置。
2. 镜像 ID 无效
  • 使用腾讯云提供的公共镜像 ID(如 img-8xr35l5w),或通过 API 查询最新镜像。
3. VPC/Subnet 依赖关系
  • 确保 VPC 和子网的 availability_zone 一致。

六、扩展学习

  • 远程状态存储 :使用腾讯云 COS(对象存储)保存 .tfstate 文件。
  • 模块化:将 VPC、安全组封装为模块,提高复用性。
  • 多环境管理 :通过 terraform -var 参数区分开发/生产环境。

通过以上步骤,你可以使用 Terraform 在腾讯云上自动化部署 CVM 实例及关联资源。

相关推荐
叼烟扛炮11 小时前
[LangChain] 学习笔记(026):模型根本没有记忆——「多轮对话」是你每次把历史重发一遍
笔记·学习·langchain
MSTcheng.17 小时前
【LinuxIO篇】Linux学习第二十三弹——fopen和open有什么区别?从 FILE* 到 fd,讲透 Linux 文件 IO
linux·运维·学习·io·系统调用·fd
Z21-X18 小时前
云原生运维学习笔记:Kubernetes Service 发现、类型与 kube-proxy 实战
运维·笔记·学习·云原生·kubernetes
sealaugh3218 小时前
powerbi学习笔记(day2)powerbi进阶
笔记·学习·powerbi
叼烟扛炮2 天前
[LangChain] 学习笔记(24):流式不改总耗时,只改「多久看到第一个字」——实测差 3 倍
笔记·学习·langchain
半扇排骨2 天前
特别篇:在线 XSS 靶场 haozi 分享与通关笔记
学习·安全·靶场·xss
艾莉丝努力练剑3 天前
【AI接入大模型SDK】解决接口连接异常问题
前端·c++·人工智能·学习·ai·架构
Severus_black3 天前
【C++初阶】模板进阶
c语言·c++·学习
紫金修道3 天前
【机械臂视觉】学习笔记一:相机成像模型与内参标定
笔记·数码相机·学习
旖旎夜光3 天前
【C 语言】(一)(下)指针进阶:const、二级指针、函数指针与指针题
c语言·开发语言·笔记·学习