赛尔校园公共服务平台 Logo
平台使用
阿里云
百度云
移动云
智算服务
教育生态
登录 →
赛尔校园公共服务平台 Logo
平台使用 阿里云 百度云 移动云 智算服务 教育生态
登录
  1. 首页
  2. 阿里云
  3. 云备份
  4. 开发参考
  5. 使用Terraform管理云备份资源

使用Terraform管理云备份资源

  • 开发参考
  • 发布于 2025-04-22
  • 0 次阅读
文档编辑
文档编辑

Terraform是一种开源工具,用于安全高效地预配和管理云基础结构。你可以使用Terraform管理云备份资源。本文为您演示如何通过Terraform创建一个云备份的备份库。

说明

当前示例代码支持一键运行,您可以直接运行代码。一键运行

前提条件

  • 由于阿里云账号(主账号)具有资源的所有权限,一旦发生泄露将面临重大风险。建议您使用RAM用户,并为该RAM用户创建AccessKey,具体操作方式,请参见创建RAM用户和创建AccessKey。

  • 使用以下示例为RAM用户授权,需要为该RAM用户授予以下权限:管理云备份资源AliyunHBRFullAccess的权限。具体操作方式请参见为RAM用户授权。

    {
        "Version": "1",
        "Statement": [
            {
                "Action": "hbr:*",
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": "hdr:*",
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": [
                    "ecs:DescribeImages",
                    "ecs:DescribeInstances",
                    "ecs:DescribeInstanceAttribute"
                ],
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": [
                    "vpc:DescribeVSwitches",
                    "vpc:DescribeVpcs",
                    "vpc:DescribeEipAddresses",
                    "vpc:DescribeVpcAttribute",
                    "vpc:DescribeZones",
                    "vcp:DescribeVSwitchAttributes"
                ],
                "Resource": "*",
                "Effect": "Allow"
            },
            {
                "Action": [
                    "ram:CreateServiceLinkedRole",
                    "ram:DeleteServiceLinkedRole"
                ],
                "Resource": "*",
                "Effect": "Allow",
                "Condition": {
                    "StringEquals": {
                        "ram:ServiceName": [
                            "dr.hbr.aliyuncs.com",
                            "ecsbackup.hbr.aliyuncs.com",
                            "ossbackup.hbr.aliyuncs.com",
                            "nasbackup.hbr.aliyuncs.com",
                            "csgbackup.hbr.aliyuncs.com",
                            "vaultencryption.hbr.aliyuncs.com",
                            "otsbackup.hbr.aliyuncs.com",
                            "crossbackup.hbr.aliyuncs.com",
                            "ecsencryption.hbr.aliyuncs.com",
                            "magpiebridge.hbr.aliyuncs.com"
                        ]
                    }
                }
            }
        ]
    }
  • 准备Terraform运行环境,您可以选择以下任一方式来使用Terraform。

    在Terraform Explorer中使用Terraform:阿里云提供了Terraform的在线运行环境,您无需安装Terraform,登录后即可在线使用和体验Terraform。适用于零成本、快速、便捷地体验和调试Terraform的场景。

    Cloud Shell:阿里云Cloud Shell中预装了Terraform的组件,并已配置好身份凭证,您可直接在Cloud Shell中运行Terraform的命令。适用于低成本、快速、便捷地访问和使用Terraform的场景。

    在本地安装和配置Terraform:适用于网络连接较差或需要自定义开发环境的场景。

重要

请确保Terraform版本不低于v0.12.28。如需检查现有版本,请运行terraform --version命令。

使用的资源

alicloud_hbr_vault:创建备份库。

使用Terraform创建一个云备份的备份库

本示例将创建一个云备份的备份库。

  1. 创建一个工作目录,并且在工作目录中创建以下名为main.tf的配置文件。main.tfTerraform主文件,定义了将要部署的资源。

    variable "region" {
      default = "cn-hangzhou"
    }
    provider "alicloud" {
      region = var.region
    }
    resource "random_integer" "default" {
      min = 10000
      max = 99999
    }
    # 创建云备份库
    resource "alicloud_hbr_vault" "example" {
      vault_name = "example_value_${random_integer.default.result}"
    }
  2. 执行以下命令,初始化Terraform运行环境。

    terraform init

    返回如下信息,表示Terraform初始化成功。

    Initializing the backend...
    
    Initializing provider plugins...
    - Finding latest version of hashicorp/alicloud...
    - Installing hashicorp/alicloud v1.234.0...
    - Installed hashicorp/alicloud v1.234.0 (signed by HashiCorp)
    
    Terraform has created a lock file .terraform.lock.hcl to record the provider
    selections it made above. Include this file in your version control repository
    so that Terraform can guarantee to make the same selections by default when
    you run "terraform init" in the future.
    
    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.
  3. 创建执行计划,并预览变更。

    terraform plan
  4. 执行以下命令,创建一个云备份的备份库。

    terraform apply

    在执行过程中,根据提示输入yes并按下Enter键,等待命令执行完成,若出现以下信息,则表示创建云备份的备份库成功。

    Plan: 2 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
    
    random_integer.default: Creating...
    random_integer.default: Creation complete after 0s [id=58472]
    alicloud_hbr_vault.example: Creating...
    alicloud_hbr_vault.example: Creation complete after 8s [id=v-00023xlq3mjub7****]
    
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  5. 验证结果

    执行terraform show命令

    您可以使用以下命令查询Terraform已创建的资源详细信息:

    terraform show

    image

    登录云备份控制台

    创建完成后,您可以通过OpenAPI、SDK或者在云备份控制台,可以查看到创建的备份库。

    image

清理资源

当您不再需要上述通过Terraform创建或管理的资源时,请运行以下命令以释放资源。关于terraform destroy的更多信息,请参见Terraform常用命令。

terraform destroy

完整示例

如果您想体验更多完整示例,请前往更多完整示例中对应产品的文件夹查看。

示例代码

variable "region" {
  default = "cn-hangzhou"
}
provider "alicloud" {
  region = var.region
}
resource "random_integer" "default" {
  min = 10000
  max = 99999
}
# 创建云备份库
resource "alicloud_hbr_vault" "example" {
  vault_name = "example_value_${random_integer.default.result}"
}

相关文档

当您遇到由于网络延迟等原因造成的terraform init超时,导致无法正常下载Provider等情况时,请参见Terraform Init 加速方案配置。

相关文章

集成概览 2025-04-22 15:06

云备份提供可视化操作控制台,还提供OpenAPI在线调试、阿里云SDK、

SDK参考 2025-04-22 15:06

云备份提供多种语言SDK,您可以根据业务的实际情况进行集成。

使用Terraform管理云备份资源 2025-04-22 15:06

Terraform是一种开源工具,用于安全高效地预配和管理云基础结构。你可以使用Terraform管理云备份资源。本文为您演示如何通过Terraform创建一个

资源编排ROS集成示例 2025-04-22 15:06

使用资源编排服务ROS调用云备份。本文为您介绍如何编写一个

API概览 2025-04-22 15:06

本产品(云备份 Cloud Backup/2017-09-08)的OpenAPI采用RPC签名风格,签名细节参见签名机制说明。我们已经为开发者封装了常见编程语言的SDK,开发者可通过下载SD

服务接入点 2025-04-22 15:06

亚太

目录
Copyright © 2025 your company All Rights Reserved. Powered by 赛尔网络.
京ICP备14022346号-15
gongan beian 京公网安备11010802041014号