Manage Prometheus instances by using Terraform
You can use Terraform to create, import, and delete Managed Service for Prometheus instances as code, enabling version-controlled and repeatable infrastructure deployments.
Prerequisites
Before you begin, make sure that the following requirements are met:
Terraform is installed.
Cloud Shell has Terraform and Alibaba Cloud account credentials preconfigured. No additional setup is required.
If you do not use Cloud Shell, install and configure Terraform on your local machine. For more information, see Install and configure Terraform locally.
NoteMake sure that your Terraform version is 1.5.0 or later. Run
terraform --versionto check the version.Alibaba Cloud credentials are configured by using one of the following methods:
NoteFor security best practices, we recommend that you create a RAM user named Terraform and grant it the required permissions instead of using your Alibaba Cloud account. For more information, see Create a RAM user and Grant permissions to a RAM user.
Method 1: Environment variables
Create environment variables to store your credentials:
export ALICLOUD_ACCESS_KEY="<your-access-key-id>" export ALICLOUD_SECRET_KEY="<your-access-key-secret>" export ALICLOUD_REGION="<region-id>"NoteReplace
<region-id>with the ID of the region in which you want to create the Prometheus instance, such ascn-beijing. To find available region IDs, see Regions and zones.Method 2: Provider configuration
Specify credentials in the
providerblock of your Terraform configuration file:provider "alicloud" { access_key = "<your-access-key-id>" secret_key = "<your-access-key-secret>" region = "<region-id>" endpoints { cms = "metrics.<region-id>.aliyuncs.com" } }ImportantYou must explicitly specify the CMS endpoint in the format
metrics.<region-id>.aliyuncs.com. Replace<region-id>with the actual region ID.
Create a Prometheus instance
Procedure
Create a working directory and create a file named
main.tfwith the following content:terraform { required_version = ">= 1.5.0" required_providers { alicloud = { source = "aliyun/alicloud" version = "~> 1.277.0" } } } provider "alicloud" { access_key = "<your-access-key-id>" secret_key = "<your-access-key-secret>" region = "cn-beijing" endpoints { cms = "metrics.cn-beijing.aliyuncs.com" } } resource "alicloud_cms_prometheus_instance" "this" { workspace = "<your-workspace-name>" prometheus_instance_name = "<instance-name>" archive_duration = 0 enable_auth_free_read = true enable_auth_free_write = true auth_free_read_policy = "{\"SourceIp\":[\"192.168.0.1/12\"],\"SourceVpc\":[\"vpc-xxx\"]}" auth_free_write_policy = "{\"SourceIp\":[\"192.168.0.1/12\"],\"SourceVpc\":[\"vpc-xxx\"]}" storage_duration = 30 } output "prometheus_instance_id" { value = alicloud_cms_prometheus_instance.this.id }The following table describes the parameters in the
resourceblock:Parameter Description Required workspaceThe name of the workspace. Changing this forces a new resource to be created. To find the workspace name, navigate to Workspace Management in the CloudMonitor console. Yes prometheus_instance_nameThe name of the Prometheus instance. Must be 3 to 200 characters in length. Yes archive_durationThe number of days to retain data after the storage duration expires. Valid values: 0, 60, 90, 180, 365, and 3650. A value of 0 means that data is not archived. No storage_durationThe number of days to retain data. Data that exceeds this storage duration is archived or deleted based on the archive_duration setting. No enable_auth_free_readSpecifies whether to enable auth-free read. Default value: false. No enable_auth_free_writeSpecifies whether to enable auth-free write. Default value: false. No auth_free_read_policyThe auth-free read policy in JSON format. Specifies allowed source IP addresses and VPCs. No auth_free_write_policyThe auth-free write policy in JSON format. Specifies allowed source IP addresses and VPCs. No Initialize the Terraform working directory:
terraform initExpected output:
Initializing the backend... Initializing provider plugins... - Finding aliyun/alicloud versions matching "~> 1.277.0"... - Installing aliyun/alicloud v1.277.0... - Installed aliyun/alicloud v1.277.0 (signed by a HashiCorp partner, key ID ...) Terraform has been successfully initialized!Preview the execution plan:
terraform planExpected output:
Terraform will perform the following actions: # alicloud_cms_prometheus_instance.this will be created ... Plan: 1 to add, 0 to change, 0 to destroy.Create the Prometheus instance:
terraform applyWhen prompted, enter
yesto confirm.Expected output:
alicloud_cms_prometheus_instance.this: Creating... alicloud_cms_prometheus_instance.this: Creation complete after 10s [id=xxx] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: prometheus_instance_id = "xxx"
Verify the result
After the instance is created, verify it in the Managed Service for Prometheus console:
Log on to the Managed Service for Prometheus console.
In the left-side navigation pane, click Instance List.
On the Instance List page, find the Prometheus instance that you created.
Import an existing Prometheus instance
To bring an existing Prometheus instance under Terraform management, use one of the following methods.
Import block (Terraform v1.5+)
Add an import block to your configuration:
import {
to = alicloud_cms_prometheus_instance.this
id = "<prometheus-instance-id>"
}CLI import
Run the following command:
terraform import alicloud_cms_prometheus_instance.this <prometheus-instance-id>Delete a Prometheus instance
Run the following command to delete the Prometheus instance that was created by Terraform:
terraform destroyWhen prompted, enter yes to confirm.
Expected output:
alicloud_cms_prometheus_instance.this: Destroying... [id=xxx]
alicloud_cms_prometheus_instance.this: Destruction complete after 5s
Destroy complete! Resources: 1 destroyed.This operation is irreversible. After you run terraform destroy, the Prometheus instance and all associated data are permanently deleted.
Timeouts
The following default timeouts apply to this resource:
| Operation | Default timeout |
|---|---|
| Create | 5 minutes |
update | 5 minutes |
| Delete | 5 minutes |
Common errors
The following table describes common errors and their solutions:
| Symptom | Cause | Resolution |
|---|---|---|
Error: InvalidParameter.workspace | The specified workspace name does not exist or is misspelled. | Verify the workspace name in the CloudMonitor console. Navigate to Workspace Management and copy the exact workspace name. |
Error: Forbidden.RAM | The RAM user does not have the required permissions to manage Prometheus instances. | Attach the AliyunCMSFullAccess policy to the RAM user, or create a custom policy that grants cms:CreatePrometheusInstance and cms:DeletePrometheusInstance permissions. |
Error: DuplicateInstanceName | A Prometheus instance with the same name already exists in the workspace. | Use a different instance name, or import the existing instance into Terraform state by using the terraform import command. |
Error: could not retrieve the provider plugins or API endpoint connection timeout | The CMS endpoint is not configured or is incorrect in the provider block. | Add the endpoints block to the provider configuration and set the CMS endpoint to metrics.<region-id>.aliyuncs.com. |
Terraform state lock error: Error acquiring the state lock | Another Terraform process is running or a previous operation was interrupted without releasing the lock. | Wait for the other process to complete, or run terraform force-unlock <lock-id> to manually release the lock. Use force-unlock only when you are sure no other process is running. |