All Products
Search
Document Center

Object Storage Service:Create a bucket with Terraform

Last Updated:Jun 03, 2026

Terraform is an open-source tool for managing cloud resources as code. Create and configure an OSS bucket using Terraform.

Note

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

Prerequisites

Note

Some resources in this tutorial may incur fees. Release them when no longer needed.

Resources used

Create a bucket

  1. Create a directory and a main.tf file. Add the following code to main.tf.

    variable "region"{
      default = "cn-beijing"
    }
    
    provider "alicloud"{
      region = var.region
    }
    
    resource "random_uuid" "default" {
    }
    
    # Create a bucket.
    resource "alicloud_oss_bucket" "bucket" {
      bucket = substr("tf-example-${replace(random_uuid.default.result, "-", "")}", 0, 16)
    }
    
    # Set the access control for the bucket.
    resource "alicloud_oss_bucket_acl" "bucket-ac"{
      bucket = alicloud_oss_bucket.bucket.id
      acl = "private"
    }
    
  2. Initialize the Terraform environment.

    terraform init

    Expected output on success:

    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. Apply the configuration.

    terraform apply

    Enter yes when prompted and press Enter. Expected output on success:

    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.
  4. Verify the results.

    Terraform show

    Run the following command to view the created resources:

    terraform show

    image

    Console

    1. Log on to the OSS console and click Buckets to view your bucket.

      image

    2. Click your Bucket Name, then go to Permission Control > ACL to verify the ACL.

      image

Clean up resources

Run the following command to release resources you no longer need (Common Commands).

terraform destroy

Complete example

Note

You can run the sample code in this example with a single click. Run Now

Sample code

variable "region"{
  default = "cn-beijing"
}

provider "alicloud"{
  region = var.region
}

resource "random_uuid" "default" {
}

# Create a bucket.
resource "alicloud_oss_bucket" "bucket" {
  bucket = substr("tf-example-${replace(random_uuid.default.result, "-", "")}", 0, 16)
}

# Set the access control for the bucket.
resource "alicloud_oss_bucket_acl" "bucket-ac"{
  bucket = alicloud_oss_bucket.bucket.id
  acl = "private"
}

Explore product-specific examples at More complete examples.