All Products
Search
Document Center

Managed Service for Prometheus:Use Terraform to manage Prometheus monitoring

Last Updated:Sep 30, 2026

Terraform lets you manage monitoring configurations for Prometheus instances, including ServiceMonitors, PodMonitors, custom jobs, and health check probes.

Prerequisites

  • A Prometheus instance for Container Service for Kubernetes (ACK) or for ECS has been created. For more information, see Use Terraform to manage a Prometheus instance.

  • Terraform is installed.

    Note
    • Make sure that your Terraform version is v0.12.28 or later. Run the terraform --version command to check.

    • Resource Orchestration Service provides a managed service for Terraform. You can use Terraform templates to define resources on Alibaba Cloud, AWS, or Azure, and configure resource parameters and dependencies. For more information, see Create a Terraform template and Create a Terraform stack.

  • Configure your Alibaba Cloud account credentials by using one of the following methods:

    Note

    For better security and permission management, create a RAM user named Terraform, create an AccessKey pair for the user, and grant the required permissions. For more information, see Create a RAM user and Manage the permissions of a RAM user.

    • Method 1: Create environment variables to store your credentials.

      export ALICLOUD_ACCESS_KEY="************"
      export ALICLOUD_SECRET_KEY="************"
      export ALICLOUD_REGION="cn-beijing"
      Note

      Replace the value of the ALICLOUD_REGION parameter with your actual region ID.

    • Method 2: Specify your credentials in the provider block of the configuration file.

      provider "alicloud" {
        access_key = "************"
        secret_key = "************"
        region     = "cn-beijing"
      }
      Note

      Replace the value of the region parameter with your actual region ID.

Limitations

  • Prometheus instances for Container Service for Kubernetes (ACK) support ServiceMonitors, PodMonitors, custom jobs, and health check probes.

  • Prometheus instances for ECS support only custom jobs and health check probes.

  • Health check probe:

    • The Status parameter is not supported.

    • The probe must be named in the following format: custom-name-{tcp/http/ping}-blackbox. For example, a TCP-type probe can be named xxx-tcp-blackbox.

    • For an ECS-based Prometheus instance (fully managed), the probe namespace must be empty or set to the fixed value vpcId-userId. Example: vpc-0jl4q1q2of2tagvwxxxx-11032353609xxxx.

Add a monitoring configuration

Add a ServiceMonitor

  1. Create a working directory and a configuration file named main.tf in the directory.

    provider "alicloud" {
    }
  2. Run the following command to initialize the Terraform environment:

    terraform init

    Expected 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.
  3. Define and apply the monitoring resource.

    1. Add the monitoring resource to the main.tf file.

      # ServiceMonitor configuration for the Prometheus instance.
      resource "alicloud_arms_prometheus_monitoring" "myServiceMonitor1" {
        cluster_id       = "c77e1106f429e4b46b0ee1720cxxxxx"   # The ID of the Prometheus instance.
        status      = "run"   # The status of the ServiceMonitor.
        type        = "serviceMonitor" 
        config_yaml = <<-EOT
      					apiVersion: monitoring.coreos.com/v1
      					kind: ServiceMonitor
      					metadata:
      					  name: tomcat-demo    # The name of the ServiceMonitor.
      					  namespace: default   # The namespace where the ServiceMonitor resides.
      					spec:
      					  endpoints:
      					    - interval: 30s    # The metrics scrape interval in seconds.
      					      path: /metrics   # The metrics scrape path.
      					      port: tomcat-monitor   # The name of the metrics scrape port.
      					  namespaceSelector:
      					    any: true           # The service namespace selector.
      					  selector:
      					    matchLabels:
      					      app: tomcat       # The service label selector.
      			EOT
      }
    2. Run the following command to generate an execution plan:

      terraform plan

      Expected output:

      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:
        # alicloud_arms_prometheus_monitoring.myServiceMonitor1 will be created
        + resource "alicloud_arms_prometheus_monitoring" "myServiceMonitor1" {
            + cluster_id        = "c77e1106f429e4b46b0ee1720cxxxxx"
            + id                = (known after apply)
            + monitoring_name = (known after apply)
        		+ status      = "run"   
       			+ type        = "serviceMonitor"
      			+ config_yaml = <<-EOT
      						apiVersion: monitoring.coreos.com/v1
      						kind: ServiceMonitor
      						metadata:
      						  name: tomcat-demo
      						  namespace: default
      						spec:
      						  endpoints:
      						    - interval: 30s
      						      path: /metrics
      						      port: tomcat-monitor
      						  namespaceSelector:
      						    any: true
      						  selector:
      						    matchLabels:
      						      app: tomcat
              EOT
          }
      Plan: 1 to add, 0 to change, 0 to destroy.
    3. Run the following command to create the ServiceMonitor:

      terraform apply

      Expected output:

      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:
        # alicloud_arms_prometheus_monitoring.myServiceMonitor1 will be created
        + resource "alicloud_arms_prometheus_monitoring" "myServiceMonitor1" {
            + cluster_id        = "c77e1106f429e4b46b0ee1720c9xxxxx"
            + id                = (known after apply)
            + monitoring_name = (known after apply)
        		+ status      = "run"   
       			+ type        = "serviceMonitor"
      			+ config_yaml = <<-EOT
      						apiVersion: monitoring.coreos.com/v1
      						kind: ServiceMonitor
      						metadata:
      						  name: tomcat-demo
      						  namespace: default
      						spec:
      						  endpoints:
      						    - interval: 30s
      						      path: /metrics
      						      port: tomcat-monitor
      						  namespaceSelector:
      						    any: true
      						  selector:
      						    matchLabels:
      						      app: tomcat
              EOT
          }
      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

      When prompted, enter yes to confirm. This action creates the ServiceMonitor configuration for the Prometheus instance.

Verify the result

To view the ServiceMonitor you created, log on to the Managed Service for Prometheus console and navigate to the Integration Center page of your Prometheus instance:

  1. Log on to the Cloud Monitor console.

  2. In the navigation pane on the left, choose Managed Service for Prometheus > Instances to open the instance list for Managed Service for Prometheus.

  3. Click the name of the Managed Service for Prometheus instance instance that you want to manage to go to the Integration Center page.

  4. Click the custom component card in the Installed section. In the panel that appears, click the Service Discovery Configurations tab to view the ServiceMonitor configuration.

    The tomcat-demo configuration appears in the list with type serviceMonitor. An enabled switch indicates the ServiceMonitor is active.

Add a PodMonitor

  1. Create a working directory and a configuration file named main.tf in the directory.

    provider "alicloud" {
    }
  2. Run the following command to initialize the Terraform environment:

    terraform init

    Expected 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.
  3. Define and apply the monitoring resource.

    1. Add the monitoring resource to the main.tf file.

      # PodMonitor configuration for the Prometheus instance.
      resource "alicloud_arms_prometheus_monitoring" "myPodMonitor1" {
        cluster_id       = "c77e1106f429e4b46b0ee1720cxxxxx"  # The ID of the Prometheus instance.
        status      = "run"  # The status of the PodMonitor.
        type        = "podMonitor" 
        config_yaml = <<-EOT
      			apiVersion: "monitoring.coreos.com/v1"
      			kind: "PodMonitor"
      			metadata:
      			  name: "podmonitor-demo"  # The name of the PodMonitor.
      			  namespace: "default"     # The namespace where the PodMonitor resides.
      			spec:
      			  namespaceSelector:
      			    any: true              # The pod namespace selector.
      			  podMetricsEndpoints:
      			    - interval: "30s"      # The metrics scrape interval in seconds.
      			      path: "/metrics"     # The metrics scrape path.
      			      port: "tomcat-monitor"  # The name of the metrics scrape port.
      			  selector:
      			    matchLabels:
      			      app: "nginx2-exporter"   # The pod label selector.
      			EOT
      }
    2. Run the following command to generate an execution plan:

      terraform plan

      Expected output:

      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:
        # alicloud_arms_prometheus_monitoring.myPodMonitor1 will be created
        + resource "alicloud_arms_prometheus_monitoring" "myPodMonitor1" {
            + cluster_id        = "c77e1106f429e4b46b0ee1720cxxxxx"
            + id                = (known after apply)
            + monitoring_name = (known after apply)
        		+ status      = "run"   
       			+ type        = "podMonitor"
      			+ config_yaml = <<-EOT
      						apiVersion: "monitoring.coreos.com/v1"
      						kind: "PodMonitor"
      						metadata:
      						  name: "podmonitor-demo"
      						  namespace: "default"
      						spec:
      						  namespaceSelector:
      						    any: true
      						  podMetricsEndpoints:
      						    - interval: "30s"
      						      path: "/metrics"
      						      port: "tomcat-monitor"
      						  selector:
      						    matchLabels:
      						      app: "nginx2-exporter"
              EOT
          }
      Plan: 1 to add, 0 to change, 0 to destroy.
    3. Run the following command to create the PodMonitor:

      terraform apply

      Expected output:

      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:
        # alicloud_arms_prometheus_monitoring.myPodMonitor1 will be created
        + resource "alicloud_arms_prometheus_monitoring" "myPodMonitor1" {
            + cluster_id        = "c77e1106f429e4b46b0ee1720c9xxxxx"
            + id                = (known after apply)
            + monitoring_name = (known after apply)
        		+ status      = "run"   
       			+ type        = "podMonitor"
      			+ config_yaml = <<-EOT
      						apiVersion: "monitoring.coreos.com/v1"
      						kind: "PodMonitor"
      						metadata:
      						  name: "podmonitor-demo"
      						  namespace: "default"
      						spec:
      						  namespaceSelector:
      						    any: true
      						  podMetricsEndpoints:
      						    - interval: "30s"
      						      path: "/metrics"
      						      port: "tomcat-monitor"
      						  selector:
      						    matchLabels:
      						      app: "nginx2-exporter"
              EOT
          }
      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

      When prompted, enter yes to confirm. This action creates the PodMonitor configuration for the Prometheus instance.

Verify the result

To view the PodMonitor you created, log on to the Managed Service for Prometheus console and navigate to the Integration Center page of your Prometheus instance:

  1. Log on to the Cloud Monitor console.

  2. In the navigation pane on the left, choose Managed Service for Prometheus > Instances to open the instance list for Managed Service for Prometheus.

  3. Click the name of the Managed Service for Prometheus instance instance that you want to manage to go to the Integration Center page.

  4. Click the custom component card in the Installed section. In the panel that appears, click the Service Discovery Configurations tab to view the PodMonitor configuration.

    The podmonitor-demo configuration appears in the list with type podMonitor. A green switch indicates the configuration is active.

Add a custom job

  1. Create a working directory and a configuration file named main.tf in the directory.

    provider "alicloud" {
    }
  2. Run the following command to initialize the Terraform environment:

    terraform init

    Expected 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.
  3. Define and apply the monitoring resource.

    1. Add the monitoring resource to the main.tf file.

      # Custom job configuration for the Prometheus instance.
      resource "alicloud_arms_prometheus_monitoring" "myCustomJob1" {
        cluster_id       = "c77e1106f429e4b46b0ee1720cxxxxx"  # The ID of the Prometheus instance.
        status      = "run"     # The status of the custom job.
        type        = "customJob" 
        config_yaml = <<-EOT
      			scrape_configs:
      			  - job_name: prometheus1    # The name of the custom job.
      			    honor_timestamps: false
      			    honor_labels: false
      			    scheme: http
      			    metrics_path: /metric
      			    static_configs:					
      			      - targets:
      			          - 127.0.0.1:9090
      			EOT
      }
    2. Run the following command to generate an execution plan:

      terraform plan

      Expected output:

      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:
        # alicloud_arms_prometheus_monitoring.myCustomJob1 will be created
        + resource "alicloud_arms_prometheus_monitoring" "myCustomJob1" {
            + cluster_id        = "c77e1106f429e4b46b0ee1720cxxxxx"
            + id                = (known after apply)
            + monitoring_name = (known after apply)
        		+ status      = "run"   
       			+ type        = "customJob"
      			+ config_yaml = <<-EOT
      						scrape_configs:
      						  - job_name: prometheus1		 
      						    honor_timestamps: false
      						    honor_labels: false
      						    scheme: http
      						    metrics_path: /metric
      						    static_configs:					
      						      - targets:
      						          - 127.0.0.1:9090
              EOT
          }
      Plan: 1 to add, 0 to change, 0 to destroy.
    3. Run the following command to create the custom job:

      terraform apply

      Expected output:

      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:
        # alicloud_arms_prometheus_monitoring.myCustomJob1 will be created
        + resource "alicloud_arms_prometheus_monitoring" "myCustomJob1" {
            + cluster_id        = "c77e1106f429e4b46b0ee1720c9xxxxx"
            + id                = (known after apply)
            + monitoring_name = (known after apply)
        		+ status      = "run"   
       			+ type        = "customJob"
      			+ config_yaml = <<-EOT
      						scrape_configs:
      						  - job_name: prometheus1		 
      						    honor_timestamps: false
      						    honor_labels: false
      						    scheme: http
      						    metrics_path: /metric
      						    static_configs:					
      						      - targets:
      						          - 127.0.0.1:9090
              EOT
          }
      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

      When prompted, enter yes to confirm. This action creates the custom job configuration for the Prometheus instance.

Verify the result

To view the custom job you created, log on to the Managed Service for Prometheus console and navigate to the Integration Center page of your Prometheus instance:

  1. Log on to the Cloud Monitor console.

  2. In the navigation pane on the left, choose Managed Service for Prometheus > Instances to open the instance list for Managed Service for Prometheus.

  3. Click the name of the Managed Service for Prometheus instance instance that you want to manage to go to the Integration Center page.

  4. Click the custom component card in the Installed section. In the panel that appears, click the Service Discovery Configurations tab to view the custom job configuration.

    The prometheus1 custom job appears in the list with type custom and an enabled switch. The configuration shows job_name: prometheus1, metrics_path: /metric, and target 127.0.0.1:9090.

Add a health check probe

  1. Create a working directory and a configuration file named main.tf in the directory.

    provider "alicloud" {
    }
  2. Run the following command to initialize the Terraform environment:

    terraform init

    Expected 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.
  3. Define and apply the monitoring resource.

    1. Add the monitoring resource to the main.tf file.

      # Probe configuration for the Prometheus instance.
      resource "alicloud_arms_prometheus_monitoring" "myProbe1" {
        cluster_id       = "c77e1106f429e4b46b0ee1720cxxxxx"  # The ID of the Prometheus instance.
        type        = "probe" 
        config_yaml = <<-EOT
      			apiVersion: monitoring.coreos.com/v1
      			kind: Probe
      			metadata:
      				name: name1-tcp-blackbox  # The name of the health check. Format: xxx-{tcp/http/ping}-blackbox
      				namespace: arms-prom # Optional
      			spec:
      				interval: 30s         # The health check interval.
      				jobName: blackbox     # Fixed value.
      				module: tcp_connect
      				prober:               # The prober configuration. Fixed value.
      					path: /blackbox/probe
      					scheme: http
      					url: 'localhost:9335'
      				targets:
      					staticConfig:
      						static:
      							- 'arms-prom-admin.arms-prom:9335'  # The target address for the health check.
      			EOT
      }
    2. Run the following command to generate an execution plan:

      terraform plan

      Expected output:

      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:
        # alicloud_arms_prometheus_monitoring.myProbe1 will be created
        + resource "alicloud_arms_prometheus_monitoring" "myProbe1" {
            + cluster_id        = "c77e1106f429e4b46b0ee1720cxxxxx"
            + id                = (known after apply)
            + monitoring_name = (known after apply)
        		+ type        = "probe"
      			+ config_yaml = <<-EOT
      						apiVersion: monitoring.coreos.com/v1
      						kind: Probe
      						metadata:
      						  name: name1-tcp-blackbox  
      						  namespace: arms-prom  
      						spec:
      						  interval: 30s
      						  jobName: blackbox
      						  module: tcp_connect
      						  prober:
      						    path: /blackbox/probe
      						    scheme: http
      						    url: 'localhost:9335'
      						  targets:
      						    staticConfig:
      						      static:
      						        - 'arms-prom-admin.arms-prom:9335'
              EOT
          }
      Plan: 1 to add, 0 to change, 0 to destroy.
    3. Run the following command to create the health check probe:

      terraform apply

      Expected output:

      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:
        # alicloud_arms_prometheus_monitoring.myProbe1 will be created
        + resource "alicloud_arms_prometheus_monitoring" "myProbe1" {
            + cluster_id        = "c77e1106f429e4b46b0ee1720c9xxxxx"
            + id                = (known after apply)
            + monitoring_name = (known after apply)
        		+ type        = "probe"
      			+ config_yaml = <<-EOT
      						apiVersion: monitoring.coreos.com/v1
      						kind: Probe
      						metadata:
      						  name: name1-tcp-blackbox  
      						  namespace: arms-prom  
      						spec:
      						  interval: 30s
      						  jobName: blackbox
      						  module: tcp_connect
      						  prober:
      						    path: /blackbox/probe
      						    scheme: http
      						    url: 'localhost:9335'
      						  targets:
      						    staticConfig:
      						      static:
      						        - 'arms-prom-admin.arms-prom:9335'
              EOT
          }
      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

      When prompted, enter yes to confirm. This action creates the health check probe configuration for the Prometheus instance.

Verify the result

To view the health check probe you created, log on to the Managed Service for Prometheus console and navigate to the Integration Center page of your Prometheus instance:

  1. Log on to the Cloud Monitor console.

  2. In the navigation pane on the left, choose Managed Service for Prometheus > Instances to open the instance list for Managed Service for Prometheus.

  3. Click the name of the Managed Service for Prometheus instance instance that you want to manage to go to the Integration Center page.

  4. Click the Health Check component card in the Installed section, and then click the Health Check tab to view the health check probe configuration that you created.

    The probe configuration appears in the list. A green check mark in the Status column indicates the health check is running.

Delete the monitoring configuration

Procedure

Run the following command to delete the resources that you created by using Terraform:

terraform destroy

Expected 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.

Verify the result

To verify the deletion, log on to the Managed Service for Prometheus console and navigate to the Integration Center page of your Prometheus instance.

  1. Log on to the Cloud Monitor console.

  2. In the navigation pane on the left, choose Managed Service for Prometheus > Instances to open the instance list for Managed Service for Prometheus.

  3. Click the name of the Managed Service for Prometheus instance instance that you want to manage to go to the Integration Center page.

  4. Click the custom or Blackbox component card in the Installed section. On the Service Discovery Configurations or Health Check tab, verify that the target monitoring configuration no longer appears.