有奖捉虫:办公协同&微信生态&物联网文档专题 HOT

操作场景

在弹性微服务中,为了帮助您聚焦设计与部署业务应用程序,省去对集群与服务器的考虑,抽象出了应用的概念。 本文介绍如何使用 Terraform 创建应用、部署和删除应用。

前提条件

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

TEM应用资源

Terraform的tencentcloud_tem_application 资源提供了以下参数:
必填:application_name 应用名称。
必填:coding_language 应用的开发语言。
可选:repo_type 镜像仓库类型:“0”为个人版镜像仓库,“1”为企业版镜像仓库,“2”为公共镜像仓库,“4”为演示镜像。
可选:repo_name 镜像仓库名称。
可选:repo_server 镜像仓库地址。
可选:use_default_image_service 是否新建镜像仓库。 详细信息请参见 tencentcloud_tem_application

创建应用

此处以创建一个应用,并在广州地域下的 test_tf 环境中部署该应用为示例。
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_application" "application" {
application_name = "demo"
description = "demo for terraform"
coding_language = "JAVA"
repo_type = 2
repo_name = "qcloud/nginx"
repo_server = "ccr.ccs.tencentyun.com"
}
repo_type 选择为公共镜像仓库。
repo_namerepo_server 填写相关镜像仓库名称和地址。
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 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_application.application will be created
+ resource "tencentcloud_tem_application" "application" {
+ application_name = "demo_tf"
+ coding_language = "JAVA"
+ description = "demo for terraform"
+ id = (known after apply)
+ repo_name = "qcloud/nginx"
+ repo_server = "ccr.ccs.tencentyun.com"
+ repo_type = 2
+ use_default_image_service = 0
}

Plan: 1 to add, 0 to change, 0 to destroy.
5. 执行以下命令,创建资源。
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_application.application will be created
+ resource "tencentcloud_tem_application" "application" {
+ application_name = "demo"
+ coding_language = "JAVA"
+ description = "demo for terraform"
+ id = (known after apply)
+ repo_name = "qcloud/nginx"
+ repo_server = "ccr.ccs.tencentyun.com"
+ repo_type = 2
+ use_default_image_service = 0
}

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_application.application: Creating...
tencentcloud_tem_application.application: Creation complete after 1s [id=app-vpzq42ep]
6. 执行完毕后,您可以在 弹性微服务控制台 应用列表中查看创建的应用。

部署应用

此步骤演示以拉取镜像的方式将上面创建的应用部署至 test-tf 环境中。
1. main.tf 文件,添加弹性微服务工作负载资源。内容如下:
resource "tencentcloud_tem_workload" "workload" {
application_id = "my-app-id"
environment_id = "my-env-id"
deploy_version = "1.9"
deploy_mode = "IMAGE"
img_repo = "qcloud/nginx"
init_pod_num = 1
cpu_spec = 1
memory_spec = 1
}
2. 执行 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_tem_workload.workload will be created
+ resource "tencentcloud_tem_workload" "workload" {
+ application_id = "my-app-id"
+ cpu_spec = 1
+ deploy_mode = "IMAGE"
+ deploy_version = "1.9"
+ environment_id = "my-evn-id"
+ 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.
3. 执行 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_workload.workload will be created
+ resource "tencentcloud_tem_workload" "workload" {
+ application_id = "my-app-id"
+ cpu_spec = 1
+ deploy_mode = "IMAGE"
+ deploy_version = "1.9"
+ environment_id = "my-env-id"
+ 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.

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_workload.workload: Creating...
tencentcloud_tem_workload.workload: Creation complete after 6s [id=en-ejoqxm65#app-vpzq42ep]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
4. 执行完毕后,您可以在 弹性微服务控制台 应用列表中查看部署的应用。

销毁应用

您可以根据需求,执行以下命令销毁所创建和部署的资源。
terraform destroy
返回信息如下:
Terraform will perform the following actions:

# tencentcloud_tem_application.application will be destroyed
- resource "tencentcloud_tem_application" "application" {
- application_name = "demo" -> null
- coding_language = "JAVA" -> null
- description = "demo for terraform" -> null
- id = "my-app-id" -> null
- repo_name = "qcloud/nginx" -> null
- repo_server = "ccr.ccs.tencentyun.com" -> null
- repo_type = 2 -> null
- use_default_image_service = 0 -> null
}

# tencentcloud_tem_environment.environment will be destroyed
- resource "tencentcloud_tem_environment" "environment" {
- description = "demo for terraform" -> null
- environment_name = "test_tf" -> null
- id = "my-env-id" -> null
- subnet_ids = [
- "subnet-id1",
- "subnet-id2",
] -> null
- vpc = "vpc-id" -> null
}

# tencentcloud_tem_workload.workload will be destroyed
- resource "tencentcloud_tem_workload" "workload" {
- application_id = "my-app-id" -> null
- cpu_spec = 1 -> null
- deploy_mode = "IMAGE" -> null
- deploy_version = "1.9" -> null
- environment_id = "my-env-id" -> null
- id = "my-env-id#my-app-id" -> null
- img_repo = "qcloud/nginx" -> null
- init_pod_num = 1 -> null
- memory_spec = 1 -> null
- security_group_ids = [] -> null
}

Plan: 0 to add, 0 to change, 3 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_application.application: Destroying... [id=my-app-id]
tencentcloud_tem_workload.workload: Destroying... [id=my-env-id#my-app-id]
tencentcloud_tem_environment.environment: Destroying... [id=my-env-id]
tencentcloud_tem_workload.workload: Destruction complete after 3s
tencentcloud_tem_environment.environment: Destruction complete after 3s
tencentcloud_tem_application.application: Destruction complete after 4s

Destroy complete! Resources: 3 destroyed.