Manage OSS buckets using Terraform
Terraform is an open source tool that lets you create and manage cloud resources safely and efficiently. This topic describes how to create a bucket using Terraform.
The sample code in this tutorial supports one-click execution. You can run the code directly. Run now
Prerequisites
To reduce security risks, use a Resource Access Management (RAM) user with the least privilege to perform the operations in this tutorial. For more information, see Create a RAM user and Manage RAM user permissions. The following access policy grants the required permissions:
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": "oss:*", "Resource": "*" } ] }Prepare a Terraform runtime environment. You can use one of the following methods to run Terraform.
Explorer: Alibaba Cloud provides an online runtime environment for Terraform. You do not need to install Terraform. You can log on to use and test Terraform online. This method is suitable for scenarios where you want to quickly and easily test Terraform at no cost.
Cloud Shell: Terraform components are pre-installed in Alibaba Cloud Cloud Shell and identity credentials are automatically 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 easily at a low cost.
Install and configure Terraform locally: This method is suitable for scenarios that have poor network connectivity or require a custom developer environment.
Some resources in this tutorial's examples incur fees. Release the resources when they are no longer needed.
Resources used
alicloud_oss_bucket: A bucket.
alicloud_oss_bucket_acl: The access control list of a bucket.
Create a bucket
Create a working directory. In the directory, create a configuration file named main.tf and copy the following code into 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 list (ACL) of the bucket resource "alicloud_oss_bucket_acl" "bucket-ac"{ bucket = alicloud_oss_bucket.bucket.id acl = "private" }Run the following command to initialize the Terraform environment:
terraform initThe following output indicates that Terraform is initialized.
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.Run the following command to execute the code:
terraform applyWhen prompted, enter
yesand press Enter. Wait for the command to finish. The following output indicates that the code is executed.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.Verify the result.
Run the terraform show command
In the working directory, run the following command to query the details of the resources created by Terraform:
terraform show
Log on to the console
Log on to the Object Storage Service (OSS) console. Click to view the created bucket.

Click the Bucket Name of the bucket that you created in this tutorial. On the tab, you can view the access control list.

Clean up resources
After you no longer need the resources that are created or managed by Terraform, run the following command to release them. For more information about the terraform destroy command, see Common commands.
terraform destroyComplete example
This sample code supports one-click execution. You can run the code directly.Run now
Sample code
To try more examples, go to the product folder in More complete examples.
References
For an introduction to Terraform, see Terraform product introduction.
For more bucket configuration examples, see alicloud_oss_bucket.
If the `terraform init` command times out due to network latency and prevents providers from being downloaded, see Configure a Terraform Init acceleration solution.