通过Terraform创建SLS告警

Terraform是一种开源工具,用于安全高效地预览、配置和管理云基础架构和资源。本文介绍如何通过Terraform创建日志服务告警。

说明

本教程所含示例代码支持一键运行,您可以直接运行代码。一键运行

前提条件

  • 已开通日志服务。具体操作,请参见资源管理概述

  • 为了降低信息安全风险,建议使用最小权限的RAM用户完成此教程的操作。请参见创建RAM用户为RAM用户授权,完成此教程所需权限的权限策略如下:

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "log:GetProject",
            "log:ListProject",
            "log:CreateProject",
            "log:DeleteProject",
            "log:UpdateProject",
            "log:GetLogStore",
            "log:GetLogStoreLogs",
            "log:GetLogStoreMeteringMode",
            "log:ListLogStores",
            "log:CreateLogStore",
            "log:PostLogStoreLogs",
            "log:UpdateLogStore",
            "log:GetDashboard",
            "log:CreateDashboard",
            "log:UpdateDashboard",
            "log:ListDashboard",
            "log:ListTagResources",
            "log:ListShards",
            "log:GetJob",
            "log:UpdateJob"
          ],
          "Resource": "*"
        },
        {
          "Effect": "Allow",
          "Action": [
            "log:GetProjectPolicy",
            "log:DeleteJob",
            "log:DeleteLogStore",
            "log:CreateJob"
          ],
          "Resource": "*"
        }
      ]
    }
  • 准备Terraform运行环境,您可以选择以下任一方式来使用Terraform。

    • 在Terraform Explorer中使用Terraform:阿里云提供了Terraform的在线运行环境,您无需安装Terraform,登录后即可在线使用和体验Terraform。适用于零成本、快速、便捷地体验和调试Terraform的场景。

    • Cloud Shell:阿里云Cloud Shell中预装了Terraform的组件,并已配置好身份凭证,您可直接在Cloud Shell中运行Terraform的命令。适用于低成本、快速、便捷地访问和使用Terraform的场景。

    • 在本地安装和配置Terraform:适用于网络连接较差或需要自定义开发环境的场景。

说明

本教程示例包含的部分资源会产生一定费用,请在不需要时及时进行释放或退订。

使用的资源

步骤一:创建项目与日志库

  1. 创建一个工作目录,并在该工作目录中创建名为main.tf的配置文件。将代码复制到main.tf中。

    variable "region" {
      default = "cn-hangzhou"
    }
    
    variable "email_list" {
      type        = list(string)
      description = "告警发出后的通知对象"
      default     = ["ali***@alibaba-inc.com", "tf-example@123.com"]
    }
    
    provider "alicloud" {
      region = var.region
    }
    
    resource "random_integer" "default" {
      min = 10000
      max = 99999
    }
    
    # 项目(Project)
    resource "alicloud_log_project" "example" {
      project_name = "project-name-${random_integer.default.result}"
      description  = "tf actiontrail example"
    }
    
    # 日志库(Logstore)
    resource "alicloud_log_store" "example" {
      project_name     = alicloud_log_project.example.project_name
      logstore_name    = "logstore_example_${random_integer.default.result}"
      retention_period = 3
    }
  2. 执行如下命令,初始化Terraform运行环境。

    terraform init

    返回信息如下,则Terraform初始化成功。

    Terraform has been successfully initialized!
    
    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.
  3. 执行如下命令,开始执行代码。

    terraform apply

    在执行过程中,根据提示输入yes并按下Enter键,等待命令执行完成,若出现以下信息,则表示代码执行成功。

    You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure.
    
    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
    
    
    Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
  4. 验证结果。

    执行terraform show命令

    您可以在工作目录中,使用以下命令查询Terraform已创建资源的详细信息:

    terraform show

    image

    登录控制台查看

    1. 登录日志服务控制台,查看已创建的Project。

      image

    2. 单击Project列表的项目名称,单击日志存储,查看已创建的日志库。

      image

步骤二:创建告警规则

  1. main.tf文件中增加如下代码。

    # 告警规则
    resource "alicloud_log_alert" "example" {
      project_name      = alicloud_log_project.example.name
      alert_name        = "alert_name_${random_integer.default.result}"
      alert_displayname = "alert_displayname_${random_integer.default.result}"
      condition         = "count> 100"
      dashboard         = "example-dashboard"
      schedule {
        type            = "FixedRate"
        interval        = "5m"
        hour            = 0
        day_of_week     = 0
        delay           = 0
        run_immediately = false
      }
      query_list {
        logstore    = alicloud_log_store.example.name
        chart_title = "chart_title"
        start       = "-60s"
        end         = "20s"
        query       = "* AND aliyun"
      }
      notification_list {
        type       = "Email"
        email_list = var.email_list
        content    = "alert content"
      }
    }
  2. 创建执行计划,并预览变更。

    terraform plan
  3. 执行如下命令,开始执行代码。

    terraform apply

    在执行过程中,根据提示输入yes并按下Enter键,等待命令执行完成,若出现以下信息,则表示代码执行成功。

    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  4. 验证结果。

    执行terraform show命令

    您可以在工作目录中,使用以下命令查询Terraform已创建资源的详细信息:

    terraform show

    image

    登录控制台查看

    登录日志服务控制台,单击Project列表的项目名称,单击告警,查看已创建的告警规则。

    image

清理资源

当您不再需要上述通过Terraform创建或管理的资源时,请运行以下命令释放资源。关于terraform destroy的更多信息,请参见Terraform常用命令

terraform destroy

完整示例

说明

本教程所含示例代码支持一键运行,您可以直接运行代码。一键运行

示例代码

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

variable "email_list" {
  type        = list(string)
  description = "告警发出后的通知对象"
  default     = ["ali***@alibaba-inc.com", "tf-example@123.com"]
}

provider "alicloud" {
  region = var.region
}

resource "random_integer" "default" {
  min = 10000
  max = 99999
}

# 项目(Project)
resource "alicloud_log_project" "example" {
  project_name = "project-name-${random_integer.default.result}"
  description  = "tf actiontrail example"
}

# 日志库(Logstore)
resource "alicloud_log_store" "example" {
  project_name     = alicloud_log_project.example.project_name
  logstore_name    = "logstore_example_${random_integer.default.result}"
  retention_period = 3
}

# 告警规则
resource "alicloud_log_alert" "example" {
  project_name      = alicloud_log_project.example.name
  alert_name        = "alert_name_${random_integer.default.result}"
  alert_displayname = "alert_displayname_${random_integer.default.result}"
  condition         = "count> 100"
  dashboard         = "example-dashboard"
  schedule {
    type            = "FixedRate"
    interval        = "5m"
    hour            = 0
    day_of_week     = 0
    delay           = 0
    run_immediately = false
  }
  query_list {
    logstore    = alicloud_log_store.example.name
    chart_title = "chart_title"
    start       = "-60s"
    end         = "20s"
    query       = "* AND aliyun"
  }
  notification_list {
    type       = "Email"
    email_list = var.email_list
    content    = "alert content"
  }
}

如果您想体验更多完整示例,请前往更多完整示例中对应产品的文件夹查看。

相关文档