Terraform enables you to create and delete remote write configurations for Managed Service for Prometheus instances by using the alicloud_arms_remote_write resource.
Prerequisites
Before you begin, make sure that you have:
- A Prometheus instance for Container Service or ECS. For more information, see Use Terraform to manage Prometheus instances.
- Terraform V0.12.28 or later installed. Run
terraform --versionto check your version.- Cloud Shell comes with Terraform preinstalled and configured with your account information.
- To install Terraform locally, see Install and configure Terraform.
Note Resource Orchestration Service (ROS) is a native infrastructure-as-code (IaC) service provided by Alibaba Cloud that also supports Terraform templates. With Terraform and 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 more information, see Create a Terraform template and Create a Terraform stack. - Your Alibaba Cloud account information configured by using one of the following methods:
Note For better flexibility and security, create a Resource Access Management (RAM) user named Terraform, then create an AccessKey pair for the RAM user and grant the required permissions. For more information, see Create a RAM user and Grant permissions to a RAM user.
- Method 1: Environment variables
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-beijing"Note SetALICLOUD_REGIONbased on your business requirements. - Method 2: Provider block in the configuration file
provider "alicloud" { access_key = "************" secret_key = "************" region = "cn-beijing" }Note Set theregionvalue based on your business requirements.
- Method 1: Environment variables
Create a remote write configuration
- Create a working directory and a file named
main.tfwith the following content:provider "alicloud" { } - Initialize the Terraform runtime environment:
terraform initExpected 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. - Add the remote write resource to
main.tf:# Remote write configuration for the Prometheus instance resource "alicloud_arms_remote_write" "myRemoteWrite1" { cluster_id = "c77e1106f429e4b46b0ee1720c*****" # The ID of the Prometheus instance remote_write_yaml = <<-EOT remote_write: - name: ArmsRemoteWrite1 url: http://47.96.XX.XX:8080/prometheus/xxx/yyy/cn-hangzhou/api/v3/write basic_auth: {username: 666, password: '******'} write_relabel_configs: - source_labels: [instance_id] separator: ; regex: si-6e2ca86444db4e5***** replacement: $1 action: keep EOTThe following table describes the parameters:
Parameter Description cluster_idThe ID of the Prometheus instance. remote_write_yamlThe YAML block that defines the remote write configuration. nameThe name of the remote write configuration. urlThe remote write endpoint URL. basic_authThe authentication credentials ( usernameandpassword).write_relabel_configsRelabeling rules that filter metrics before sending. Includes source_labels,separator,regex,replacement, andaction. - Preview the changes:
terraform planExpected 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_remote_write.ack-rw1 will be created + resource "alicloud_arms_remote_write" "ack-rw1" { + cluster_id = "c77e1106f429e4b46b0ee1720c*****" + id = (known after apply) + remote_write_name = (known after apply) + remote_write_yaml = <<-EOT remote_write: - name: ArmsRemoteWrite1 url: http://xx.xx.xx.xx:8080/prometheus/xxx/yyy/cn-hangzhou/api/v3/write basic_auth: {username: 666, password: '******'} write_relabel_configs: - source_labels: [instance_id] separator: ; regex: si-6e2ca86444db4e5***** replacement: $1 action: keep EOT } Plan: 1 to add, 0 to change, 0 to destroy. - Apply the configuration:
terraform applyExpected 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_remote_write.ack-rw1 will be created + resource "alicloud_arms_remote_write" "ack-rw1" { + cluster_id = "c77e1106f429e4b46b0ee1720c9*****" + id = (known after apply) + remote_write_name = (known after apply) + remote_write_yaml = <<-EOT remote_write: - name: ArmsRemoteWrite1 url: http://xx.xx.xx.xx:8080/prometheus/xxx/yyy/cn-hangzhou/api/v3/write basic_auth: {username: 666, password: '******'} write_relabel_configs: - source_labels: [instance_id] separator: ; regex: si-6e2ca86444db4e5***** replacement: $1 action: keep 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: yesIf
yesis returned, the remote write configuration is created for the Prometheus instance.
Verify the creation in the console
-
Log on to the ARMS console.
-
In the left navigation pane, choose .
Click the name of the Managed Service for Prometheus instance instance that you want to manage to go to the Integration Center page.
- Click custom in the Installed section. In the panel that appears, click the Remote Write tab to view the created remote write configuration.
Delete the remote write configuration
Run the following command to delete the remote write configuration that was created by 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 deletion in the console
-
Log on to the ARMS console.
-
In the left navigation pane, choose .
Click the name of the Managed Service for Prometheus instance instance that you want to manage to go to the Integration Center page.
- Click custom in the Installed section. In the panel that appears, click the Remote Write tab. If the remote write configuration no longer appears, it has been deleted.