本文介绍Terraform的安装和配置详情,以及如何使用Terraform来管理OSS。
安装和配置Terraform
使用Terraform前,您需要按照以下步骤安装并配置Terraform。
以上操作完成之后,您就可以使用Terraform工具了。
使用Terraform管理OSS
Terraform安装完成之后,您就可以通过Terraform 的操作命令管理 OSS 了,下面介绍几个常用的操作命令:
- terraform plan:预览功能,允许在正式执行配置文件之前,查看将要执行哪些操作。
例如,您添加了创建Bucket的配置文件 test.tf:
[root@test terraform-test]#vim test.tf resource "alicloud_oss_bucket" "bucket-acl"{ bucket = "figo-chen-2020" acl = "private" }
使用 terraform plan可查看到将会执行的操作。[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:执行工作目录中的配置文件。
例如您想创建名为 figo-chen-2020的Bucket,您需要先添加创建 Bucket 的配置文件 test.tf。
[root@test terraform-test]#vim test.tf resource "alicloud_oss_bucket" "bucket-acl"{ bucket = "figo-chen-2020" acl = "private" }
之后使用terraform apply命令执行配置文件即可。
[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.
说明 此配置运行后,如果 figo-chen-2020这个Bucket不存在,则创建一个Bucket;如果已存在,且为Terraform创建的空Bucket,则会删除原有 Bucket 并重新生成。 - terraform destroy:可删除通过Terraform创建的空Bucket。
- terraform import :如果Bucket不是通过Terraform 创建,可通过命令导入现有的Bucket。
首先,创建名为 main.tf 的文件,并写入Bucket相关信息:
[root@test terraform-test]#vim main.tf resource "alicloud_oss_bucket" "bucket" { bucket = "test-hangzhou-2025" acl = "private" }
使用如下命令导入test-hangzhou-2025这个Bucket:
terraform import alicloud_oss_bucket.bucket test-hangzhou-2025
参考文档
- 更多Bucket配置操作示例请参见alicloud_oss_bucket。
- 更多Object相关配置操作示例请参见alicloud_oss_bucket_object。