本文介绍了如何使用Terraform创建一个存储空间,并设置存储空间属性用来控制静态网站托管、日志、生命周期等。

前提条件

在开始之前,请您确保完成以下操作:

操作步骤

  1. 创建一个Bucket。
    1. 创建terraform.tf文件,输入以下内容,并保存在当前执行的目录中。
      说明 需要为每个Terraform项目创建一个独立的执行目录。
      provider "alicloud" {
        alias  = "bj-prod"
        region = "cn-beijing"
      }
      
      resource "alicloud_oss_bucket" "bucket-new" {
        provider = alicloud.bj-prod
      
        bucket = var.bucket-new
        acl    = var.acl-bj
      }
    2. 运行terraform apply开始创建。出现类似下面的日志,则表示创建成功。
      alicloud_oss_bucket.bucket-new: Creating...
        acl:               "" => "acl-bj"
        bucket:            "" => "bucket-new"
        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-new: Creation complete after 1s (ID: bucket-vmeixme)
      
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  2. 设置Bucket属性。
    1. 创建resource.tf文件,输入以下内容,并保存在当前执行的目录中。
      说明 该目录下所有*.tf 文件都会被terraform自动加载。因此,用户可以按照实际用途将配置信息写入不同的文件中。
      resource "alicloud_oss_bucket" "bucket-attr" {
        provider = alicloud.bj-prod
      
        bucket = var.bucket-attr
      
        website {
          index_document = var.index-doc
          error_document = var.error-doc
        }
      
        logging {
          target_bucket = alicloud_oss_bucket.bucket-new.id
          target_prefix = var.target-prefix
        }
      
        lifecycle_rule {
          id      = var.rule-days
          prefix  = "${var.rule-prefix}/${var.role-days}"
          enabled = true
      
          expiration {
            days = var.rule-days
          }
        }
      
        lifecycle_rule {
          id      = var.role-date
          prefix  = "${var.rule-prefix}/${var.role-date}"
          enabled = true
      
          expiration {
            date = var.rule-date
          }
        }
      
        referer_config {
          allow_empty = var.allow-empty
          referers    = [var.referers]
        }
      }
    2. 运行terraform apply开始配置Bucket的属性。出现类似下面的日志,说明配置成功。
      alicloud_oss_bucket.bucket-new2: Creating...
        acl:                                         "" => "private"
        bucket:                                      "" => "bucket-attr"
        creation_date:                               "" => "<computed>"
        extranet_endpoint:                           "" => "<computed>"
        intranet_endpoint:                           "" => "<computed>"
        lifecycle_rule.#:                            "" => "2"
        lifecycle_rule.0.enabled:                    "" => "true"
        lifecycle_rule.0.expiration.#:               "" => "1"
        lifecycle_rule.0.expiration.3672460294.date: "" => ""
        lifecycle_rule.0.expiration.3672460294.days: "" => "365"
        lifecycle_rule.0.id:                         "" => "365"
        lifecycle_rule.0.prefix:                     "" => "path/365"
        lifecycle_rule.1.enabled:                    "" => "true"
        lifecycle_rule.1.expiration.#:               "" => "1"
        lifecycle_rule.1.expiration.3998705666.date: "" => "2018-01-01"
        lifecycle_rule.1.expiration.3998705666.days: "" => ""
        lifecycle_rule.1.id:                         "" => "expirationByDate"
        lifecycle_rule.1.prefix:                     "" => "path/expirationByDate"
        location:                                    "" => "<computed>"
        logging.#:                                   "" => "1"
        logging.0.target_bucket:                     "" => "bucket-attr"
        logging.0.target_prefix:                     "" => "log/"
        logging_isenable:                            "" => "true"
        owner:                                       "" => "<computed>"
        referer_config.#:                            "" => "<computed>"
        storage_class:                               "" => "<computed>"
        website.#:                                   "" => "1"
        website.0.error_document:                    "" => "error.html"
        website.0.index_document:                    "" => "index.html"
      alicloud_oss_bucket.bucket-new2: Creation complete after 1s (ID: bucket-attr)
      
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.