Use Terraform to manage Prometheus instances
Terraform is an open-source infrastructure as code (IaC) tool by HashiCorp. You can use Terraform to create, update, and delete Prometheus instances on Alibaba Cloud.
Prerequisites
Terraform is installed.
Cloud Shell has Terraform preconfigured with your Alibaba Cloud credentials. No additional setup is required.
If you do not use Cloud Shell, install Terraform manually. For more information, see Install and configure Terraform.
NoteYou must install Terraform V0.12.28 or later. Run the
terraform --versioncommand to query the Terraform version.Resource Orchestration Service provides managed Terraform capabilities. You can create Terraform templates to define resources in Alibaba Cloud, AWS, or Azure, configure resource parameters, and set dependencies between resources. For more information, see Create a Terraform template and Create a Terraform stack.
Alibaba Cloud credentials are configured by using one of the following methods:
NoteFor better security, create a RAM user named Terraform, create an AccessKey pair for the RAM user, and grant permissions.
Method 1: Set environment variables.
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-beijing"NoteSet
export ALICLOUD_REGIONto your target region.Method 2: Add credentials to the provider block in the configuration file.
provider "alicloud" { access_key = "************" secret_key = "************" region = "cn-beijing" }NoteSet
export ALICLOUD_REGIONto your target region.
Create a Prometheus instance
Create a Prometheus instance to monitor a Container Service for Kubernetes (ACK) cluster
Create a working directory with a file named main.tf.
#provider, use alicloud provider "alicloud" { #access_key = "************" #secret_key = "************" #region = "cn-beijing" } # The information about the Prometheus instance for the ACK cluster. resource "alicloud_arms_prometheus" "my_ack1" { cluster_id = "The ID of the ACK cluster." cluster_type = "aliyun-cs" grafana_instance_id = "The free or Grafana workspace ID." }Initialize the Terraform runtime environment:
terraform initExpected output:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... 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.Create an execution plan:
terraform planExpected output:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 5 to add, 0 to change, 0 to destroy. ...Create the Prometheus instance:
terraform applyExpected output:
... 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 ... alicloud_arms_prometheus: Creation complete after 8m26s [id=************] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.If
yesis returned, the Prometheus instance is created. Log on to the Managed Service for Prometheus console. On the Instances page, verify the instance:-
Log on to the Cloud Monitor console.
-
In the navigation pane on the left, choose to open the instance list for Managed Service for Prometheus.
On the Instances page, verify the ACK cluster Prometheus instance.
-
Create a Prometheus instance to monitor an ACK Serverless cluster
Create a working directory with a file named main.tf.
#provider, use alicloud provider "alicloud" { #access_key = "************" #secret_key = "************" #region = "cn-beijing" } # The information about the Prometheus instance for the ACK Serverless cluster. resource "alicloud_arms_prometheus" "my_ack serverless1" { cluster_id = "The ID of the ACK Serverless cluster." cluster_type = "aliyun-cs" grafana_instance_id = "The free or Grafana workspace ID." vpc_id = "The ID of the VPC where the ACK Serverless cluster resides." vswitch_id = "The ID of the vSwitch corresponding to the ACK Serverless cluster." security_group_id = "The ID of the security group corresponding to the ACK Serverless cluster." }Initialize the Terraform runtime environment:
terraform initExpected output:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... 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.Create an execution plan:
terraform planExpected output:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 5 to add, 0 to change, 0 to destroy. ...Create the Prometheus instance:
terraform applyExpected output:
... 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 ... alicloud_arms_prometheus: Creation complete after 8m26s [id=************] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.If
yesis returned, the Prometheus instance is created. Log on to the Managed Service for Prometheus console. On the Instances page, verify the instance:-
Log on to the Cloud Monitor console.
-
In the navigation pane on the left, choose to open the instance list for Managed Service for Prometheus.
On the Instances page, verify the ACK Serverless cluster Prometheus instance.
-
Create a Prometheus instance for Elastic Compute Service (ECS)
Create a working directory with a file named main.tf.
#provider, use alicloud provider "alicloud" { #access_key = "************" #secret_key = "************" #region = "cn-beijing" } # The information about the Prometheus instance for ECS. resource "alicloud_arms_prometheus" "my_vpc1" { cluster_type = "ecs" cluster_name = "The name of the Prometheus instance." # Format: name-vpcId. Example: my_vpc1-vpc-xxxx.ss. grafana_instance_id = "The free or Grafana workspace ID." vpc_id = "The ID of the VPC where the ECS instance resides." vswitch_id = "The ID of the vSwitch corresponding to the ECS instance." security_group_id = "The ID of the security group corresponding to the ECS instance." }Initialize the Terraform runtime environment:
terraform initExpected output:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... 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.Create an execution plan:
terraform planExpected output:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 5 to add, 0 to change, 0 to destroy. ...Create the Prometheus instance:
terraform applyExpected output:
... 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 ... alicloud_arms_prometheus: Creation complete after 8m26s [id=************] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.If
yesis returned, the Prometheus instance is created. Log on to the Managed Service for Prometheus console. On the Instances page, verify the instance:-
Log on to the Cloud Monitor console.
-
In the navigation pane on the left, choose to open the instance list for Managed Service for Prometheus.
On the Instances page, verify the ECS Prometheus instance.
-
Create a general-purpose Prometheus instance
Create a working directory with a file named main.tf.
#provider, use alicloud provider "alicloud" { #access_key = "************" #secret_key = "************" #region = "cn-beijing" } # The information about the Prometheus instance for Remote Write. resource "alicloud_arms_prometheus" "my_rw1" { cluster_name = "The name of the Prometheus instance." cluster_type = "remote-write" grafana_instance_id = "free or the ID of the Grafana workspace." }Initialize the Terraform runtime environment:
terraform initExpected output:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... 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.Create an execution plan:
terraform planExpected output:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 5 to add, 0 to change, 0 to destroy. ...Create the Prometheus instance:
terraform applyExpected output:
... 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 ... alicloud_arms_prometheus: Creation complete after 8m26s [id=************] Apply complete! Resources: 4 added, 0 changed, 0 destroyed.If
yesis returned, the Prometheus instance is created. Log on to the Managed Service for Prometheus console. On the Instances page, verify the instance:-
Log on to the Cloud Monitor console.
-
In the navigation pane on the left, choose to open the instance list for Managed Service for Prometheus.
On the Instances page, verify the general-purpose Prometheus instance.
-
Create a global aggregation instance
Create a working directory with a file named main.tf.
#provider, use alicloud provider "alicloud" { #access_key = "************" #secret_key = "************" #region = "cn-beijing" } # The information about the global aggregation instance. resource "alicloud_arms_prometheus" "my_global1" { cluster_name = "The name of the Prometheus instance." cluster_type = "global-view" grafana_instance_id = "The free or Grafana workspace ID." subClustersJson = jsonencode( #"The Prometheus instances whose data you want to aggregate. The value must be a JSON string." [ { "headers": {}, "regionId": "The ID of the region. Example: cn-hangzhou.", "sourceType": "AlibabaPrometheus", "extras": {}, "clusterId": "The ID of the Prometheus instance.", "sourceName": "The name of the Prometheus instance.", "dataSource": "", "userId": "The user ID." }, { "headers": {}, "regionId": "The ID of the region. Example: cn-hangzhou.", "sourceType": "AlibabaPrometheus", "extras": {}, "clusterId": "The ID of the Prometheus instance.", "sourceName": "The name of the Prometheus instance.", "dataSource": "", "userId": "The user ID." } ] ) }Initialize the Terraform runtime environment:
terraform initExpected output:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.90.1... ... 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.Create an execution plan:
terraform planExpected output:
Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ... Plan: 5 to add, 0 to change, 0 to destroy. ...Create the Prometheus instance:
terraform applyExpected output:
... 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 ... alicloud_arms_prometheus: Creation complete after 8m26s [id=************] Apply complete! Resources: 5 added, 0 changed, 0 destroyed.If
yesis returned, the Prometheus instance is created. Log on to the Managed Service for Prometheus console. On the Instances page, verify the instance:-
Log on to the Cloud Monitor console.
-
In the navigation pane on the left, choose to open the instance list for Managed Service for Prometheus.
On the Instances page, verify the global aggregation instance.
-
Delete a Prometheus instance
Procedure
Run the following command to delete a Terraform-managed Prometheus instance:
terraform destroyExpected output:
...
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
...
Destroy complete! Resources: 1 destroyed.Check the result
-
Log on to the Cloud Monitor console.
-
In the navigation pane on the left, choose to open the instance list for Managed Service for Prometheus.
If the Prometheus instance does not exist on the Instances page, the deletion succeeded.