使用Terraform的后端来使用S3

将”terraform.tfstate”文件上传到S3的步骤

版本信息 xī)

Terraform版本为v0.11.13

操作步骤

创建S3存储桶

可以使用管理控制台创建(创建方式无所谓)。由于官方建议,应启用Bucket版本控制。

创建 DynamoDB(非必须)

为了避免多人同时编辑S3存储桶,需要创建一个DynamoDB。
同样,可以立即通过管理控制台创建它。
请注意,在创建时必须将主键命名为”LockID”。

在Configuration文件中写入backend信息。

假设以下设定文件已经完成了设定以及认证信息文件。

provider "aws" {
  region = "ap-northeast-1"
}

terraform {
  backend "s3" {
    bucket = "S3バケット名"               # 適宜変更
    key = "terraform.tfstate"
    encrypt = true                       # 適宜変更
    region = "ap-northeast-1"            # 適宜変更
    dynamodb_table = "DynamoDBテーブル名"  # 適宜変更
  }
}

...

配置后台的设定

$ terraform init

沉迷其中

如果在后端设置中使用变量,会出现这样的错误。看起来,后端设置部分在变量展开之前就被加载了。目前似乎无法解决。

$ terraform init                                      

Initializing the backend...                                                                                                                         
Error loading backend config: 1 error(s) occurred:       

* terraform.backend: configuration cannot contain interpolations                                                                                    

The backend configuration is loaded by Terraform extremely early, before     
the core of Terraform can be initialized. This is necessary because the backend
dictates the behavior of that core. The core is what handles interpolation
processing. Because of this, interpolations cannot be used in backend    
configuration.                                                                 

If you'd like to parameterize backend configuration, we recommend using                             
partial configuration with the "-backend-config" flag to "terraform init".
bannerAds