Manage OSS buckets using Terraform

更新时间:
复制 MD 格式

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.

Note

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.

Note

Some resources in this tutorial's examples incur fees. Release the resources when they are no longer needed.

Resources used

Create a bucket

  1. 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"
    }
    
  2. Run the following command to initialize the Terraform environment:

    terraform init

    The 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.
  3. Run the following command to execute the code:

    terraform apply

    When prompted, enter yes and 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.
  4. 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

    image

    Log on to the console

    1. Log on to the Object Storage Service (OSS) console. Click Bucket List to view the created bucket.

      image

    2. Click the Bucket Name of the bucket that you created in this tutorial. On the Permission Control > Access Control List tab, you can view the access control list.

      image

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 destroy

Complete example

Note

This sample code supports one-click execution. You can run the code directly.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 list (ACL) of the bucket
resource "alicloud_oss_bucket_acl" "bucket-ac"{
  bucket = alicloud_oss_bucket.bucket.id
  acl = "private"
}

To try more examples, go to the product folder in More complete examples.

References