全部文档
当前文档

暂无内容

如果没有找到您期望的内容,请尝试其他搜索词

文档中心

Backend

最近更新时间:2024-01-25 14:14:14

Backend用于设置State的存储机制。Terraform使用State来确定对资源进行哪些更改,其主要目的是实现存储系统中的对象与配置中声明的资源实例之间的绑定,在进行任何操作之前,Terraform都会更新真实资源的状态。

默认情况下,当通过Terraform对资源进行创建、修改等,资源的当前状态信息会存放在当前目录下的terraform.tfstate文件中。但在团队协作中,将State存在本地增加了代码维护的成本。

通过在Terraform中配置Backend,可以将资源状态存放在远端存储服务中,比如KS3、Terraform Cloud、HashiCorp Consul等。通过这种方式,让资源状态脱离本地存储,从而更有利于维护,提高了管理的灵活性。

在KS3中使用Backend步骤

  1. 在Terraform块中添加Backend配置。

terraform {
  backend "s3" {
    bucket   = "likui-test3"
    key      = "terraform/terraform.tfstate"
    region   = "BEIJING"
    endpoint = "https://ks3-cn-beijing.ksyuncs.com"

    skip_region_validation      = true
    skip_metadata_api_check     = true
    skip_credentials_validation = true
    skip_requesting_account_id  = true
    skip_s3_checksum            = true
  }
}

参数说明:

参数名称

说明

bucket

配置存储资源状态的存储桶名称。

key

配置资源状态文件的名称。

region

存储桶所属地区。

endpoint

存储桶的访问域名,注意:域名必须要以https://http://开头。

skip_*

表示跳过部分参数的校验和检查。

  1. 在环境变量中,配置您用于访问KS3的AK和SK,获取AK和SK方法参见文档:获取AK/SK

export AWS_ACCESS_KEY_ID="****"
export AWS_SECRET_ACCESS_KEY="****"

  1. 执行以下命令进行初始化。

terraform init

若输出以下内容则表示成功。

Initializing the backend...

Successfully configured the backend "s3"! Terraform will automatically
use this backend unless the backend configuration changes.

Initializing provider plugins...
......
Terraform has been successfully initialized!

若在本地已有terraform.tfstate文件,则会输出以下内容,提示是否要将该State同步到远端。

Initializing the backend...
Do you want to copy existing state to the new backend?
  Pre-existing state was found while migrating the previous "local" backend to the
  newly configured "s3" backend. No existing state was found in the newly
  configured "s3" backend. Do you want to copy this state to the new "s3"
  backend? Enter "yes" to copy and "no" to start with an empty state.

  Enter a value:

文档导读
纯净模式常规模式

纯净模式

点击可全屏预览文档内容
文档反馈