All Products
Search
Document Center

Simple Log Service:Configure Logtail collection with Terraform

Last Updated:Jun 17, 2026

Terraform is an open-source tool for provisioning and managing cloud infrastructure. You can use Terraform to configure Logtail collection for Simple Log Service.

Note

You can run the sample code in this tutorial with one click. Run now.

Prerequisites

  • Simple Log Service (SLS) is activated. For more information, see Storage resource hierarchy.

  • For security, use a RAM user with the minimum required permissions. For more information, see Create a RAM user and Manage RAM user permissions. The following policy grants the required permissions:

    {
      "Version": "1",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "log:GetProject",
            "log:ListProject",
            "log:CreateProject",
            "log:DeleteProject",
            "log:UpdateProject",
            "log:GetAppliedMachineGroups",
            "log:GetMachineGroup",
            "log:ListMachineGroup",
            "log:CreateMachineGroup",
            "log:GetLogStore",
            "log:GetLogStoreLogs",
            "log:GetLogStoreMeteringMode",
            "log:ListLogStores",
            "log:CreateLogStore",
            "log:PostLogStoreLogs",
            "log:UpdateLogStore",
            "log:GetLogtailPipelineConfig",
            "log:UpdateLogtailPipelineConfig",
            "log:ListTagResources",
            "log:ListShards",
            "log:ListSavedSearch",
            "log:GetIndex",
            "log:ListDashboard",
            "log:ListConfig",
            "log:CreateConfig",
            "log:GetConfig",
            "log:ApplyConfigToGroup",
            "log:DeleteConfig",
            "log:DeleteMachineGroup",
            "log:GetProjectPolicy",
            "log:DeleteLogStore"
          ],
          "Resource": "*"
        }
      ]
    }
  • Prepare a Terraform environment. You can use one of the following methods to run Terraform.

    • Use Terraform in Terraform Explorer: Alibaba Cloud provides an online environment to run Terraform. You do not need to install Terraform. You can log on to use and try Terraform online. This method is suitable for scenarios where you want to try and debug Terraform quickly and conveniently at no cost.

    • Cloud Shell: Alibaba Cloud Cloud Shell has Terraform components pre-installed and identity credentials configured. You can run Terraform commands directly in Cloud Shell. This method is suitable for scenarios where you want to access and use Terraform quickly and conveniently at a low cost.

    • Install and configure Terraform on your local machine: This method is suitable for scenarios with poor network connectivity or when you need a custom development environment.

Note

Some resources in this tutorial incur fees. Release these resources when you no longer need them to avoid unexpected charges.

Resources

Step 1: Create a project

  1. Create a working directory and create a configuration file named main.tf in it. Copy the following code into main.tf.

    variable "region" {
      default = "cn-hangzhou"
    }
    variable "identify_list" {
      type        = list(string)
      description = "The IP addresses of machines in the machine group"
      default     = ["10.0.0.1", "10.0.0.2"]
    }
    provider "alicloud" {
      region = var.region
    }
    resource "random_integer" "default" {
      min = 10000
      max = 99999
    }
    # A project
    resource "alicloud_log_project" "example" {
      project_name = "project-name-${random_integer.default.result}"
      description  = "tf actiontrail example"
    }
  2. Run the following command to initialize the Terraform runtime environment.

    terraform init

    The following output indicates successful initialization.

    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. Run the following command to apply the configuration.

    terraform apply

    When prompted, enter yes and press Enter. The following output indicates successful execution.

    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: 2 added, 0 changed, 0 destroyed.
  4. Verify the result.

    Terraform show

    In the working directory, run the following command to view the resource details:

    terraform show
    # alicloud_log_project.example:
    resource "alicloud_log_project" "example" {
        create_time       = "2024-11-25 14:23:17"
        description       = "tf actiontrail example"
        id                = "project-name-xxx"
        name              = "project-name-xxx"
        project_name      = "project-name-xxx"
        resource_group_id = "rg-xxx"
        status            = "Normal"
    }
    # random_integer.default:
    resource "random_integer" "default" {
        id     = "xxx"
        max    = 99999
        min    = 10000
        result = xxx
    }

    Console

    Log on to the Simple Log Service console to view the project.

Step 2: Create a machine group and a Logstore

  1. Add the following code to the main.tf file.

    # A machine group
    resource "alicloud_log_machine_group" "example" {
      project     = alicloud_log_project.example.project_name
      name          = "terraform-example-${random_integer.default.result}"
      identify_type = "ip"
      topic         = "terraform"
      identify_list = var.identify_list
    }
    # A 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. Create an execution plan to preview the changes.

    terraform plan
  3. Run the following command to apply the configuration.

    terraform apply

    When prompted, enter yes and press Enter. The following output indicates successful execution.

    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  4. Verify the result.

    Terraform show

    In the working directory, run the following command to view the resource details:

    terraform show
    shell@Alicloud:~/log/1log$ terraform show
    # alicloud_log_machine_group.example:
    resource "alicloud_log_machine_group" "example" {
        id             = "project-name-xxx:terraform-example-xxx"
        identify_list  = [
            "10.0.0.1",
            "10.0.0.2",
        ]
        identify_type  = "ip"
        name           = "terraform-example-xxx"
        project        = "project-name-xxx"
        topic          = "terraform"
    }
    # alicloud_log_project.example:
    resource "alicloud_log_project" "example" {
        create_time       = "2024-11-25 14:23:17"
        description       = "tf actiontrail example"
        id                = "project-name-xxx"
        name              = "project-name-xxx"
        project_name      = "project-name-xxx"
        resource_group_id = "rg-xxx"
        status            = "Normal"
        tags              = {}
    }
    # alicloud_log_store.example:
    resource "alicloud_log_store" "example" {
        append_meta          = true
        auto_split           = false
        create_time          = 1732516271
        enable_web_tracking  = false
        id                   = "project-name-xxx:logstore_example_xxx"
        logstore_name        = "logstore_example_xxx"
        max_split_shard_count = 0
        metering_mode        = "ChargeByDataIngest"
        mode                 = "standard"
        name                 = "logstore_example_xxx"
        project              = "project-name-xxx"
        project_name         = "project-name-xxx"
        retention_period     = 3
        shard_count          = 2
    }

    Console

    1. Log on to the Simple Log Service console. In the Projects section, click the project name. In the left-side navigation pane, choose Log Storage to view the Logstore.

    2. In the left-side navigation pane, choose Resources > Machine Groups to view the machine group.

Step 3: Configure Logtail collection

  1. Add the following code to the main.tf file.

    # A Logtail configuration
    resource "alicloud_logtail_config" "example" {
      project     = alicloud_log_project.example.project_name
      logstore    = alicloud_log_store.example.logstore_name
      name        = "config-sample-${random_integer.default.result}"
      input_type  = "file"
      output_type = "LogService"
      input_detail = jsonencode(
      	{
    		"logPath": "/logPath",
    		"filePattern": "access.log",
    		"logType": "json_log",
    		"topicFormat": "default",
    		"discardUnmatch": false,
    		"enableRawLog": true,
    		"fileEncoding": "gbk",
    		"maxDepth": 10
    	}
      )
    }
    # A Logtail attachment
    resource "alicloud_logtail_attachment" "example" {
      project     = alicloud_log_project.example.project_name
      logtail_config_name = alicloud_logtail_config.example.name
      machine_group_name  = alicloud_log_machine_group.example.name
    }
  2. Create an execution plan to preview the changes.

    terraform plan
  3. Run the following command to apply the configuration.

    terraform apply

    When prompted, enter yes and press Enter. The following output indicates successful execution.

    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  4. Verify the result.

    Terraform show

    In the working directory, run the following command to view the resource details:

    terraform show
    # alicloud_logtail_attachment.example:
    resource "alicloud_logtail_attachment" "example" {
        id                 = "project-name-xxx:config-sample-xxx:terraform-example-xxx"
        logtail_config_name = "config-sample-xxx"
        machine_group_name = "terraform-example-xxx"
        project            = "project-name-xxx"
    }
    # alicloud_logtail_config.example:
    resource "alicloud_logtail_config" "example" {
        id             = "project-name-xxx:logstore_example_xxx:config-sample-xxx"
        input_detail   = jsonencode(
            {
                discardUnmatch = false
                enableRawLog   = true
                fileEncoding   = "gbk"
                filePattern    = "access.log"
                logPath        = "/logPath"
                logType        = "json_log"
                maxDepth       = 10
                topicFormat    = "default"
            }
        )
        input_type       = "file"
        last_modify_time = "1732519340"
        logstore         = "logstore_example_xxx"
        name             = "config-sample-xxx"
        output_type      = "LogService"
        project          = "project-name-xxx"
    }

    Console

    1. Log on to the Simple Log Service console. In the Projects section, click the project name. In the left-side navigation pane, choose Log Storage > Logtail Configurations to view the Logtail configuration.

    2. Click the Logtail configuration name and go to the Machine Group Management tab to view the associated machine group.

      In the Applied Machine Groups panel, find the machine group with a name prefixed by terraform-example-. This confirms that the machine group is associated with the Logtail configuration.

Release resources

When you no longer need the resources created in this tutorial, run the following command to release them. For more information about the terraform destroy command, see Common commands.

terraform destroy

Complete example

Note

You can run the sample code in this tutorial with one click. Run now.

Sample code

variable "region" {
  default = "cn-hangzhou"
}
variable "identify_list" {
  type        = list(string)
  description = "The IP addresses of machines in the machine group"
  default     = ["10.0.0.1", "10.0.0.2"]
}
provider "alicloud" {
  region = var.region
}
resource "random_integer" "default" {
  min = 10000
  max = 99999
}
# A project
resource "alicloud_log_project" "example" {
  project_name = "project-name-${random_integer.default.result}"
  description  = "tf actiontrail example"
}
# A machine group
resource "alicloud_log_machine_group" "example" {
  project     = alicloud_log_project.example.project_name
  name          = "terraform-example-${random_integer.default.result}"
  identify_type = "ip"
  topic         = "terraform"
  identify_list = var.identify_list
}
# A 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
}
# A Logtail configuration
resource "alicloud_logtail_config" "example" {
  project     = alicloud_log_project.example.project_name
  logstore    = alicloud_log_store.example.logstore_name
  name        = "config-sample-${random_integer.default.result}"
  input_type  = "file"
  output_type = "LogService"
  input_detail = jsonencode(
  	{
		"logPath": "/logPath",
		"filePattern": "access.log",
		"logType": "json_log",
		"topicFormat": "default",
		"discardUnmatch": false,
		"enableRawLog": true,
		"fileEncoding": "gbk",
		"maxDepth": 10
	}
  )
}
# A Logtail attachment
resource "alicloud_logtail_attachment" "example" {
  project     = alicloud_log_project.example.project_name
  logtail_config_name = alicloud_logtail_config.example.name
  machine_group_name  = alicloud_log_machine_group.example.name
}

For more examples, navigate to the desired product folder in More complete examples.

References