本文介绍如何使用Terraform调用接口操作日志服务告警。
背景信息
Terraform是一种开源工具,用于安全高效地预览、配置和管理云基础架构和资源。Terraform的命令行接口(CLI)提供了一种简单机制,用于将配置文件部署到阿里云或其他任意支持的云上,并对其进行版本控制。
使用场景
使用Terraform操作日志服务告警,主要包括如下使用场景。
场景1:初始化告警资源
场景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所在地域。 |
步骤二:配置告警
说明 下述脚本中的example1、example2等为Resource名称,可自定义配置,但在同一个terraform.tf文件中不可重复。
- 创建一个Terraform工作目录(例如名为sls的工作目录),并在该目录下创建一个名为terraform.tf的文件。
- 在terraform.tf文件中,添加如下内容,初始化阿里云账号告警资源。
参数说明,请参见
alicloud_log_alert_resource。
data "alicloud_log_alert_resource" "example1" {
type = "user"
lang = "cn"
}
完成该初始化后,日志服务将在您所指定的告警中心Project地域(ALICLOUD_REGION)创建告警中心Project、告警中心Logstore和内置告警仪表盘。
- 在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。
- 在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"
}
}
- 在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}"
}
- 使terraform.tf文件中的告警配置生效。
- 执行如下命令。
- 输入yes。
如果返回结果中提示
Apply complete!
,表示应用告警配置成功,日志服务会根据此告警配置监控数据,完成告警。