本文介绍如何使用Terraform调用接口操作日志服务告警。

前提条件

已安装和配置Terraform。具体操作,请参见在Cloud Shell中使用Terraform在本地安装和配置Terraform

背景信息

Terraform是一种开源工具,用于安全高效地预览、配置和管理云基础架构和资源。Terraform的命令行接口(CLI)提供了一种简单机制,用于将配置文件部署到阿里云或其他任意支持的云上,并对其进行版本控制。

使用场景

使用Terraform操作日志服务告警,主要包括如下使用场景。

使用场景

场景1:初始化告警资源

  • 初始化阿里云账号告警资源,包括初始化中心Project、中心Logstore和内置告警仪表盘等。
    说明 每个阿里云账号只需执行一次告警资源初始化操作。
    • 中心Project:名称为sls-alert-{uid}-{region}。其中uid为阿里云账号,region为您所指定的中心Project所在地域。
    • 中心Logstore:名称为internal-alert-center-log,该Logstore属于中心Project,可免费使用,用于存储告警执行过程中的执行历史和诊断信息。
    • 内置告警仪表盘:包括全局告警排障中心、全局告警链路中心、全局告警规则中心和开放告警中心。
  • 初始化Project告警资源,包括初始化告警历史统计Logstore和内置告警历史统计仪表盘。

    告警监控规则属于Project,因此在某个Project中创建告警监控规则前,需先初始化Project下的告警资源。

    说明 每个Project只需执行一次告警资源初始化操作。
    • 告警历史统计Logstore:名称为internal-alert-history,该Logstore可免费使用,用于存储当前Project下所有告警监控规则的评估历史记录,包括每次评估的状态和告警触发状态。
    • 内置告警历史统计仪表盘:名称为internal-alert-analysis,用于展示告警监控规则的执行历史成功率等。

场景2:管理告警监控规则

告警监控规则是指对时序数据、日志等数据源的监控设置,包括协同监控、分组评估、触发条件、严重度、无数据告警和告警恢复等参数设置。

场景3:管理告警资源

告警管理系统接收来自于告警监控系统的告警事务,完成告警编排(例如静默、抑制、去重等),然后将告警事务发送给通知管理系统,再由通知管理系统通知用户。此告警过程涉及告警策略、行动策略、用户、用户组、Webhook等告警资源。

步骤一:配置身份信息以及告警中心地域

在环境变量中配置用户身份信息以及告警中心Project所在地域。

export ALICLOUD_ACCESS_KEY="LT****AD"
export ALICLOUD_SECRET_KEY="zf****SS"
export ALICLOUD_REGION="cn-heyuan"
参数 说明
ALICLOUD_ACCESS_KEY 阿里云访问密钥AccessKey ID。更多信息,请参见访问密钥
ALICLOUD_SECRET_KEY 阿里云访问密钥AccessKey Secret。更多信息,请参见访问密钥
ALICLOUD_REGION 告警的中心Project所在地域。

步骤二:配置告警

说明 下述脚本中的example1example2等为Resource名称,可自定义配置,但在同一个terraform.tf文件中不可重复。
  1. 创建一个Terraform工作目录(例如名为sls的工作目录),并在该目录下创建一个名为terraform.tf的文件。
  2. terraform.tf文件中,添加如下内容,初始化阿里云账号告警资源。
    参数说明,请参见alicloud_log_alert_resource
    data "alicloud_log_alert_resource" "example1" {
      type          = "user"
      lang          = "cn"
    }

    完成该初始化后,日志服务将在您所指定的告警中心Project地域(ALICLOUD_REGION)创建告警中心Project、告警中心Logstore和内置告警仪表盘。

  3. terraform.tf文件中,添加如下内容,初始化Project告警资源。

    project需位于您所指定的告警中心Project地域(ALICLOUD_REGION)。参数说明,请参见alicloud_log_alert_resource

    data "alicloud_log_alert_resource" "example2" {
      type          = "project"
      project       = "test-project"
    }

    完成该初始化后,日志服务将在名为test-project的Project中创建一个名为internal-alert-log的Logstore。

  4. terraform.tf文件中,添加如下内容,创建告警监控规则。
    创建告警监控规则时,包括告警名称、无数据告警、查询列表、标签、告警策略、行动策略等配置。参数说明,请参见alicloud_log_alert
    resource "alicloud_log_alert" "example3" {
      version           = "2.0"
      type              = "default"
      project_name      = "test-project"
      alert_name        = "tf-test-alert-2"
      alert_displayname = "tf-test-alert-displayname-2"
      dashboard         = "tf-test-dashboard"
      mute_until        = "1666666666"
      no_data_fire      = "false"
      no_data_severity  = 8
      send_resolved     = true
      schedule_interval = "5m"
      schedule_type     = "FixedRate"
      query_list {
        store       = "tf-test-logstore"
        store_type  = "log"
        project     = "test-project"
        region      = "cn-heyuan"
        chart_title = "chart_title"
        start       = "-60s"
        end         = "20s"
        query       = "* AND aliyun | select count(1) as cnt"
        time_span_type = "Custom"
      }
      query_list {
        store       = "tf-test-logstore-5"
        store_type  = "log"
        project     = "test-project"
        region      = "cn-heyuan"
        chart_title = "chart_title"
        start       = "-60s"
        end         = "20s"
        query       = "error | select count(1) as error_cnt"
        time_span_type = "Custom"
      }
      join_configurations {
          type = "cross_join"
          condition = ""
      }
      labels {
        key = "env"
        value = "test"
      }
      labels {
        key = "env1"
        value = "test1"
      }
      annotations {
        key = "title"
        value = "alert title-1"
      }
      annotations {
        key = "desc"
        value = "alert desc"
      }
      annotations {
        key = "test_key"
        value = "test value"
      }
      group_configuration {
        type   = "custom"
        fields = ["a", "b", "d"]
      }
      severity_configurations {
        severity = 8
        eval_condition = {
          condition = "cnt > 3"
          count_condition = "__count__ > 3"
        }
      }
      severity_configurations {
        severity = 6
        eval_condition = {
          condition = ""
          count_condition = "__count__ > 0"
        }
      }
      severity_configurations {
        severity = 2
        eval_condition = {
          condition = ""
          count_condition = ""
        }
      }
      
      policy_configuration {
        alert_policy_id  = "sls.builtin.dynamic"
        action_policy_id = "sls_test_action"
        repeat_interval  = "1m"
      }
    }
  5. terraform.tf文件中,添加如下内容,创建告警资源。
    告警资源主要包括用户、用户组、值班组、webhook集成、告警策略、行动策略、内容模板、默认日志和渠道额度等。此处以创建用户为例,告警资源的数据结构,请参见告警资源数据的结构。参数说明,请参见alicloud_log_resource_record
    resource "alicloud_log_resource_record" "user" {
      resource_name         = "sls.common.user"
      record_id             = "test_tf_user"
      tag                   = "test tf user" 
      value                 = "{\n\t\"user_name\": \"test tf user\", \n\t\"sms_enabled\": true, \n\t\"phone\": \"1390000****\", \n\t\"voice_enabled\": false, \n\t\"email\": [\n\t\t\"t**@example.com\"\n\t], \n\t\"enabled\": true, \n\t\"user_id\": \"test_tf_user\", \n\t\"country_code\": \"86\"\n}"
    }
  6. 使terraform.tf文件中的告警配置生效。
    1. 执行如下命令。
      terraform apply
    2. 输入yes
      如果返回结果中提示Apply complete!,表示应用告警配置成功,日志服务会根据此告警配置监控数据,完成告警。terraform