腾讯云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. 配置腾讯云凭证
  • 登录 腾讯云控制台 获取 SecretIdSecretKey

  • 设置环境变量:

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

六、扩展学习

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

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

相关推荐
wdfk_prog6 小时前
[Linux]学习笔记系列 -- [drivers][i2c]i2c-dev
linux·笔记·学习
越努力越幸运5087 小时前
CSS3学习之网格布局grid
前端·学习·css3
chillxiaohan8 小时前
GO学习记录——多文件调用
开发语言·学习·golang
Funny_AI_LAB9 小时前
AI Agent最新重磅综述:迈向高效智能体,记忆、工具学习和规划综述
人工智能·学习·算法·语言模型·agi
代码游侠11 小时前
学习笔记——Linux内核与嵌入式开发1
linux·运维·前端·arm开发·单片机·嵌入式硬件·学习
宇钶宇夕11 小时前
CoDeSys入门实战一起学习(二十八):(LD)三台电机顺起逆停程序详解—上升、下降沿使用上
单片机·嵌入式硬件·学习
科技林总11 小时前
【系统分析师】6.5 电子商务
学习
代码游侠11 小时前
C语言核心概念复习(一)
c语言·开发语言·c++·笔记·学习
tb_first11 小时前
万字超详细苍穹外卖学习笔记1
java·jvm·spring boot·笔记·学习·tomcat·mybatis
今儿敲了吗11 小时前
10| 扫雷
c++·笔记·学习