通過Terraform創(chuàng)建SLS告警
Terraform是一種開源工具,用于安全高效地預(yù)覽、配置和管理云基礎(chǔ)架構(gòu)和資源。本文介紹如何通過Terraform創(chuàng)建日志服務(wù)告警。
本教程所含示例代碼支持一鍵運(yùn)行,您可以直接運(yùn)行代碼。一鍵運(yùn)行
前提條件
已開通日志服務(wù)。具體操作,請參見資源管理概述。
為了降低信息安全風(fēng)險(xiǎn),建議使用最小權(quán)限的RAM用戶完成此教程的操作。請參見創(chuàng)建RAM用戶與為RAM用戶授權(quán),完成此教程所需權(quán)限的權(quán)限策略如下:
{ "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": "*" } ] }
準(zhǔn)備Terraform運(yùn)行環(huán)境,您可以選擇以下任一方式來使用Terraform。
在Terraform Explorer中使用Terraform:阿里云提供了Terraform的在線運(yùn)行環(huán)境,您無需安裝Terraform,登錄后即可在線使用和體驗(yàn)Terraform。適用于零成本、快速、便捷地體驗(yàn)和調(diào)試Terraform的場景。
Cloud Shell:阿里云Cloud Shell中預(yù)裝了Terraform的組件,并已配置好身份憑證,您可直接在Cloud Shell中運(yùn)行Terraform的命令。適用于低成本、快速、便捷地訪問和使用Terraform的場景。
在本地安裝和配置Terraform:適用于網(wǎng)絡(luò)連接較差或需要自定義開發(fā)環(huán)境的場景。
本教程示例包含的部分資源會(huì)產(chǎn)生一定費(fèi)用,請?jiān)诓恍枰獣r(shí)及時(shí)進(jìn)行釋放或退訂。
使用的資源
alicloud_log_project:項(xiàng)目(Project)。
alicloud_log_store:日志庫(Logstore)。
alicloud_log_alert:告警規(guī)則。
步驟一:創(chuàng)建項(xiàng)目與日志庫
創(chuàng)建一個(gè)工作目錄,并在該工作目錄中創(chuàng)建名為main.tf的配置文件。將代碼復(fù)制到main.tf中。
variable "region" { default = "cn-hangzhou" } variable "email_list" { type = list(string) description = "告警發(fā)出后的通知對象" default = ["ali***@alibaba-inc.com", "tf-example@123.com"] } provider "alicloud" { region = var.region } resource "random_integer" "default" { min = 10000 max = 99999 } # 項(xiàng)目(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 }
執(zhí)行如下命令,初始化Terraform運(yùn)行環(huán)境。
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.
執(zhí)行如下命令,開始執(zhí)行代碼。
terraform apply
在執(zhí)行過程中,根據(jù)提示輸入
yes
并按下Enter鍵,等待命令執(zhí)行完成,若出現(xiàn)以下信息,則表示代碼執(zhí)行成功。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.
驗(yàn)證結(jié)果。
執(zhí)行terraform show命令
您可以在工作目錄中,使用以下命令查詢Terraform已創(chuàng)建資源的詳細(xì)信息:
terraform show
登錄控制臺(tái)查看
登錄日志服務(wù)控制臺(tái),查看已創(chuàng)建的Project。
單擊Project列表的項(xiàng)目名稱,單擊日志存儲(chǔ),查看已創(chuàng)建的日志庫。
步驟二:創(chuàng)建告警規(guī)則
在
main.tf
文件中增加如下代碼。# 告警規(guī)則 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" } }
創(chuàng)建執(zhí)行計(jì)劃,并預(yù)覽變更。
terraform plan
執(zhí)行如下命令,開始執(zhí)行代碼。
terraform apply
在執(zhí)行過程中,根據(jù)提示輸入
yes
并按下Enter鍵,等待命令執(zhí)行完成,若出現(xiàn)以下信息,則表示代碼執(zhí)行成功。Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
驗(yàn)證結(jié)果。
執(zhí)行terraform show命令
您可以在工作目錄中,使用以下命令查詢Terraform已創(chuàng)建資源的詳細(xì)信息:
terraform show
登錄控制臺(tái)查看
登錄日志服務(wù)控制臺(tái),單擊Project列表的項(xiàng)目名稱,單擊告警,查看已創(chuàng)建的告警規(guī)則。
清理資源
當(dāng)您不再需要上述通過Terraform創(chuàng)建或管理的資源時(shí),請運(yùn)行以下命令釋放資源。關(guān)于terraform destroy
的更多信息,請參見Terraform常用命令。
terraform destroy
完整示例
本教程所含示例代碼支持一鍵運(yùn)行,您可以直接運(yùn)行代碼。一鍵運(yùn)行
示例代碼
variable "region" {
default = "cn-hangzhou"
}
variable "email_list" {
type = list(string)
description = "告警發(fā)出后的通知對象"
default = ["ali***@alibaba-inc.com", "tf-example@123.com"]
}
provider "alicloud" {
region = var.region
}
resource "random_integer" "default" {
min = 10000
max = 99999
}
# 項(xiàng)目(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
}
# 告警規(guī)則
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"
}
}
如果您想體驗(yàn)更多完整示例,請前往更多完整示例中對應(yīng)產(chǎn)品的文件夾查看。
相關(guān)文檔
Terrafrom介紹,請參見Terraform產(chǎn)品介紹。
當(dāng)您遇到由于網(wǎng)絡(luò)延遲等原因造成的
terraform init
超時(shí),導(dǎo)致無法正常下載Provider等情況時(shí),請參見Terraform Init 加速方案配置。