This topic describes how to install, configure, and use Terraform to manage 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 steps.
Use Terraform to manage OSS
After Terraform is installed, you can run Terraform commands to manage OSS. Some common
command:
- terraform plan: You can run this command to view the operations to be performed by a configuration
file.
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 by 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.
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" }
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: You can run this command to import a bucket that is not created
by Terraform.
To run this command, create a file named main.tf and write information about the bucket:
[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 bucket configuration examples, see alicloud_oss_bucket.
- For more information about object configuration examples, see alicloud_oss_bucket_object.