环境资源管理

最近更新时间:2022-09-09 14:34:54

我的收藏

操作场景

在弹性微服务中,环境是一组计算、网络、存储等资源的集合。TEM 提供多环境管理的功能,您可根据自身业务需要,创建开发、测试、预发、生产等多个环境,分别部署应用,达成环境隔离的目的。不同环境中的应用彼此隔离。 本文介绍如何使用 Terraform 创建、编辑和删除 TEM 环境资源。

前提条件

请参见 Terraform 快速开始 安装 Terraform 并配置腾讯云鉴权。

TEM 环境资源

Terraform 的 tencentcloud_tem_environment 资源提供了以下参数:
必填:environment_name 环境名称。
必填:vpc 指定私有网络。
必填:subnet_ids 指定子网,
可选:description 环境描述信息。 详细信息请参见 terraform_tem_environment

创建环境

此处以在广州地域下创建一个命名为 tf-test 的环境为示例:
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"
}
region:填写地域 ID。
获取凭证:在 API 密钥管理 面中创建并复制 SecretId 和 SecretKey。
2. 创建 main.tf 文件,配置腾讯云 Provider 并创建弹性微服务环境。文件内容如下:
resource "tencentcloud_tem_environment" "environment" {
environment_name = "test_tf"
description = "demo for terraform"
vpc = "my-vpc-id"
subnet_ids = ["my-subnet-1-id", "my-subnet-2-id"]
}
3. 执行以下命令,初始化工作目录并下载插件。
terraform init
返回信息如下:
Initializing the backend...

Initializing provider plugins...
- Finding latest version of tencentcloudstack/tencentcloud...
- Installing tencentcloudstack/tencentcloud v1.77.4...
- Installed tencentcloudstack/tencentcloud v1.77.4 (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. 执行以下命令,创建资源。
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_tem_environment.environment will be created
+ resource "tencentcloud_tem_environment" "environment" {
+ description = "demo for terraform"
+ environment_name = "test_tf"
+ id = (known after apply)
+ subnet_ids = [
+ "subnet-5ob92rbm",
+ "subnet-dwpro9ni",
]
+ vpc = "vpc-dd5m94px"
}

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_tem_environment.environment: Creating...
tencentcloud_tem_environment.environment: Creation complete after 2m37s [id=en-853agv3j]
5. 执行完毕后,您可以在 弹性微服务控制台 查看创建的资源。

编辑环境

以编辑上面所创建的环境名称为示例。
1. main.tf 文件中编辑环境资源信息:
resource "tencentcloud_tem_environment" "environment" {
environment_name = "test_tf_renamed"
description = "demo for terraform"
vpc = "my-vpc-id"
subnet_ids = ["my-subnet-1-id", "my-subnet-2-id"]
}
2. 执行 terraform plan 命令更新计划,应返回如下信息:
tencentcloud_tem_environment.environment: Refreshing state... [id=en-853agv3j]

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_tem_environment.environment will be updated in-place
~ resource "tencentcloud_tem_environment" "environment" {
~ environment_name = "test_tf" -> "test_tf_renamed"
id = "my-env-id"
# (3 unchanged attributes hidden)
}

Plan: 0 to add, 1 to change, 0 to destroy.
3. 执行 terraform apply 执行更新了的计划,返回信息如下:
tencentcloud_tem_environment.environment: Refreshing state... [id=en-853agv3j]

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_tem_environment.environment will be updated in-place
~ resource "tencentcloud_tem_environment" "environment" {
~ environment_name = "test_tf" -> "test_tf_renamed"
id = "en-853agv3j"
# (3 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_tem_environment.environment: Modifying... [id=en-853agv3j]
tencentcloud_tem_environment.environment: Modifications complete after 1s [id=en-853agv3j]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

销毁环境

以销毁上面所创建的环境资源为例。
1. 执行以下命令。
terraform destroy
2. 销毁成功后返回以下信息。
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_tem_environment.environment will be destroyed
- resource "tencentcloud_tem_environment" "environment" {
- description = "demo for terraform" -> null
- environment_name = "test_tf_renamed" -> null
- id = "en-853agv3j" -> null
- subnet_ids = [
- "subnet-5ob92rbm",
- "subnet-dwpro9ni",
] -> null
- vpc = "vpc-dd5m94px" -> 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_tem_environment.environment: Destroying... [id=en-853agv3j]
tencentcloud_tem_environment.environment: Destruction complete after 2s

Destroy complete! Resources: 1 destroyed.