All Products
Search
Document Center

Managed Service for Prometheus:Use Terraform to manage Prometheus instances

Last Updated:Jan 08, 2025

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

Prerequisites

  • Terraform is installed.

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

      Note
      • You must install Terraform V0.12.28 or later. You can run the terraform --version command to query the Terraform version.

      • While Resource Orchestration Service (ROS) is a native infrastructure-as-code (IaC) service provided by Alibaba Cloud, it also supports the integration of Terraform templates. By using Terraform with ROS, you can define and manage resources in Alibaba Cloud, Amazon Web Services (AWS), or Microsoft Azure, specify resource parameters, and configure dependency relationships for the resources. For more information, see Create a Terraform template and Create a Terraform stack.

  • 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 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 a RAM user.

    • 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 Instances page. To view the Prometheus instance, perform the following steps:

    1. Log on to the Managed Service for Prometheus console.

    2. In the left-side navigation pane, click Instances.

    3. On the Instances 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 ACK Serverless 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 Instances page. To view the Prometheus instance, perform the following steps:

    1. Log on to the Managed Service for Prometheus console.

    2. In the left-side navigation pane, click Instances.

    3. On the Instances 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 Instances page. To view the Prometheus instance, perform the following steps:

    1. Log on to the Managed Service for Prometheus console.

    2. In the left-side navigation pane, click Instances.

    3. On the Instances page, view the created Prometheus instance for ECS.

Create a general-purpose Prometheus 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 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 Instances page. To view the Prometheus instance, perform the following steps:

    1. Log on to the Managed Service for Prometheus console.

    2. In the left-side navigation pane, click Instances.

    3. On the Instances page, view the created general-purpose Prometheus instance.

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 Instances page. To view the Prometheus instance, perform the following steps:

    1. Log on to the Managed Service for Prometheus console.

    2. In the left-side navigation pane, click Instances.

    3. On the Instances 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 Managed Service for Prometheus console.

  2. In the left-side navigation pane, click Instances.

  3. If the Prometheus instance does not exist on the Instances page, the Prometheus instance is deleted.