安装 Terraform for Tencent 使用

news2024/9/21 2:47:59

第一步 :下载安装包

前往 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

  • Terraform 0.13.x
  • Go 1.18.x (to build the provider plugin)

因为依赖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.

参考文档:云资源自动化 for Terraform CLI 方式-快速开始-文档中心-腾讯云

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1965171.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Vulnhub靶机-Jangow 1.0.1

Vulnhub靶机-Jangow 1.0.1 修改为NAT模式 ?buscarecho <?php eval($_POST[cmd])?> >shell.php后面试了试很多网上的方法反弹shell但都不行

LeetCode面试150——122买卖股票的最佳时机II

题目难度&#xff1a;中等 默认优化目标&#xff1a;最小化平均时间复杂度。 Python默认为Python3。 目录 1 题目描述 2 题目解析 3 算法原理及题目解析 3.1 动态规划 3.2 贪心算法 参考文献 1 题目描述 给你一个整数数组 prices &#xff0c;其中 prices[i] 表示某支…

Spring-FactoryBean来配置Bean

spring通过FactoryBean配置&#xff0c;比前面的工厂方法配置Bean要重要些&#xff0c;因为我们整合很多第三方的框架的时候&#xff0c;需要用到FactoryBean来配置第三方框架中的bean 对象&#xff0c;从而把第三方框架整合到spring中来&#xff01;当然在整合这些第三方框架的…

2024西安铁一中集训DAY28 ---- 模拟赛(简单dp + 堆,模拟 + 点分治 + 神秘dp)

文章目录 前言时间安排及成绩题解A. 江桥不会做的签到题&#xff08;简单dp&#xff09;B. 江桥树上逃&#xff08;堆&#xff0c;模拟&#xff09;C. 括号平衡路径&#xff08;点分治&#xff09;D. 回到起始顺序&#xff08;dp&#xff0c;组合数学&#xff09; 前言 T2好难…

吴恩达老师机器学习-ex4

梯度检测没有实现。有借鉴网上的部分 导入相关库&#xff0c;读取数据 因为这次的数据是mat文件&#xff0c;需要使用scipy库中的loadmat进行读取数据。 通过对数据类型的分析&#xff0c;发现是字典类型&#xff0c;查看该字典的键&#xff0c;可以发现又X&#xff0c;y等关…

使用obsidian-webpage-export 插件,将 Obsidian 中的笔记导出为网页

&#x1f49d;&#x1f49d;&#x1f49d;欢迎莅临我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐&#xff1a;「storm…

2024下《网络工程师》案例简答题,刷这些就够了!

距离2024下半年软考已经越来越近了&#xff0c;不知道今年备考软考网络工程师的同学们开始准备了吗&#xff1f; 简答题一直是网工拿分的重点区域&#xff0c;对于许多考生来说&#xff0c;也往往是最具挑战性的部分。今天我就把那些重要的案例简答题类型整理汇总给大家&#x…

【Python学习手册(第四版)】学习笔记12-if语句(and、or、三元表达式)详解

个人总结难免疏漏&#xff0c;请多包涵。更多内容请查看原文。本文以及学习笔记系列仅用于个人学习、研究交流。 本文较简单&#xff0c;对if语句的格式、示例、多路做了示例&#xff0c;以及真值测试&#xff08;and、or等&#xff09;介绍&#xff0c;最后介绍了三三元表达式…

M12电连接器的编码分类及应用领域分析

12电连接器的编码主要包括A、B、C、D、X、S、T、K、L等类型&#xff0c;每种编码都有其特定的应用场景和功能&#xff1a; A编码&#xff1a;适用于传感器、直流电、1G以太网。 B编码&#xff1a;主要用于PROFIBUS总线系统。 C编码&#xff1a;适用于交流电。 D编码&#x…

十八次(虚拟主机与vue项目、samba磁盘映射、nfs共享)

1、虚拟主机搭建环境准备 将原有的nginx.conf文件备份 [rootserver ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak[rootserver ~]# grep -Ev "#|^$" /usr/local/nginx/conf/nginx.conf[rootserver ~]# grep -Ev "#|^$"…

视频编辑SDK,功能全面、包体小,支持高效灵活接入

如何在快节奏的市场环境中&#xff0c;快速制作出高质量、富有吸引力的视频内容&#xff0c;成为了众多企业面临的共同挑战。美摄科技&#xff0c;作为视频编辑技术的先行者&#xff0c;携其全面功能、小巧包体、高效灵活接入的视频编辑SDK&#xff0c;为企业视频创作带来了革命…

pytorch tensor的高级索引

1. 取索引的方式[[a,b,c...],[a,b,c...] ] 下面的例子对于这个x进行操作 全取x, print(x[:,:,:]) 第一个冒号代表0轴&#xff0c;第二个冒号代表1轴&#xff0c;第三个冒号代表2轴 第一个冒号可以选这类 第二个冒号可以选这类 第三个冒号可以选这类 2. 比较符号 idxx[:,0,:…

全麦饼:健康与美味的完美结合

在追求健康饮食的当下&#xff0c;全麦饼以其独特的魅力脱颖而出&#xff0c;成为了众多美食爱好者的新宠。食家巷全麦饼&#xff0c;顾名思义&#xff0c;主要由全麦面粉制作而成。与普通面粉相比&#xff0c;全麦面粉保留了小麦的麸皮、胚芽和胚乳&#xff0c;富含更多的膳食…

基于SpringBoot+Vue的热门网游推荐网站(带1w+文档)

基于SpringBootVue的热门网游推荐网站(带1w文档) 基于SpringBootVue的热门网游推荐网站(带1w文档) 本系统选用B/S结构开发&#xff0c;它是一个提供可以对热门网游推荐进行信息管理的系统&#xff0c;用户可以在该系统获取最新动态&#xff0c;可以结识更多的朋友&#xff0c;产…

Scrapy vs Beautifulsoup - 哪一个更适合您?

你是新手开发者还是经验丰富的开发者&#xff1f; 不管怎样&#xff0c;有一点是肯定的——网页爬虫对你来说可能很棘手&#xff01; 因此&#xff0c;我们必须选择一个高效的工具来简化我们的工作。 你在权衡哪个更适合网页爬虫&#xff0c;Scrapy还是BeautifulSoup吗&…

基于alpha shapes的任意空间平面点云边缘提取(python)

1、背景介绍 基于alpha shapes提取二维平面点云边缘点&#xff0c;一般是将点云投影至xoy平面&#xff0c;利用x、y坐标根据alpha shapes判别准则即可实现边缘点识别。具体的原理&#xff0c;可以参考之前博客&#xff1a; 基于alpha shapes的点云边缘点提取&#xff08;pytho…

Vulnhub - JANGOW: 1.0.1 靶标实战

靶场地址&#xff1a;https://www.vulnhub.com/entry/jangow-101,754/ 靶场IP&#xff1a;192.168.56.118 信息收集 使用御剑对目标进行扫描 该靶标开启了21、80两个端口&#xff0c;21端口运行服务为ftp&#xff0c;其版本为 vsftpd 3.0.3 &#xff0c;80端口运行服务为Apa…

飞塔fortigate怎么进入cli

好几个人问了好几次 捂脸 就右上角找到这个图标点进去

干货来喽:车载语音识别测试全面分析笔记!

从台架到实车的语音识别专项测试实战&#xff0c;笔记很详细哦&#xff0c;跟着了解学习起来&#xff01; 一、语音识别原理及测试范围 1、语音识别的原理&#xff1a; ① 通过麦克风输入人的声音 ② 声学处理&#xff1a;处理掉杂音,噪音 ③ 特征处理&#xff1a;提取声音中…

花几千上万学习Java,真没必要!(三十七)

IO 流&#xff1a; 读取数据到内存的过程&#xff1a;可以包括从硬盘、网络或其他存储设备中将数据加载到内存中&#xff0c;以便程序能够访问和处理这些数据。 写入数据到硬盘的过程&#xff1a;通常是将内存中的数据保存到硬盘或其他持久性存储设备中&#xff0c;以便长期存…