使用Terraform管理OSS
本文介绍 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。
- 导入 Bucket:若 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。
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论