Create a CloudMonitor alert contact

更新时间:
复制 MD 格式

Use Terraform to create a CloudMonitor alert contact that receives notifications by email.

Note

Run this example in your browser with Terraform Explorer.

Prerequisites

Before you begin, make sure you have:

  • An Alibaba Cloud account and a Resource Access Management (RAM) user with an AccessKey pair. For details, see Create a RAM user and Create an AccessKey pair

  • The AliyunCloudMonitorFullAccess permission granted to the RAM user. For details, see Grant permissions to a RAM user

  • A Terraform runtime environment. Choose one of the following options:

    • Terraform Explorer -- an online environment that requires no installation (recommended)

    • Cloud Shell -- Terraform preinstalled with identity credentials configured

    • Local installation -- suitable when you need a custom development environment or have unstable network connections

Important

Terraform 0.12.28 or later is required. Run terraform --version to check your version.

AliyunCloudMonitorFullAccess policy document

{
    "Version": "1",
    "Statement": [
        {
            "Action": "cms:*",
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": "arms:*",
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "ecs:DescribeInstances",
                "rds:DescribeDBInstances",
                "slb:DescribeLoadBalancer*",
                "vpc:DescribeEipAddresses",
                "vpc:DescribeRouterInterfaces",
                "vpc:DescribeGlobalAccelerationInstances",
                "vpc:DescribeVpnGateways",
                "vpc:DescribeNatGateways",
                "vpc:DescribeBandwidthPackages",
                "vpc:DescribeCommonBandwidthPackages",
                "oss:ListBuckets",
                "log:ListProject",
                "cdn:DescribeUserDomains",
                "mns:ListQueue",
                "mns:ListTopic",
                "ess:DescribeScalingGroups",
                "ocs:DescribeInstances",
                "kvstore:DescribeInstances",
                "kvstore:DescribeLogicInstanceTopology",
                "hbase:DescribeClusterList",
                "hitsdb:DescribeHiTSDBInstanceList",
                "dds:DescribeDBInstances",
                "petadata:DescribeInstances",
                "petadata:DescribeDatabases",
                "gpdb:DescribeDBInstances",
                "emr:ListClusters",
                "opensearch:ListApps",
                "elasticsearch:ListInstance",
                "mongodb:DescribeDBInstances",
                "rds:DescribeReplicas",
                "CloudAPI:DescribeApis",
                "netgateway:DescribeNatGateways",
                "ddos:DescribeInstancePage",
                "live:DescribeLiveUserDomains",
                "cen:DescribeCens",
                "cen:DescribeCenAttachedChildInstances",
                "kafka:GetKafkaInstanceList",
                "scdn:DescribeScdnUserDomains",
                "dcdn:DescribeDcdnUserDomains",
                "polardb:DescribeDBInstances",
                "polardb:DescribeRegions",
                "polardb:DescribeDBClusters",
                "cs:DescribeClusters",
                "resourcemanager:GetAccount",
                "resourcemanager:ListAccountsForParent",
                "resourcemanager:ListAccounts",
                "resourcemanager:GetFolder",
                "resourcemanager:ListFoldersForParent",
                "resourcemanager:ListAncestors",
                "resourcemanager:GetResourceDirectory"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "quotas:ListProductQuotas",
                "quotas:GetProductQuota",
                "quotas:ListProducts",
                "quotas:ListProductQuotaDimensions",
                "quotas:ListProductDimensionGroups"
            ],
            "Resource": "acs:quotas:*:*:*",
            "Effect": "Allow"
        },
        {
            "Action": "ram:CreateServiceLinkedRole",
            "Resource": "*",
            "Effect": "Allow",
            "Condition": {
                "StringEquals": {
                    "ram:ServiceName": "cloudmonitor.aliyuncs.com"
                }
            }
        }
    ]
}

Resources used

alicloud_cms_alarm_contact: Creates an alert contact.

Step 1: Write the configuration

Create a working directory and add a main.tf file with the following configuration:

terraform {
  required_providers {
    alicloud = {
      source  = "aliyun/alicloud"
      version = ">= 1.99.0"
    }
  }
  required_version = ">= 0.12.28"
}

variable "region" {
  default = "cn-heyuan"
}

provider "alicloud" {
  region = var.region
}

variable "name" {
  default = "tf_example"
}

variable "describe" {
  default = "For example"
}

variable "mail" {
  default = "username@example.com"
}

resource "alicloud_cms_alarm_contact" "example" {
  alarm_contact_name = var.name
  describe           = var.describe
  channels_mail      = var.mail

  lifecycle {
    ignore_changes = [channels_mail]
  }
}

The configuration defines the following:

Block Purpose
terraform Declares the Alibaba Cloud provider source and version constraints
provider "alicloud" Configures the Alibaba Cloud provider with the cn-heyuan region
resource "alicloud_cms_alarm_contact" Creates an alert contact with a name, description, and email address

The alicloud_cms_alarm_contact resource supports the following arguments:

Argument Required Description
alarm_contact_name Yes Name of the alert contact. Must be 2 to 40 characters
describe Yes Description of the alert contact
channels_mail No Email address for alert notifications
channels_sms No Phone number for SMS alert notifications
Important

After Terraform creates the contact, the specified email address receives an activation link. The contact does not receive alert notifications until you activate the email. The lifecycle { ignore_changes = [channels_mail] } block prevents Terraform from detecting this external activation as configuration drift.

Step 2: Initialize Terraform

Run terraform init to download the Alibaba Cloud provider plugin:

terraform init

Expected output:

Initializing the backend...

Initializing provider plugins...
- Finding aliyun/alicloud versions matching ">= 1.99.0"...
- Installing aliyun/alicloud v1.234.0...
- Installed aliyun/alicloud v1.234.0 (signed by a HashiCorp partner, key ID ...)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

Step 3: Preview changes

Run terraform plan to preview the resources that Terraform will create:

terraform plan

Review the output to confirm that Terraform will create one alicloud_cms_alarm_contact resource.

Step 4: Create the alert contact

Run terraform apply to create the alert contact:

terraform apply

When prompted, type yes and press Enter to confirm. Expected output:

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

alicloud_cms_alarm_contact.example: Creating...
alicloud_cms_alarm_contact.example: Creation complete after 1s [id=********]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Step 5: Verify the result

Verify with Terraform CLI

Run terraform show to inspect the created resource:

terraform show

The output displays the alert contact attributes, including alarm_contact_name, describe, and channels_mail.

Verify in the CloudMonitor console

  1. Log on to the CloudMonitor console.

  2. Navigate to the alert contacts page.

  3. Confirm that the alert contact appears in the list.

Clean up resources

To delete the alert contact, run terraform destroy:

terraform destroy

When prompted, type yes and press Enter to confirm. For more information about the terraform destroy command, see Common commands.

What's next