All Products
Search
Document Center

Application Real-Time Monitoring Service:Use Terraform to manage Prometheus instances

Last Updated:Aug 30, 2023

Terraform is an open source tool provided by HashiCorp. Terraform allows you to securely and efficiently preview, configure, and manage cloud infrastructures and resources. You can use Terraform to automatically create and update Alibaba Cloud infrastructures and resources, and manage versions based on your requirements. This topic describes how to use Terraform to manage Prometheus instances.

Prerequisites

  • Terraform is installed.

    Note

    You must install Terraform 0.12.28 or later. You can run the terraform --version command to query the Terraform version.

    • By default, Cloud Shell is preinstalled with Terraform and configured with your account information. You do not need to modify the configurations.

    • If you do not use Cloud Shell, you can directly install Terraform. For more information, see Install and configure Terraform in the local PC.

  • Your Alibaba Cloud account information is configured. You can use one of the following methods to configure Alibaba Cloud account information:

    Note

    To improve the flexibility and security of permission management, we recommend that you create a Resource Access Management (RAM) user named Terraform. Then, create an AccessKey pair for the RAM user and grant permissions to the RAM user. For more information, see Create a RAM user and Grant permissions to RAM users.

    • Method 1: Add environment variables to store authentication information.

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

      Specify the value of the export ALICLOUD_REGION parameter based on your business requirements.

    • Method 2: Specify identity information in the provider section of the configuration file.

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

      Specify the value of the export ALICLOUD_REGION parameter based on your business requirements.

Create a Prometheus instance

Create a Prometheus instance to monitor a Container Service for Kubernetes (ACK) cluster

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

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # The information about the Prometheus instance for Container Service. 
    resource "alicloud_arms_prometheus" "my_ack1" {
      cluster_id = "The ID of the ACK cluster."
      cluster_type = "aliyun-cs"
      grafana_instance_id = "free or the ID of the Grafana workspace."
    }
  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:

    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.
    ...
  4. Run the following command to create the Prometheus instance:

    terraform apply

    Expected 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 yes is returned, the Prometheus instance is created. You can log on to the Managed Service for Prometheus console and view the Prometheus instance on the Managed Service for Prometheus page. To view the Prometheus instance, perform the following steps:

    1. Log on to the ARMS console.
    2. In the left-side navigation pane, choose Prometheus Service > Prometheus Instances.
    3. On the Managed Service for Prometheus page, view the created Prometheus instance for Container Service.

Create a Prometheus instance to monitor an ACK Serverless cluster

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

    #provider, use alicloud
    provider "alicloud" {
      #access_key = "************"
      #secret_key = "************"
      #region     = "cn-beijing"
    }
    
    # The information about the Prometheus instance for Container Service. 
    resource "alicloud_arms_prometheus" "my_ack serverless1" {  cluster_id = "The ID of the ASK cluster."
      cluster_type = "aliyun-cs"
      grafana_instance_id = "free or the ID of the Grafana workspace."
      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."
    }
  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:

    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.
    ...
  4. Run the following command to create the Prometheus instance:

    terraform apply

    Expected 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 yes is returned, the Prometheus instance is created. You can log on to the Managed Service for Prometheus console and view the Prometheus instance on the Managed Service for Prometheus page. To view the Prometheus instance, perform the following steps:

    1. Log on to the ARMS console.
    2. In the left-side navigation pane, choose Prometheus Service > Prometheus Instances.
    3. On the Managed Service for Prometheus page, view the created Prometheus instance for Container Service.

Create a Prometheus instance for ECS

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

    #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 = "free or the ID of the Grafana workspace."
        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."
    }
  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:

    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.
    ...
  4. Run the following command to create the Prometheus instance:

    terraform apply

    Expected 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 yes is returned, the Prometheus instance is created. You can log on to the Managed Service for Prometheus console and view the Prometheus instance on the Managed Service for Prometheus page. To view the Prometheus instance, perform the following steps:

    1. Log on to the ARMS console.
    2. In the left-side navigation pane, choose Prometheus Service > Prometheus Instances.
    3. On the Managed Service for Prometheus page, view the created Prometheus instance for ECS.

Create a Prometheus instance for Remote Write

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

    #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."
    }
  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:

    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.
    ...
  4. Run the following command to create the Prometheus instance:

    terraform apply

    Expected 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 yes is returned, the Prometheus instance is created. You can log on to the Managed Service for Prometheus console and view the Prometheus instance on the Managed Service for Prometheus page. To view the Prometheus instance, perform the following steps:

    1. Log on to the ARMS console.
    2. In the left-side navigation pane, choose Prometheus Service > Prometheus Instances.
    3. On the Managed Service for Prometheus page, view the created Prometheus instance for Remote Write.

Create a global aggregation instance

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

    #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 = "free or the ID of the Grafana workspace."
      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."
         }
       ]
      )
    }
  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:

    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.
    ...
  4. Run the following command to create the Prometheus instance:

    terraform apply

    Expected 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 yes is returned, the Prometheus instance is created. You can log on to the Managed Service for Prometheus console and view the Prometheus instance on the Managed Service for Prometheus page. To view the Prometheus instance, perform the following steps:

    1. Log on to the ARMS console.
    2. In the left-side navigation pane, choose Prometheus Service > Prometheus Instances.
    3. On the Managed Service for Prometheus page, view the created global aggregation instance.

Delete a Prometheus instance

Procedure

You can run the following command to delete a Prometheus instance 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.

Check the result

  1. Log on to the ARMS console.
  2. In the left-side navigation pane, choose Prometheus Service > Prometheus Instances.
  3. If the Prometheus instance does not exist on the Managed Service for Prometheus page, the Prometheus instance is deleted.