Terraform is an Infrastructure as Code (IaC) tool that lets you define, provision, and manage Simple Log Service (SLS) resources through declarative configuration files. You can version-control, reuse, and share your SLS infrastructure setup instead of manually creating resources in the console.
Core concepts
Terraform uses providers and modules as building blocks. Alibaba Cloud publishes SLS-related providers and modules on the Terraform Registry.
Providers
A provider is a Terraform plugin that interacts with the APIs of a cloud platform. To manage SLS resources, use the Alibaba Cloud Provider (alicloud). It supports SLS projects, Logstores, machine groups, Logtail configurations, dashboards, alerts, and other resources.
Modules
A module is a reusable package of Terraform configuration files that encapsulates a set of related resources. Modules help you standardize your SLS infrastructure and share configurations across teams. Browse available SLS modules on the Terraform Registry.
Workflow
To manage SLS resources with Terraform, follow this general workflow:
-
Install Terraform
Install the Terraform command-line interface (CLI) on your local machine, or use Terraform Explorer or Cloud Shell for a browser-based environment. For more information, see Install and configure Terraform locally.
-
Write a configuration file
Create a configuration file (for example,
main.tf) and define the provider and the SLS resources that you want to create. The following example declares the Alibaba Cloud Provider and creates an SLS project:provider "alicloud" { region = "cn-hangzhou" } resource "alicloud_log_project" "example" { project_name = "my-sls-project" description = "SLS project managed by Terraform" } -
Initialize the working directory
Run
terraform initin the directory that contains your configuration file. Terraform downloads the specified provider plugin and initializes the working directory. -
Preview changes
Run
terraform planto compare the current state with your configuration. Terraform displays the actions it will take (create, modify, or destroy resources) without making actual changes. -
Apply changes
Run
terraform applyto execute the planned changes. Terraform calls the Alibaba Cloud API to create or modify the specified SLS resources.
Prerequisites
Before you use Terraform to manage SLS resources, make sure that the following prerequisites are met:
-
SLS is activated. For more information, see Resource management overview.
-
A Terraform runtime environment is ready. You can set up Terraform by using one of the following methods:
-
Terraform Explorer: An online environment provided by Alibaba Cloud. No installation required. Suitable for quick experiments and debugging.
-
Cloud Shell: Preinstalled with Terraform and preconfigured with your identity credentials. Suitable for fast, low-cost access to Terraform.
-
Install and configure Terraform locally: Suitable when network connectivity is limited or you need a custom development environment.
-
Supported SLS resources
The Alibaba Cloud Provider supports the following SLS resources. For the complete list and parameter references, see the Alibaba Cloud Provider documentation on the Terraform Registry.
|
Terraform resource |
Description |
|
|
An SLS project. |
|
|
A Logstore within a project. |
|
|
A machine group for Logtail log collection. |
|
|
A Logtail configuration that defines log collection rules. |
|
|
Binds a Logtail configuration to a machine group. |
|
|
An alert rule for an SLS project. |
|
|
A dashboard for visualizing log data. |
|
|
A Log Audit Service configuration. |