This topic describes how to install, configure, and use Terraform to manage Object Storage Service (OSS).
Install and configure Terraform
Before you use Terraform, perform the following steps to install and configure Terraform:
You can use Terraform after you complete the preceding operations.
Use Terraform to manage OSS
After Terraform is installed, you can run Terraform commands to manage OSS. The following
examples provide a description of some common commands of Terraform:
- terraform plan: You can run this command to view the operations to be performed before a configuration
file is executed.
Assume that you add a configuration file named test.tf that is used to create a bucket.
[root@test terraform-test]#vim test.tf resource "alicloud_oss_bucket" "bucket-acl"{ bucket = "figo-chen-2020" acl = "private" }
You can run the terraform plan command to view the operations to be performed based on the test.tf configuration file.[root@test terraform-test]# terraform plan Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ------------------------------------------------------------------------ An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: + alicloud_oss_bucket.bucket-acl id: <computed> acl: "private" bucket: "figo-chen-2020" creation_date: <computed> extranet_endpoint: <computed> intranet_endpoint: <computed> location: <computed> logging_isenable: "true" owner: <computed> referer_config.#: <computed> storage_class: <computed> Plan: 1 to add, 0 to change, 0 to destroy. ------------------------------------------------------------------------ Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.
- terraform apply: You can run this command to execute a configuration file in the working directory.
For example, to create a bucket named figo-chen-2020, you must add a configuration file named test.tf.
[root@test terraform-test]#vim test.tf resource "alicloud_oss_bucket" "bucket-acl"{ bucket = "figo-chen-2020" acl = "private" }
Then, run the terraform apply command to execute the configuration file.
[root@test terraform-test]#terraform apply An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: + alicloud_oss_bucket.bucket-acl id: <computed> acl: "private" bucket: "figo-chen-2020" creation_date: <computed> extranet_endpoint: <computed> intranet_endpoint: <computed> location: <computed> logging_isenable: "true" owner: <computed> referer_config.#: <computed> storage_class: <computed> Plan: 1 to add, 0 to change, 0 to destroy. 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 alicloud_oss_bucket.bucket-acl: Creating... acl: "" => "private" bucket: "" => "figo-chen-2020" creation_date: "" => "<computed>" extranet_endpoint: "" => "<computed>" intranet_endpoint: "" => "<computed>" location: "" => "<computed>" logging_isenable: "" => "true" owner: "" => "<computed>" referer_config.#: "" => "<computed>" storage_class: "" => "<computed>" alicloud_oss_bucket.bucket-acl: Creation complete after 1s (ID: figo-chen-2020) Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Note After you execute the configuration file, a new bucket is created if the figo-chen-2020 bucket does not exist. If the figo-chen-2020 bucket exists and contains no data, the existing bucket is overwritten by the new bucket. - terraform destroy: You can run this command to delete an empty bucket created by Terraform.
- terraform import : If a bucket is not created by using Terraform, you can run this command to import
an existing bucket.
Before you run this command, run the following command to create a file named main.tf and write information about the existing bucket to the file:
[root@test terraform-test]#vim main.tf resource "alicloud_oss_bucket" "bucket" { bucket = "test-hangzhou-2025" acl = "private" }
Run the following command to import the test-hangzhou-2025 bucket:
terraform import alicloud_oss_bucket.bucket test-hangzhou-2025
References
- For more information about how to configure buckets by using Terraform, visit alicloud_oss_bucket.
- For more information about how to configure objects by using Terraform, visit alicloud_oss_bucket_object.