All Products
Search
Document Center

Application Real-Time Monitoring Service:Use Terraform to manage the custom jobs of an environment instance

Last Updated:Jul 15, 2024

To increase monitoring efficiency and simplify relevant management, Managed Service for Prometheus allows you to create a monitoring job by using Terraform, and configure parameters about monitored objects, the path where the captured metrics are saved, and the interval at which metrics are captured. Then, you can deploy the job in a Container Service for Kubernetes (ACK) environment instance or an Elastic Compute Service (ECS) environment instance to implement automatic monitoring.

Prerequisites

An Container Service for Kubernetes (ACK) environment instance is created. For more information, see Use Terraform to manage an environment instance.

Limits

Only ACK environment instances and ECS environment instances are supported.

Create a custom job for the environment instance

  1. Create a working directory and create a configuration file named main.tf in the working directory to configure custom job resources.

    provider "alicloud" {
    # access_key = "************"
    # secret_key = "************"
    # region = "cn-beijing"
    }
    # Custom job settings of the environment instance. 
    resource "alicloud_arms_env_custom_job" "my-custom-job1" {
    	env_custom_job_name = "The name of the custom job. Example: my-custom-job1."
      environment_id = "The ID of the ACK or ECS environment instance. Example: env-xxxxx."
      config_yaml    = <<-EOT
          scrape_configs:
          - job_name: job-demo1
            honor_timestamps: false
            honor_labels: false
            scrape_interval: 30s
            scheme: http
            metrics_path: /metric
            static_configs:
            - targets:
              - 127.0.0.1:9090
          - job_name: job-demo2
            honor_timestamps: false
            honor_labels: false
            scrape_interval: 30s
            scheme: http
            metrics_path: /metric
            static_configs:
            - targets:
              - 127.0.0.1:9090
            http_sd_configs:
            - url: 127.0.0.1:9090
              refresh_interval: 30s
    	EOT
    }
  2. Run the following command to initialize the runtime environment for Terraform:

    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. Run the following command to create 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_env_custom_job.myCustomJob1 will be created
     + resource "alicloud_arms_env_custom_job" "myCustomJob1" {
    				env_custom_job_name = "myCustomJob1"
    				environment_id = "xxx"
    				config_yaml = ....
     }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
  4. Run the following command to create a 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_env_custom_job.myCustomJob1 will be created
     + resource "alicloud_arms_env_custom_job" "myCustomJob1" {
    				env_custom_job_name = "myCustomJob1"
    				environment_id = "xxx"
    				config_yaml = ....
     }
    
    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

    If Enter a value is returned, enter yes. The custom job of the current environment instance is created.

  5. Log on to the ARMS console.

  6. In the left-side navigation pane, click Integration Management. On the Integration Management page, click the name of the environment instance.

  7. On the environment instance details page, click the Metric Scraping tab, and then click the Customize Collection tab to check whether the custom job is created.

Delete the custom job from the ACK environment instance

  1. You can run the following command to delete an ACK managed cluster that is 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.

    If Enter a value is returned, enter yes.

  2. Log on to the ARMS console.

  3. In the left-side navigation pane, click Integration Management. On the Integration Management page, click the name of the environment instance.

  4. On the environment instance details page, click the Metric Scraping tab, and then click the Customize Collection tab to check whether the custom job is deleted.