Terraform是由HashiCorp開源的開發人員工具,支援以代碼的形式管理雲基礎設施。作為與Terraform整合的主流雲廠商之一,阿里雲已在Terraform社區發布了多個Security Center相關的資源(Resource)和資料來源(Data Source)。您可以使用Terraform管理這些資源,滿足通過簡單編碼自動化部署、持續演化雲基礎設施等業務訴求。本文介紹如何使用Terraform快速開通Security Center資源。
當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
-
由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作,請參見建立RAM使用者和建立AccessKey。
-
為運行Terraform命令的RAM使用者綁定以下最小權限原則,以擷取管理本樣本所涉及資源的許可權。更多資訊,請參見管理RAM使用者的許可權。
該權限原則允許RAM使用者描述、修改和建立Security Center的相關資源。請根據實際情況調整策略中的動作和資源範圍。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "sas:Describe*", // 允許查看Security Center的相關資訊 "sas:CreateInstance", // 允許建立Security Center執行個體 "sas:UpdateInstance", // 允許升級Security Center執行個體 "sas:Modify*" // 如果有修改其他設定的需求,可以添加該參數 ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "ecs:DescribeInstances" // 可能需要查詢ECS執行個體的許可權,以便於關聯到Security Center ], "Resource": "*" } ] }
準備工作
準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。
-
在Terraform Explorer中使用Terraform:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。
-
使用Terraform快速建立資源:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。
-
在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。
使用的資源
alicloud_threat_detection_instance:用於配置和管理阿里雲威脅檢測服務的資源類型。
開通Security Center執行個體
開通Security Center執行個體後,請務必謹慎管理其生命週期,避免產生不必要的費用。
下列程式碼範例以在杭州(cn-hangzhou)地區下開通Security Center企業版訂用帳戶執行個體為例,介紹開通Security Center的具體操作。
-
建立並進入執行目錄。
mkdir /usr/local/terraform cd /usr/local/terraform說明您需要為每個Terraform專案建立一個獨立的執行目錄。
-
建立terraform.tf檔案,並設定檔內容。
vim terraform.tfterraform.tf檔案內容填寫如下:
# 配置阿里雲provider,設定地區為杭州(cn-hangzhou) provider "alicloud" { region = "cn-hangzhou" } # 定義資源名稱,預設值為"terraform-example" variable "name" { default = "terraform-example" } # 版本代碼,預設值為"level2"(企業版) variable "version_code" { default = "level2" } # 購買的伺服器數量,預設值為"30" variable "buy_number" { default = "30" } # 付費模式,預設值為"Subscription"(訂閱) variable "payment_type" { default = "Subscription" } # 預付費周期,預設值為"1"(單位:月) variable "period" { default = "1" } # 自動續訂狀態,預設值為"ManualRenewal"(手動續訂) variable "renewal_status" { default = "ManualRenewal" } # 日誌分析儲存容量,預設值為"100"(單位:GB) variable "sas_sls_storage" { default = "100" } # 防勒索資料容量,預設值為"100"(單位:GB) variable "sas_anti_ransomware" { default = "100" } # 網頁防篡改開關,預設值為"1"(是) variable "sas_webguard_boolean" { default = "1" } # 網頁防篡改授權數量,預設值為"100" variable "sas_webguard_order_num" { default = "100" } # 雲蜜罐開關,預設值為"1"(是) variable "honeypot_switch" { default = "1" } # 雲蜜罐授權數,預設值為"32" variable "honeypot" { default = "32" } # 惡意檔案檢測開關,預設值為"1"(是) variable "sas_sdk_switch" { default = "1" } # 惡意檔案檢測數量,預設值為"1000"(單位:10,000次) variable "sas_sdk" { default = "1000" } # 容器鏡像安全掃描次數,步長為20,即只能填寫20的倍數 variable "container_image_scan_new" { default = "100" } #Agentic SOC開關 variable "threat_analysis_switch" { default = "1" } #Agentic SOC流量,步長為100,即只能填寫100的倍數,預設值為"100"(單位:GB/天) variable "threat_analysis_flow" { default = "100" } #Agentic SOC與日誌儲存容量,預設值為"1000"(單位:GB) variable "threat_analysis_sls_storage" { default = "1000" } # 使用變數定義的安全威脅檢測執行個體資源 resource "alicloud_threat_detection_instance" "default" { version_code = var.version_code buy_number = var.buy_number payment_type = var.payment_type period = var.period renewal_status = var.renewal_status sas_sls_storage = var.sas_sls_storage sas_anti_ransomware = var.sas_anti_ransomware container_image_scan_new = var.container_image_scan_new sas_webguard_boolean = var.sas_webguard_boolean sas_webguard_order_num = var.sas_webguard_order_num honeypot_switch = var.honeypot_switch honeypot = var.honeypot sas_sdk_switch = var.sas_sdk_switch sas_sdk = var.sas_sdk threat_analysis_switch = var.threat_analysis_switch threat_analysis_flow = var.threat_analysis_flow threat_analysis_sls_storage = var.threat_analysis_sls_storage }配置項
必填
說明
樣本值
modify_type
否
如需升降配,該參數值為必填值,需在上述代碼中添加該配置項。取值:
-
Upgrade:升級配置。
-
Downgrade:降級配置。
Upgrade
version_code
是
Security Center的版本。取值:
-
level7:防病毒版
-
level3:進階版
-
level2:企業版
-
level8:旗艦版
-
level10:僅採購增值服務
level2
buy_number
否
要防護的伺服器台數。
說明僅當version_code設定為level3、level2或level8時,需要配置該參數。
30
v_core
否
要防護的伺服器總核心數( Vmcore總數)。
說明僅當version_code設定為level7或level8時,需要配置該參數。
100
payment_type
是
付費模式。取值:
-
Subscription:預付費模式。
Subscription
period
否
預付費周期。單位:月。
取值:1、6、12、24、36、60。
1
renewal_status
否
自動續約狀態,取值:
-
AutoRenewal:自動續約。
-
ManualRenewal(預設):手動續約。
ManualRenewal
renewal_period
否
自動續約周期,單位:月。
說明當renewal_status設定為AutoRenewal時,需要配置該參數。
12
sas_sls_storage
否
日誌分析容量,單位:GB。
取值範圍:0~600,000,步長為10。
說明應《網路安全法》日誌至少儲存180天的要求,推薦每台伺服器配置50GB日誌儲存容量。
100
sas_anti_ransomware
否
防勒索病毒的資料備份容量。單位:GB。
取值範圍:10~9,999,999,999,步長為10。
100
container_image_scan_new
否
容器鏡像安全掃描,建議設定為每個月需要進行安全風險檢測的鏡像數量。單位:個。
取值範圍:0~200,000,步長為20,即只能填寫20的倍數。
說明Security Center是以摘要(Digest)值唯一標識一個鏡像,鏡像的摘要值不變時,只在第一次掃描時消耗一個鏡像安全掃描次數。摘要值變化後,執行掃描操作會重新消耗鏡像安全掃描次數。例如,您需要檢測10個鏡像,在購買Security Center服務的期限內,預計鏡像更新總次數為20次(即所有鏡像摘要值變更總次數為20次),則容器鏡像安全掃描數量需要設定為30(即10+20)。
100
sas_webguard_boolean
否
是否開啟網頁防篡改。取值:
-
1:是
-
0:否
1
sas_webguard_order_num
否
網頁防篡改的授權數。單位:次。
取值範圍:0~9,999。
說明僅當sas_webguard_boolean設定為1時,需要配置該參數。
100
sas_sc
否
是否開通安全大屏,取值:
說明海外地區不支援購買,此參數設定後不生效。僅version_code設定為level3、level2或level8時,支援該功能。
-
true:是
-
false:否
false
honeypot_switch
否
是否開啟雲蜜罐。取值:
-
1:是
-
2:否
1
honeypot
否
雲蜜罐授權數。單位:個。
取值範圍:20~500。
說明僅當honeypot_switch設定為1時,需要配置該參數。
20
sas_sdk_switch
否
是否開啟惡意檔案檢測。取值:
-
1:是
-
0:否
1
sas_sdk
否
惡意檔案檢測次數。單位:萬次。
取值範圍:10~9,999,999,999,步長為10。
10
Threat_analysis_switch_1
否
Agentic SOC開關。取值:
-
0:否
-
1:是
1
Threat_analysis_flow
否
Agentic SOC的日誌接入流量。單位:GB/天。
取值範圍:100~9,999,999,999,步長為100。
說明僅當Threat_analysis_switch_1為1時,需要配置該參數。
100
Threat_analysis_sls_storage
否
Agentic SOC的日誌儲存容量。單位:GB。
取值範圍:0~9,999,999,999,步長為1000。
1000
-
-
執行
terraform init命令,初始化配置。返回如下資訊時,表示初始化完成:
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. -
執行
terraform apply命令,建立Security Center執行個體。在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成。如果出現以下資訊,則表示授權完成。Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:返回如下資訊時,表示建立成功:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
結果驗證
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:
terraform show
shell@Alicloud:~/022501$ terraform show
# alicloud_threat_detection_instance.default:
resource "alicloud_threat_detection_instance" "default" {
buy_number = "30"
container_image_scan_new = "100"
create_time = "2025-02-25T01:37:09Z"
honeypot = "32"
honeypot_switch = "1"
id = "sas-pb3rcrrnr0er"
payment_type = "Subscription"
period = 1
renewal_period_unit = "M"
renewal_status = "ManualRenewal"
sas_anti_ransomware = "100"
sas_cspm_switch = "0"
sas_sc = false
sas_sdk = "1000"
sas_sdk_switch = "1"
sas_sls_storage = "100"
sas_webguard_boolean = "1"
sas_webguard_order_num = "100"
status = "Normal"
threat_analysis_switch = "0"
threat_analysis_switch1 = "0"
version_code = "level2"
vul_switch = "0"
}
通過控制台驗證
登入Security Center控制台,請檢查企業版及相關功能是否已成功開通。
登入後,在控制台右上方確認顯示企業版標識,總覽頁中訂用帳戶服務和隨用隨付服務均顯示為已開啟,表示企業版及相關功能已成功開通。
相關文檔
-
關於Terraform產品的更多資訊,請參見瞭解阿里雲Terraform。