Prerequisites
Installing Terraform
Note:
The installed version of Terraform must not be below v1.6.3. You can verify the version of Terraform installed by using the command terraform --version.
Configuring Tencent Cloud Account Information
Before using Terraform for the first time, go to API Key to apply for a SecretID and SecretKey of the security credentials. If you already have valid credentials, skip this step. 1. Log in to the CAM Console and choose Access Key > API Keys from the left sidebar. 2. On the API Keys page, click Create Key to create a SecretId/SecretKey pair.
There are two methods for configuring Tencent Cloud account information:
Authentication by Static Credentials
Create a provider.tf
file in the user directory with the following content. Replace my-secret-id
and my-secret-key
with your SecretId and SecretKey, respectively.
provider "tencentcloud" {
secret_id = "my-secret-id"
secret_key = "my-secret-key"
}
Authentication by Environment Variables
Configure the computer environment variables or cloud environment variables by running the following command. Replace YOUR_SECRET_ID
and YOUR_SECRET_KEY
with your SecretId and SecretKey, respectively.
export TENCENTCLOUD_SECRET_ID=YOUR_SECRET_ID
export TENCENTCLOUD_SECRET_KEY=YOUR_SECRET_KEY
Creating Prometheus Instances
1. Create the configuration file for Terraform. Create a file named main.tf in the project directory and open it with an appropriate editor. Add the Tencent Cloud provider configuration to the main.tf or provider.tf file.
provider "tencentcloud" {
region = "your_region"
secret_id = "your_secret_id"
secret_key = "your_secret_key"
}
2. Defining Tencent Cloud Resources: Use the resources provided by Tencent Cloud in the main.tf file to define the resources you want to create and manage.
#Specifying Provider Configuration Information
terraform {
required_providers {
tencentcloud = {
source = "tencentcloudstack/tencentcloud"
}
}
}
#Creating a Prometheus Instance in the Mumbai Region
resource "tencentcloud_monitor_tmp_instance" "foo" {
instance_name = "tf-tmp-instance-sjtest"
vpc_id = "vpc-0n42dxzs"
subnet_id = "subnet-es8rv1kx"
data_retention_time = 30
zone = "ap-mumbai-1"
tags = {
"createdBy" = "terraform"
}
}
#Creating a Grafana Instance in the Mumbai Region (Optional)
resource "tencentcloud_monitor_grafana_instance" "foo" {
instance_name = "tf-grfana-cstest"
vpc_id = "vpc-0n42dxzs"
subnet_ids = ["subnet-es8rv1kx"]
grafana_init_password = "1234567890"
enable_internet = false
is_destroy = true
tags = {
"createdBy" = "test"
}
}
#Binding Grafana and Prometheus Instances (Optional)
resource "tencentcloud_monitor_tmp_manage_grafana_attachment" "foo" {
grafana_id = tencentcloud_monitor_grafana_instance.foo.id
instance_id = tencentcloud_monitor_tmp_instance.foo.id
}
3. Initialize the Terraform runtime environment by running the following command:
Expected output information:
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of tencentcloudstack/tencentcloud from the dependency lock file
- Using previously-installed tencentcloudstack/tencentcloud v1.81.32
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. To generate resource planning, run the following command:
Expected output information:
tencentcloud_vpc.vpc: Refreshing state... [id=vpc-3csjp7k8]
tencentcloud_monitor_tmp_instance.foo: Refreshing state... [id=prom-4wcvt7p1]
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_monitor_tmp_instance.foo will be created
+ resource "tencentcloud_monitor_tmp_instance" "foo" {
+ api_root_path = (known after apply)
+ data_retention_time = 30
+ id = (known after apply)
+ instance_name = "tf-tmp-instance-sjtest"
+ ipv4_address = (known after apply)
+ proxy_address = (known after apply)
+ remote_write = (known after apply)
+ subnet_id = "subnet-es8rv1kx"
+ tags = {
+ "createdBy" = "terraform"
}
+ vpc_id = "vpc-0n42dxzs"
+ zone = "ap-mumbai-1"
}
Plan: 1 to add, 0 to change, 0 to destroy.
5. To create an instance, run the following command:
Expected output information:
tencentcloud_vpc.vpc: Refreshing state... [id=vpc-3csjp7k8]
tencentcloud_monitor_tmp_instance.foo: Refreshing state... [id=prom-4wcvt7p1]
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_monitor_tmp_instance.foo will be created
+ resource "tencentcloud_monitor_tmp_instance" "foo" {
+ api_root_path = (known after apply)
+ data_retention_time = 30
+ id = (known after apply)
+ instance_name = "tf-tmp-instance-sjtest"
+ ipv4_address = (known after apply)
+ proxy_address = (known after apply)
+ remote_write = (known after apply)
+ subnet_id = "subnet-es8rv1kx"
+ tags = {
+ "createdBy" = "terraform"
}
+ vpc_id = "vpc-0n42dxzs"
+ zone = "ap-mumbai-1"
}
Plan: 1 to add, 0 to change, 0 to destroy.
Enter "yes" to the following information to proceed:
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value:
If the following information appear, the creation is successful:
tencentcloud_monitor_tmp_instance.foo: Creating...
tencentcloud_monitor_tmp_instance.foo: Still creating... [10s elapsed]
tencentcloud_monitor_tmp_instance.foo: Creation complete after 12s [id=prom-8dyb6iny]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Viewing the Status of Prometheus Instances
Log in to the TCOP, and choose Managed Service for Prometheus from the left-hand navigation bar. Existing instances are displayed in the Prometheus instance list. Deleting Prometheus Instances
To delete a Prometheus instance, run the following command:
If the following information appears, you are prompted to enter "yes" for confirmation:
tencentcloud_monitor_tmp_instance.foo: Refreshing state... [id=prom-8dyb6iny]
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_monitor_tmp_instance.foo will be destroyed
- resource "tencentcloud_monitor_tmp_instance" "foo" {
- api_root_path = "http://10.0.0.34:9090/api/v1" -> null
- data_retention_time = 30 -> null
- id = "prom-8dyb6iny" -> null
- instance_name = "tf-tmp-instance-sjtest" -> null
- ipv4_address = "10.0.0.34" -> null
- proxy_address = "10.0.0.34:9090" -> null
- remote_write = "http://10.0.0.34:9090/api/v1/prom/write" -> null
- subnet_id = "subnet-es8rv1kx" -> null
- tags = {
- "createdBy" = "terraform"
} -> null
- vpc_id = "vpc-0n42dxzs" -> null
- zone = "ap-mumbai-1" -> 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_monitor_tmp_instance.foo: Destroying... [id=prom-8dyb6iny]
tencentcloud_monitor_tmp_instance.foo: Destruction complete after 6s
Destroy complete! Resources: 1 destroyed.
Note:
When Destroy complete! Resources: 1 destroyed.
is displayed, the instance has been successfully deleted.
Was this page helpful?