第一步 :下载安装包
前往 Terraform 官网,使用命令行直接安装 Terraform 或下载二进制安装文件。
解压并配置全局路径
Linux/MAC:export PATH=$PATH:${可执行文件所在目录}
例如:
export PATH=$PATH:$/usr/bin/terraform
Windows:可执行文件所在目录添加到系统环境变量 PATH 中
文章以linux 为例:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
[root@mac-vm-temp ~]# terraform --version
Terraform v1.9.3
on linux_amd64
表示安装成功了;
第二步:安装Terraform Provider For TencentCloud
Requirements
因为依赖go环境执行,所以需要先安装go;执行以下命令,下载go安装包并解压至【/usr/local/go】目录中
wget https://dl.google.com/go/go1.21.4.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.21.4.linux-amd64.tar.gz
使用go,还需要设置环境变量,通过编辑【/etc/profile】文件;
vim /etc/profile
追加一行 :export PATH=$PATH:/usr/local/go/bin
按下Esc,然后输入【:wq】保存并退出。
使用 source 命令重新加载 【/etc/profile】文件,以便使修改后的环境变量立即生效。
source /etc/profile
测试安装是否成功
选个测试目录,编辑个go文件
vim hello.go
package main
import "fmt"
func main(){
fmt.Println("Hello, world!")
}
保存并退出编辑器后,就可以使用go命令编译刚才的程序了。
go build hello.go
生成可执行文件 hello;
运行程序
./hello
输出 Hello, world! 表示安装成功。
安装 Terraform Provider For TencentCloud
$ mkdir -p $GOPATH/src/github.com/tencentcloudstack
$ cd $GOPATH/src/github.com/tencentcloudstack
$ git clone https://github.com/tencentcloudstack/terraform-provider-tencentcloud.git
$ cd terraform-provider-tencentcloud
$ go build .
备注:go build . 时间会有点长,执行时不会有任何输出,预计15-20min左右。
参考文档:https://github.com/tencentcloudstack/terraform-provider-tencentcloud
认证和鉴权
凭证获取
在首次使用 Terraform 之前,请前往 云 API 密钥页面 申请安全凭证 SecretId 和 SecretKey。若已有可使用的安全凭证,则跳过该步骤。
1. 登录 访问管理控制台,在左侧导航栏,选择访问密钥 > API 密钥管理。
2. 在 API 密钥管理页面,单击新建密钥,即可以创建一对 SecretId/SecretKey。
静态凭证鉴权
在用户目录下创建 provider.tf
文件,输入如下内容:
my-secret-id
及 my-secret-key
请替换为 获取凭证 中的 SecretId 和 SecretKey。
provider "tencentcloud" {
secret_id = "my-secret-id"
secret_key = "my-secret-key"
}
环境变量鉴权
请将如下信息添加至环境变量配置:
YOUR_SECRET_ID
及 YOUR_SECRET_KEY
请替换为 获取凭证 中的 SecretId 和 SecretKey。
export TENCENTCLOUD_SECRET_ID=YOUR_SECRET_ID
export TENCENTCLOUD_SECRET_KEY=YOUR_SECRET_KEY
使用 Terraform 创建腾讯云 VPC
1. 创建 provider.tf 文件,指定 provider 配置信息。文件内容如下:
terraform {
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud"
# 通过version指定版本
# version = ">=1.60.18"
}
}
}
provider "tencentcloud" {
region = "ap-guangzhou"
# secret_id = "my-secret-id"
# secret_key = "my-secret-key"
}
2. 创建 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"]
is_multicast = false
tags = {
"test" = "test"
}
}
3. 执行以下命令,初始化工作目录并下载插件。
terraform init
如果返回遇到下载失败问题报错如下:
Initializing the backend...
Initializing provider plugins...
- Finding latest version of tencentcloudstack/tencentcloud...
╷
│ Error: Failed to install provider
│
│ Error while installing tencentcloudstack/tencentcloud v1.78.5: could not query provider registry forregistry.terraform.io/tencentcloudstack/tencentcloud: failed to retrieve authentication checksums for provider: the request failed after 2 attempts, please try again later: Get
│ "https://github.com/tencentcloudstack/terraform-provider-tencentcloud/releases/download/v1.78.5/terraform-provider-tencentcloud_1.78.5_SHA256SUMS": context deadline exceeded
问题定位
Terraform 的默认镜像源 registry.terraform.io
部署在国外,在国内拉取镜像时可能存在网络问题导致拉取速度慢、甚至无法成功拉取。为解决此问题,您可以使用腾讯云提供的 镜像源。
操作步骤
创建 Terraform CLI 配置文件
按需创建 .terraformrc
或 terraform.rc
配置文件,您可以将其与其他配置文件放在一个文件夹内,位置取决于主机操作系统:
在 Windows 上,该文件必须命名 terraform.rc
,并放置在相关用户的 %APPDATA%
目录中。该目录的物理位置取决于您的 Windows 版本和系统配置;可以在 PowerShell 中使用 $env:APPDATA
查找其在系统上的位置。
在所有其他系统上,文件必须命名 .terraformrc
,并直接放在相关用户的 home 目录中。Terraform CLI 配置文件的位置也可以使用 TF_CLI_CONFIG_FILE
环境变量来指定。任何此类文件都应遵循命名模式 *.tfrc
,详情见 Terraform 官网指引。
以 LINUX 为例,创建 .terraformrc
文件在用户的 home 目录下,内容如下:
provider_installation {
network_mirror {
url = "https://mirrors.tencent.com/terraform/"
// 限制只有腾讯云相关Provider, 从url中指定镜像源下载
include = ["registry.terraform.io/tencentcloudstack/*"]
}
direct {
// 声明除了腾讯云相关Provider, 其它Provider依然从默认官方源下载
exclude = ["registry.terraform.io/tencentcloudstack/*"]
}
}
创建完成保存后,需要再声明下:
export TF_CLI_CONFIG_FILE=.terraformrc
再执行 terraform init 就正常了。
init 加速参考文档:云资源自动化 for Terraform Init 加速-常见问题-文档中心-腾讯云
返回信息如下所示:
Initializing the backend...
Initializing provider plugins...
- Finding latest version of tencentcloudstack/tencentcloud...
- Installing tencentcloudstack/tencentcloud v1.60.18...
- Installed tencentcloudstack/tencentcloud v1.60.18 (signed by a HashiCorp partner, key ID 84F69E1C1BECF459)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
4. 执行以下命令,升级 Provider 版本。
terraform init -upgrade
5. 执行以下命令,查看执行计划,显示将要创建的资源详情。
terraform plan
返回信息如下所示:
terraform_workspace terraform plan
Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
+ create
Terraform will perform the following actions:
# tencentcloud_vpc.foo will be created
+ resource "tencentcloud_vpc" "foo" {
+ cidr_block = "10.0.0.0/16"
+ create_time = (known after apply)
+ default_route_table_id = (known after apply)
+ dns_servers = [
+ "119.29.29.29",
+ "8.8.8.8",
]
+ id = (known after apply)
+ is_default = (known after apply)
+ is_multicast = false
+ name = "ci-temp-test-updated"
+ tags = {
+ "test" = "test"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
─────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these
actions if you run "terraform apply" now.
6. 执行以下命令,创建资源。
terraform apply
根据提示输入 yes 创建资源,返回信息如下所示:
➜ terraform_workspace terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
+ create
Terraform will perform the following actions:
# tencentcloud_vpc.foo will be created
+ resource "tencentcloud_vpc" "foo" {
+ cidr_block = "10.0.0.0/16"
+ create_time = (known after apply)
+ default_route_table_id = (known after apply)
+ dns_servers = [
+ "119.29.29.29",
+ "8.8.8.8",
]
+ id = (known after apply)
+ is_default = (known after apply)
+ is_multicast = false
+ name = "ci-temp-test-updated"
+ tags = {
+ "test" = "test"
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
tencentcloud_vpc.foo: Creating...
tencentcloud_vpc.foo: Still creating... [10s elapsed]
tencentcloud_vpc.foo: Creation complete after 13s [id=vpc-07mx4yfd]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
执行完毕后,您可以在腾讯云控制台查看创建的资源。
7. (可选)更新资源。
若您将资源配置信息修改为如下所示内容:
resource "tencentcloud_vpc" "foo" {
name = "ci-temp-test-updated2"
cidr_block = "10.0.0.0/16"
dns_servers = ["119.29.29.29", "8.8.8.8"]
is_multicast = false
tags = {
"test" = "test"
}
}
请执行 terraform plan
命令更新计划。返回信息如下所示:
➜ terraform_workspace terraform plan
tencentcloud_vpc.foo: Refreshing state... [id=vpc-jhmdf9q9]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# tencentcloud_vpc.foo will be updated in-place
~ resource "tencentcloud_vpc" "foo" {
id = "vpc-jhmdf9q9"
~ name = "ci-temp-test-updated" -> "ci-temp-test-updated2"
tags = {
"test" = "test"
}
# (6 unchanged attributes hidden)
}
Plan: 0 to add, 1 to change, 0 to destroy.
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply"
now.
执行terraform apply
命令,应用更新的数据创建资源。返回信息如下:
➜ terraform_workspace terraform apply
tencentcloud_vpc.foo: Refreshing state... [id=vpc-jhmdf9q9]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
~ update in-place
Terraform will perform the following actions:
# tencentcloud_vpc.foo will be updated in-place
~ resource "tencentcloud_vpc" "foo" {
id = "vpc-jhmdf9q9"
~ name = "ci-temp-test-updated" -> "ci-temp-test-updated2"
tags = {
"test" = "test"
}
# (6 unchanged attributes hidden)
}
Plan: 0 to add, 1 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
tencentcloud_vpc.foo: Modifying... [id=vpc-jhmdf9q9]
tencentcloud_vpc.foo: Modifications complete after 1s [id=vpc-jhmdf9q9]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
8. 您可根据实际需求,执行以下命令销毁资源。
terraform destroy
返回信息如下所示:
➜ terraform_workspace terraform destroy
tencentcloud_vpc.foo: Refreshing state... [id=vpc-07mx4yfd]
Terraform used the selected providers to generate the following execution plan. Resource actions are
indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# tencentcloud_vpc.foo will be destroyed
- resource "tencentcloud_vpc" "foo" {
- cidr_block = "10.0.0.0/16" -> null
- create_time = "2021-12-15 16:20:32" -> null
- default_route_table_id = "rtb-4m1nmo0e" -> null
- dns_servers = [
- "119.29.29.29",
- "8.8.8.8",
] -> null
- id = "vpc-07mx4yfd" -> null
- is_default = false -> null
- is_multicast = false -> null
- name = "ci-temp-test-updated" -> null
- tags = {
- "test" = "test"
} -> null
}
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
tencentcloud_vpc.foo: Destroying... [id=vpc-07mx4yfd]
tencentcloud_vpc.foo: Destruction complete after 7s
Destroy complete! Resources: 1 destroyed.