上云无忧 > 文档中心 > 腾讯云弹性微服务 TEM Terraform 管理 - 管理现存资源
弹性微服务 TEM
腾讯云弹性微服务 TEM Terraform 管理 - 管理现存资源

文档简介:
操作场景: 若您在使用 Terraform 管理云资源之前,已经通过腾讯云控制台创建了资源,则可参考本文进行操作,将现存资源使用 Terraform 管理。
*此产品及展示信息均由腾讯云官方提供。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠

操作场景

若您在使用 Terraform 管理云资源之前,已经通过腾讯云控制台创建了资源,则可参考本文进行操作,将现存资源使用 Terraform 管理。

操作步骤

使用 Terraform 接管已经存在的资源,实际上在 Terraform 源文件和状态文件里都反映出该资源的状态即可。本文以使用 Terraform 管理现存的 弹性微服务环境与应用 为例。

步骤1:获取资源 ID

1. 获取应用 ID,您可以在控制台 > 应用管理界面中获取现存应用的 ID。

2. 获取环境 ID,您可以在控制台 > 环境管理界面中获取现存环境的 ID。

步骤2:导入资源文件

1. 进入 Terraform 工作目录,执行以下命令,查看 main.tf 内容。
		
cat main.tf
返回结果如下:
		
resource "tencentcloud_tem_environment" "environment" { }
resource "tencentcloud_tem_application" "application" { }
resource "tencentcloud_tem_workload" "workload" {
application_id = tencentcloud_tem_application.application.id
environment_id = tencentcloud_tem_environment.environment.id
deploy_version = "1.9"
deploy_mode = "IMAGE"
img_repo = "qcloud/nginx"
init_pod_num = 1
cpu_spec = 1
memory_spec = 1
}
在此示例中,main.tf 中已经规划了一个名为 environment 的环境和一个名为 application 的应用,并通过名为 workload 的工作负载部署至以上的环境,但是从以上代码中可以看出,application 和 environment 为空。在此将展示如何将已经创建的存量环境和应用纳入 Terraform 管理。
2. 在文件所在目录下执行以下命令,完成初始化工作。
		
terraform init --upgrade
返回结果如下:
		
Initializing the backend...
Initializing provider plugins...
- Finding latest version of tencentcloudstack/tencentcloud...
- Using previously-installed tencentcloudstack/tencentcloud v1.78.6
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.
3. 初始化完成后,执行以下命令,将资源导入状态文件。
		
terraform import tencentcloud_tem_application.application recourceID
terraform import tencentcloud_tem_environment.environment en-vpzz6d6p
返回的信息如下所示:
		
tencentcloud_tem_application.application: Importing from ID "app-l5m4gaz5"...
tencentcloud_tem_application.application: Import prepared!
Prepared tencentcloud_tem_application for import
tencentcloud_tem_application.application: Refreshing state... [id=app-l5m4gaz5]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
		
tencentcloud_tem_environment.environment: Importing from ID "en-vpzz6d6p"...
tencentcloud_tem_environment.environment: Import prepared!
Prepared tencentcloud_tem_environment for import
tencentcloud_tem_environment.environment: Refreshing state... [id=en-vpzz6d6p]
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
4. 执行以下命令,查看状态文件
		
cat terraform.tfstate
可查看如下关于资源的信息:
		
{
"version": 4,
"terraform_version": "1.2.8",
"serial": 47,
"lineage": "c12a26a4-134b-e0e5-9ba1-0b2234xxxxxx",
"outputs": {},
"resources": [
{
"mode": "managed",
"type": "tencentcloud_tem_application",
"name": "application",
"provider": "provider[\"registry.terraform.io/tencentcloudstack/tencentcloud\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"application_name": "demo",
"coding_language": "JAVA",
"description": "demo for terraform",
"id": "app-apko7765",
"instance_id": "",
"repo_name": "qcloud/nginx",
"repo_server": null,
"repo_type": 2,
"use_default_image_service": null
},
"sensitive_attributes": [],
"private": "eyJzY2hlbWFfdmVyc2lvbiI6XXXXXX=="
}
]
},
{
"mode": "managed",
"type": "tencentcloud_tem_environment",
"name": "environment",
"provider": "provider[\"registry.terraform.io/tencentcloudstack/tencentcloud\"]",
"instances": [
{
"schema_version": 0,
"attributes": {
"description": "demo for terraform",
"environment_name": "test_tf",
"id": "en-vpzz6d6p",
"subnet_ids": [
"subnet-5ob92rbm",
"subnet-dwpro9ni"
],
"vpc": "vpc-dd5m94px"
},
"sensitive_attributes": [],
"private": "eyJzY2hlbWFfdmVyc2lvbiI6XXXXXX=="
}
]
}
]
}

步骤3:更新源文件

1. 执行以下命令,打印资源信息。
		
terraform show
返回的信息如下所示:
		
# tencentcloud_tem_application.application:
resource "tencentcloud_tem_application" "application" {
application_name = "demo"
coding_language = "JAVA"
description = "demo for terraform"
id = "app-apko7765"
repo_name = "qcloud/nginx"
repo_type = 2
}
# tencentcloud_tem_environment.environment:
resource "tencentcloud_tem_environment" "environment" {
description = "demo for terraform"
environment_name = "test_tf"
id = "en-vpzz6d6p"
subnet_ids = [
"subnet-5ob92rbm",
"subnet-dwpro9ni",
]
vpc = "vpc-dd5m94px"
}
2. 将资源代码拷贝至 Terraform 源文件 main.tf 中。需删除其中不可设置的选项,例如 id。完成编辑后,main.tf 文件内容如下所示:
		
resource "tencentcloud_tem_environment" "environment" {
description = "demo for terraform"
environment_name = "test_tf"
# id = "en-vpzz6d6p"
subnet_ids = [
"subnet-5ob92rbm",
"subnet-dwpro9ni",
]
vpc = "vpc-dd5m94px"
}
resource "tencentcloud_tem_application" "application" {
application_name = "demo"
coding_language = "JAVA"
description = "demo for terraform"
# id = "app-apko7765"
repo_name = "qcloud/nginx"
repo_type = 2
}
resource "tencentcloud_tem_workload" "workload" {
application_id = "${tencentcloud_tem_application.application.id}"
environment_id = "${tencentcloud_tem_environment.environment.id}"
deploy_version = "1.9"
deploy_mode = "IMAGE"
img_repo = "qcloud/nginx"
init_pod_num = 1
cpu_spec = 1
memory_spec = 1
}

步骤4:验证结果

1. 执行以下命令,使用当前工作目录下的代码和状态文件更新 Terraform 规划。
		
terraform plan
2. 返回信息如下所示,可查看 Terraform 已成功接管。

		

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_workload.workload will be created
+ resource "tencentcloud_tem_workload" "workload" {
+ application_id = "app-apko7765"
+ cpu_spec = 1
+ deploy_mode = "IMAGE"
+ deploy_version = "1.9"
+ environment_id = "en-vpzz6d6p"
+ id = (known after apply)
+ img_repo = "qcloud/nginx"
+ init_pod_num = 1
+ memory_spec = 1
+ env_conf {
+ config = (known after apply)
+ key = (known after apply)
+ secret = (known after apply)
+ type = (known after apply)
+ value = (known after apply)
}
}
Plan: 1 to add, 0 to change, 0 to destroy.

至此,Terraform 已经接管了现存的 environment 和 application 资源,并将规划创建 workload。之后,您可以通过 Terraform 对资源进行编辑和删除。
相似文档
  • 弹性微服务支持哪些语言? 弹性微服务目前支持 Java 语言的微服务应用,针对非微服务应用,您可以通过制作 Docker 镜像并上传至弹性微服务,助您部署任何语言的应用。
  • 日志中出现乱码,如何解决? 问题描述: 部署至弹性微服务中的业务,如果在日志中显示乱码,一般是由于 locale 的错误配置导致。 遇到这种情况时,可以通过以下方案解决,设置与您应用部署地域相应的编码配置。
  • 为使用腾讯云微服务引擎-弹性微服务(以下简称“本服务”),您应当阅读并遵守《腾讯云微服务引擎-弹性微服务服务等级协议》(以下简称“本协议”或“SLA”),以及《腾讯云服务协议》。本协议包含本服务的术语和定义、服务可用性/服务成功率等级指标、赔偿方案、免责条款等相关内容。除非另有约定,本协议不适用于本服务正在内测的功能,腾讯云对本服务的内测功能不作可用性、服务质量等任何承诺。请您务必审慎阅读、充分理解各条款内容,限制、免责条款或者其他涉及您重大权益的条款可能会以加粗、加下划线等形式提示您重点注意。
  • API 网关(API Gateway)是 API 托管服务,提供 API 的完整生命周期管理,包括创建、维护、发布、运行、下线等。您可使用 API Gateway 封装自身业务,将您的数据、业务逻辑或功能安全可靠的开放出来,用以实现自身系统集成、以及与合作伙伴的业务连接。
  • 2023-07,支持成都地域:专享实例支持新地域:成都,可在购买专享实例直接选购。
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部