详情页标题前

阿里云对象存储OSS使用Terraform管理OSS-云淘科技

详情页1

本文介绍Terraform的安装和配置详情,以及如何使用Terraform来管理OSS。

安装并配置Terraform

使用Terraform前,您需要按照以下步骤安装并配置Terraform。

  1. 前往Terraform官网下载适用于您的操作系统的程序包。本文以Linux系统为例。
  2. 将程序包解压到/usr/local/bin。如果将可执行文件解压到其他目录,则需要将路径加入到全局变量。
  3. 执行以下命令验证是否已成功安装Terraform。
    terraform

    成功返回示例如下。

    Usage: terraform [-version] [-help]  [args]
  4. 创建RAM用户,并为其授权。 重要 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户的AccessKey配置Terraform工具。
    1. 登录RAM控制台。
    2. 创建名为Terraform的RAM用户,并为该用户创建 AccessKey。具体步骤参见创建RAM用户。
    3. 为RAM用户授权。您可以根据实际的情况为Terraform授予合适的管理权限。具体步骤参见为RAM用户授权。
  5. 执行以下命令为Terraform项目创建工作目录。重要 每个Terraform项目都需要1个独立的工作目录。
    mkdir terraform-test
  6. 执行以下命令进入工作目录terraform-test。
    cd terraform-test

    Terraform在执行时,会读取该目录空间下所有*.tf和*.tfvars文件。因此,您可以按照实际用途将配置信息写入到不同的文件中。常用配置文件如下。

    文件 说明
    provider.tf provider配置
    terraform.tfvars 配置provider要用到的变量
    variable.tf 通用变量
    resource.tf 资源定义
    data.tf 包文件定义
    output.tf 输出

    本文以provider配置文件provider.tf为例。

  7. 执行以下命令创建身份认证信息配置文件provider.tf。
    vim provider.tf

    配置文件信息示例如下。

    provider "alicloud" {
        region           = "cn-beijing"
        access_key  = "LTA**********NO2"
        secret_key   = "MOk8x0*********************wwff"
    }
  8. 执行以下命令初始化工作目录terraform-test。重要 每个Terraform项目在新建Terraform工作目录并创建配置文件后,都需要初始化工作目录。
    terraform init

    成功返回示例如下。

    Initializing provider plugins...
    - Checking for available provider plugins on https://releases.hashicorp.com...
    - Downloading plugin for provider "alicloud" (1.25.0)...
    
    
    
    
    The following providers do not have any version constraints in configuration,
    so the latest version was installed.
    
    
    To prevent automatic upgrades to new major versions that may contain breaking
    changes, it is recommended to add version = "..." constraints to the
    corresponding provider blocks in configuration, with the constraint strings
    suggested below.
    
    
    * provider.alicloud: version = "~> 1.25"
    
    
    Terraform has been successfully initialized!
    
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.            

使用Terraform管理OSS

Terraform安装完成之后,您就可以通过Terraform的操作命令管理OSS了,下面介绍几个常用的操作命令。

terraform plan

terraform plan用于预览将要执行的操作。该命令允许您在正式执行配置文件之前,查看将要执行哪些操作。

使用terraform plan预览创建Bucket的操作示例如下。

  1. 执行以下命令创建配置文件test.tf。
    vim test.tf

    配置文件信息示例如下。

    resource "alicloud_oss_bucket" "bucket-acl"{
      bucket = "demo-2023"
      acl = "private"
    }
  2. 执行以下命令查看将会执行的操作。
    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:                
          acl:               "private"
          bucket:            "demo-2023"
          creation_date:     
          extranet_endpoint: 
          intranet_endpoint: 
          location:          
          logging_isenable:  "true"
          owner:             
          referer_config.#:  
          storage_class:     
    
    
    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

terraform apply用于执行工作目录中的配置文件。

使用terraform apply创建Bucket的操作示例如下。

  1. 执行以下命令创建配置文件test.tf。
    vim test.tf

    配置文件信息示例如下。

    resource "alicloud_oss_bucket" "bucket-acl"{
      bucket = "demo-2023"
      acl = "private"
    }
  2. 执行以下命令执行配置文件。
    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:                
          acl:               "private"
          bucket:            "demo-2023"
          creation_date:     
          extranet_endpoint: 
          intranet_endpoint: 
          location:          
          logging_isenable:  "true"
          owner:             
          referer_config.#:  
          storage_class:     
    
    
    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:            "" => "demo-2023"
      creation_date:     "" => ""
      extranet_endpoint: "" => ""
      intranet_endpoint: "" => ""
      location:          "" => ""
      logging_isenable:  "" => "true"
      owner:             "" => ""
      referer_config.#:  "" => ""
      storage_class:     "" => ""
    alicloud_oss_bucket.bucket-acl: Creation complete after 1s (ID: demo-2023)
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

    说明 此配置执行后,如果demo-2023这个Bucket不存在,则创建一个Bucket。如果已存在,且为Terraform创建的空Bucket,则会删除原有Bucket并重新生成。

terraform destroy

terraform destroy可删除通过Terraform创建的空Bucket。

使用terraform import删除通过Terraform创建的空Bucket的操作示例如下。

  1. 执行以下命令创建配置文件test.tf。
    vim test.tf

    配置文件信息示例如下。

    resource "alicloud_oss_bucket" "bucket-acl"{
      bucket = "demo-2023"
      acl = "private"
    }
  2. 执行以下命令执行配置文件。
    terraform destory

    成功返回示例如下。

    Terraform used the selected providers to generate the following execution plan.
    Resource actions are indicated with the following symbols:
      - destroy
    
    Terraform will perform the following actions:
    
      # alicloud_oss_bucket.bucket-acl will be destroyed
      - resource "alicloud_oss_bucket" "bucket-acl" {
          - acl               = "private" -> null
          - bucket            = "demo-2023" -> null
          - creation_date     = "2023-01-04" -> null
          - extranet_endpoint = "oss-cn-hangzhou.aliyuncs.com" -> null
          - force_destroy     = false -> null
          - id                = "demo-2023" -> null
          - intranet_endpoint = "oss-cn-hangzhou-internal.aliyuncs.com" -> null
          - location          = "oss-cn-hangzhou" -> null
          - owner             = "1379***" -> null
          - redundancy_type   = "LRS" -> null
          - storage_class     = "Standard" -> null
          - tags              = {} -> null
        }
    
    Plan: 0 to add, 0 to change, 1 to destroy.
    
    Do you really want to destroy all resources?
      Terraform will destroy all your managed infrastructure, as shown above.
      There is no undo. Only 'yes' will be accepted to confirm.
    
      Enter a value: yes
    
    alicloud_oss_bucket.bucket-acl: Destroying... [id=demo-2023]
    alicloud_oss_bucket.bucket-acl: Destruction complete after 2s
    
    Destroy complete! Resources: 1 destroyed.

terraform import

如果Bucket不是通过Terraform创建,可通过terraform import导入现有的Bucket。

使用terraform import导入Bucket的操作示例如下。

  1. 执行以下命令创建配置文件。
    vim main.tf

    配置文件信息示例如下。

    resource "alicloud_oss_bucket" "bucket" { 
     bucket = "aliyundoc-demo" 
     acl = "private"
    }
  2. 执行以下命令执行配置文件。
    terraform import alicloud_oss_bucket.bucket aliyundoc-demo

    成功返回示例如下。

    alicloud_oss_bucket.bucket: Importing from ID "aliyundoc-demo"...
    alicloud_oss_bucket.bucket: Import prepared!
      Prepared alicloud_oss_bucket for import
    alicloud_oss_bucket.bucket: Refreshing state... [id=aliyundoc-demo]
    
    Import successful!
    
    The resources that were imported are shown above. These resources are now in
    your Terraform state and will henceforth be managed by Terraform.

相关文档

  • 更多Bucket配置操作示例,请参见alicloud_oss_bucket。
  • 更多Object配置操作示例,请参见alicloud_oss_bucket_object。

内容没看懂? 不太想学习?想快速解决? 有偿解决: 联系专家

阿里云企业补贴进行中: 马上申请

腾讯云限时活动1折起,即将结束: 马上收藏

同尘科技为腾讯云授权服务中心。

购买腾讯云产品享受折上折,更有现金返利:同意关联,立享优惠

转转请注明出处:https://www.yunxiaoer.com/159162.html

(0)
上一篇 2023年12月10日
下一篇 2023年12月10日
详情页2

相关推荐

  • 阿里云容器服务ACK使用抢占式实例运行Job任务-云淘科技

    抢占式ECI实例特别适合于Job任务或者无状态应用,可以有效地节约实例使用成本。本文介绍如何使用抢占式ECI实例来运行Job任务。 背景信息 使用抢占式实例前,请了解以下相关信息: 抢占式实例的市场价格随供需变化而浮动,您需要在创建实例时指定出价模式,当市场价格低于出价且资源库存充足时,就能成功创建抢占式实例。 抢占式实例创建成功后有一个小时的保护期,超出保…

    阿里云容器服务 2023年12月10日
  • 阿里云ECS云服务器Packer的DevOps配置-云淘科技

    本文提供了在阿里云ECS使用Packer创建自定义镜像的DevOps(开发运维一体化)常用配置,适用于使用Packer创建ECS自定义镜像的场景。 镜像标签 字段名称:tags{“key”:”value”}。 适用场景:当您的自定义镜像达到一定的数量时,适当的标记镜像有利于镜像管理和检索。例如记录镜像版本号和镜…

    阿里云服务器 2023年12月9日
  • 阿里云对象存储OSSBrowser.js快速入门-云淘科技

    本文介绍如何在Browser.js SDK中快速使用OSS服务,包括上传文件、下载文件和查看文件列表等。 前提条件 已安装Browser.js SDK。具体操作,请参见Browser.js安装。 已配置跨域资源共享(CORS)规则。具体操作,请参见准备工作。 注意事项 目前浏览器中不允许执行Bucket相关的操作,仅允许执行Object相关的操作,例如Put…

    阿里云对象存储 2023年12月10日
  • 信息流广告,信息流部分建议宽度830px,只针对默认列表样式,顺序随机
  • 阿里云大数据开发治理平台 DataWorksPolarDB数据源-云淘科技

    PolarDB数据源为您提供读取和写入PolarDB双向通道的功能,您可以通过向导模式和脚本模式配置同步任务。 使用限制 离线读写 支持读取视图表。 实时读 来源数据源为阿里云PolarDB MySQL时,您需要开启Binlog。阿里云PolarDB MySQL是一款完全兼容MySQL的云原生数据库,默认使用了更高级别的物理日志代替Binlog,但为了更好地…

    2023年12月10日
  • 阿里云对象存储OSSJava-云淘科技

    本文以Java语言为例,讲解在服务端通过Java代码完成签名,并且设置上传回调,然后通过表单直传数据到OSS。 前提条件 应用服务器对应的域名可通过公网访问。 确保应用服务器已安装Java 1.6以上版本(执行命令java -version进行查看)。 确保PC端浏览器支持JavaScript。 步骤1:配置应用服务器 下载应用服务器源码(Java版本)。 …

    2023年12月10日

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
本站为广大会员提供阿里云、腾讯云、华为云、百度云等一线大厂的购买,续费优惠,保证底价,买贵退差。