All Products
Search
Document Center

Terraform:Manage a bucket by using Terraform

Last Updated:Nov 04, 2025

This topic describes how to use Terraform to create a bucket and configure its properties, such as static website hosting, logging, and lifecycle rules.

Note

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

Prerequisites

Procedure

  1. Create a bucket.

    1. Create the terraform.tf file, enter the following content, and save the file to the current working directory.

      Note

      Create a separate working directory for each Terraform project.

      Note

      You can run this sample code with a single click. Run now

      provider "alicloud" {
        alias  = "bj-prod"
        region = "cn-beijing"
      }
      
      resource "random_uuid" "default" {} 
      
      resource "alicloud_oss_bucket" "bucket-new" {
        storage_class = "Standard"
        bucket        = "bucket-auto-2024${random_uuid.default.id}"
      }
      
      resource "alicloud_oss_bucket_acl" "default" {
        bucket = alicloud_oss_bucket.bucket-new.bucket
        acl    = "public-read"
      }
      
      output "buckname"{
        value = alicloud_oss_bucket.bucket-new.bucket
      }
    2. Run the terraform apply command. A log similar to the following output indicates that the bucket is created.

      Note

      OSS bucket names must be globally unique. If the ErrorCode=BucketAlreadyExists error is returned, the bucket name is already in use. Change the name and retry.

      alicloud_oss_bucket.bucket-new: Creating...
      alicloud_oss_bucket.bucket-new: Creation complete after 2s [id=bucket-20200310-1]
      
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  2. Set the bucket properties.

    1. Create the resource.tf file, enter the following content, and save the file to the current working directory.

      Note

      Terraform automatically loads all .tf files in the directory. You can write configuration information to different files as needed.

      resource "alicloud_oss_bucket" "bucket-attr" {
        provider = alicloud.bj-prod
      
        bucket = "bucket-2-${random_uuid.default.id}"
        # Default homepage and 404 page of the static website
        website {
          index_document = "index.html"
          error_document = "error.html"
        }
        # Path where access logs are stored
        logging {
          target_bucket = alicloud_oss_bucket.bucket-new.id
          target_prefix = "log/"
        }
        # Object lifecycle rule
        lifecycle_rule {
          id      = "expirationByDays"
          prefix  = "path/expirationByDays"
          enabled = true
      
          expiration {
            days = 365
          }
        }
        # Hotlink protection settings
        referer_config {
          allow_empty = true
          referers    = ["http://www.aliyun.com", "https://www.aliyun.com", "http://?.aliyun.com"]
        }
      }
    2. Run the terraform apply command to configure the bucket properties. A log similar to the following output indicates that the properties are configured.

      alicloud_oss_bucket.bucket-attr: Creating...
      alicloud_oss_bucket.bucket-attr: Creation complete after 2s [id=bucket-20200310-2]
      
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.