All Products
Search
Document Center

Application Real-Time Monitoring Service:Manage PodMonitor configurations with Terraform

Last Updated:Mar 11, 2026

In Kubernetes, a PodMonitor defines which pods Prometheus scrapes for metrics. By managing PodMonitor resources through Terraform, you can version-control your monitoring configuration and let Managed Service for Prometheus automatically discover pods and collect metrics. This improves system stability and reliability.

This topic describes how to create and delete a PodMonitor configuration for a Container Service for Kubernetes (ACK) environment instance using the alicloud_arms_env_pod_monitor Terraform resource.

Prerequisites

Before you begin, make sure that you have:

Limits

Only ACK environment instances are supported.

Create a PodMonitor configuration

  1. Create a working directory and add a main.tf file with the following configuration: Replace the following placeholders with your values:

    PlaceholderDescriptionExample
    <environment-id>The ID of your ACK environment instanceenv-xxxxx
    <your-access-key>Your Alibaba Cloud AccessKey IDLTAI5tXxx
    <your-secret-key>Your Alibaba Cloud AccessKey secretxXxXxXx
    <region-id>The region IDcn-beijing

    Key YAML fields

    FieldDescription
    metadata.nameA unique name for the PodMonitor.
    metadata.namespaceThe namespace where the PodMonitor resides. Use arms-prom.
    arms.prometheus.io/discoverySet to 'true' to activate the PodMonitor, or 'false' to deactivate it.
    selector.matchLabelsLabels that identify the target pods.
    namespaceSelector.anySet to true to match pods in all namespaces.
    podMetricsEndpointsA list of scrape endpoints, each with interval, targetPort, and path.
       provider "alicloud" {
         # Set credentials through environment variables:
         #   export ALICLOUD_ACCESS_KEY="<your-access-key>"
         #   export ALICLOUD_SECRET_KEY="<your-secret-key>"
         #   export ALICLOUD_REGION="<region-id>"
       }
    
       resource "alicloud_arms_env_pod_monitor" "my-pod-monitor1" {
         environment_id = "<environment-id>"    # Example: env-xxxxx
         config_yaml    = <<-EOT
           apiVersion: monitoring.coreos.com/v1
           kind: PodMonitor
           metadata:
             name: my-pod-monitor1
             namespace: arms-prom
             annotations:
               arms.prometheus.io/discovery: 'true'    # Set to 'false' to disable this PodMonitor
           spec:
             selector:
               matchLabels:
                 app: xxx
                 release: yyy
             namespaceSelector:
               any: true                               # Match pods across all namespaces
             podMetricsEndpoints:
               - interval: 30s
                 targetPort: 9335
                 path: /metrics
               - interval: 10s
                 targetPort: 9335
                 path: /metrics1
         EOT
       }
  2. Initialize Terraform: Expected output:

       terraform init
       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.
  3. Preview the changes: Expected output:

       terraform plan
       Terraform will perform the following actions:
    
        # alicloud_arms_env_pod_monitor.my-pod-monitor1 will be created
        + resource "alicloud_arms_env_pod_monitor" "my-pod-monitor1" {
              environment_id = "env-xxxxx"
              config_yaml    = ...
        }
    
       Plan: 1 to add, 0 to change, 0 to destroy.
  4. Apply the configuration: When prompted with Enter a value, type yes to confirm. Terraform creates the PodMonitor configuration for your environment instance.

       terraform apply
  5. Verify in the ARMS console:

    1. Log on to the ARMS console.

    2. In the left-side navigation pane, click Integration Management.

    3. Click the name of your environment instance.

    4. Click the Metric Scraping tab, then click the Pod Monitor tab.

    5. Confirm that your PodMonitor configuration appears in the list.

Delete a PodMonitor configuration

  1. Destroy the PodMonitor resource: Expected output: When prompted with Enter a value, type yes to confirm.

       terraform 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
       ...
       Destroy complete! Resources: 1 destroyed.
  2. Verify in the ARMS console:

    1. Log on to the ARMS console.

    2. In the left-side navigation pane, click Integration Management.

    3. Click the name of your environment instance.

    4. Click the Metric Scraping tab, then click the Pod Monitor tab.

    5. Confirm that the PodMonitor configuration has been removed.

Related topics