我试用了Terraform云(Terraform云免费版)

一切始于一个意识到的点。

有一天,我从朋友那里收到了一条Slack消息,内容是“你见过Terraform Cloud吗?”(非常感谢这个引荐!)

那么,当查看Terraform的官方文档时……哎?

スクリーンショット 2019-06-06 15.43.50.png

介绍Terraform云远程状态管理

刚刚还有这样的消息吗?不知道是什么。

我似乎注意到了一个名为Terraform Cloud免费套餐的东西。这是企业的免费版本吗?这是官方的链接。

我立刻注册了。

当我登录时,出现了这样的画面。

我不确定应该做什么,但首先选择”创建新组织”。

スクリーンショット 2019-06-06 15.47.29.png

然后出现了这样的画面,我用名为kei1的名字创建并注册了一个组织。

スクリーンショット 2019-06-06 15.49.32.png

左上方显示着组织名称,但似乎没有工作空间存在…嗯,以后可能会知道吧。

スクリーンショット 2019-06-06 15.50.31.png

我按下右上角的”开始”按钮。然后弹出了这样的模态框。

スクリーンショット 2019-06-06 15.51.21.png

看起来需要做以下事项,部分已经附带了链接。

    • Download Terraform 0.11.13 or later ⇢ クリア済み

 

    • Create your user access token -> これはやる必要あり

 

    • Configure the Terraform Enterprise remote backend -> !? Enterpriseとな!?

 

    Re-initialize your local Terraform workspace with terraform init -> remote stateに切り替えたら terraform init というのは当然なのでそれはそれでやる

创建您的用户访问令牌

似乎可以从https://app.terraform.io/app/settings/tokens创建。屏幕可能会显示如下内容。

スクリーンショット 2019-06-06 15.54.32.png

在 DESCRIPTION栏中输入,然后点击生成令牌按钮。

生成token后请将其复制。
如果不在这里复制,将永远无法再次显示出来,请注意。

配置 Terraform Enterprise 的远程后端

点击链接后跳转到了这里。

注意:我们建议使用Terraform v0.11.13或更新版本与该后端一起使用。该后端需要在app.terraform.io上使用Terraform Enterprise帐号或使用新的Terraform Enterprise私有实例(版本v201809-1或更新版本)。

要么是在app.terraform.io上的Terraform Enterprise帐户,要么是Terraform Enterprise的私有实例。

因为我们正在使用 app.terraform.io 进行作业,所以我们满足了条件,对吧?
当听说需要 Terraform Enterprise 远程后端时,我有点惊讶,是必须要使用 Enterprise 吗?不过我觉得应该是可以的。

在“Get Started”页面的末尾,有一个指向此处的链接。请点击此链接:
https://www.terraform.io/docs/enterprise/free/

据说,需要这样配置后端设置。

首先,按照以下方式编写terraform代码块。

我将文件名设为terraform.tf。

terraform {
  backend "remote" {
    hostname = "app.terraform.io"
    organization = "kei1" // <-- ここはいいとして・・・

    workspaces {
      name = "default" // <-- workspaceなんてまだ作ってないんだからdefaultでいいんかな・・・
    }
  }
}

此外,您可以在自己的主目录中创建一个名为 .terraformrc 的文件,并在其中添加以下配置。

.terraformrc 翻译为中文:terraform配置文件。

credentials "app.terraform.io" {
  token = "Zc37BBcXCC(略)"
}

通过使用 terraform init重新初始化您的本地 Terraform 工作区。

在这种状态下,执行 terraform init。

% terraform init

Initializing the backend...

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

Initializing provider plugins...

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.

看起来似乎很顺利。

我稍微返回屏幕看一下。

スクリーンショット 2019-06-06 16.17.37.png

我明白了。我曾经苦恼于该写什么作为工作空间的名称…但事实并非如此。
原来是所写的工作空间被创建了…这样的啊。
(这与terraform workspace命令有什么关联呢?我得调查一下。)

2019年6月7日 补充说明

在這種情況下,工作空間似乎不被支持。

% terraform workspace list
workspaces not supported

因此,在terraform块中声明的、与本应具有不同含义的workspace,将始终进行操作。
因此,如果想要做类似传统方式的事情,可能唯一的办法是通过某种条件在terraform块内使用三元运算符之类的方式来切换workspace名称,但似乎无法在terraform块的workspaces中使用var…

试试实际使用 terraform apply

那就试着申请看看。

顺便提一下,我用 main.tf 文件写了以下内容。

只需创建一个密钥对tf。

resource "ecl_compute_keypair_v2" "keypair_1" {
  name = "keypair_1"
}
% 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:

  # ecl_compute_keypair_v2.keypair_1 will be created
  + resource "ecl_compute_keypair_v2" "keypair_1" {
      + fingerprint = (known after apply)
      + id          = (known after apply)
      + name        = "keypair_1"
      + private_key = (known after apply)
      + public_key  = (known after apply)
      + region      = (known after apply)
    }

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

ecl_compute_keypair_v2.keypair_1: Creating...
ecl_compute_keypair_v2.keypair_1: Creation complete after 1s [id=keypair_1]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

哦,我只创建了一对键,但顺利创建了。

当我们追随画面,我们能清晰地看到状态的存在。

スクリーンショット 2019-06-06 16.19.19.png

我们来更新一下,好吗?

将main.tf文件修改为以下内容。


resource "ecl_compute_keypair_v2" "keypair_1" {
  name = "keypair_1"
}

resource "ecl_compute_keypair_v2" "keypair_2" {
  name = "keypair_2"
}

我添加了密钥对。

那么,我会申请的。

% terraform apply                                                            (git)-[master]
ecl_compute_keypair_v2.keypair_1: Refreshing state... [id=keypair_1]

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:

  # ecl_compute_keypair_v2.keypair_2 will be created
  + resource "ecl_compute_keypair_v2" "keypair_2" {
      + fingerprint = (known after apply)
      + id          = (known after apply)
      + name        = "keypair_2"
      + private_key = (known after apply)
      + public_key  = (known after apply)
      + region      = (known after apply)
    }

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

ecl_compute_keypair_v2.keypair_2: Creating...
ecl_compute_keypair_v2.keypair_2: Creation complete after 1s [id=keypair_2]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

似乎一切都順利成功了。

国家的状况会是怎样呢?

スクリーンショット 2019-06-06 16.21.47.png

哦,可以看到状态的历史记录啊……

而且似乎还可以突出显示差异部分。

スクリーンショット 2019-06-06 16.28.56.png

我尝试了破坏它,但最终成功地删除了资源,而且使用感也完全一样。

哎呀,真厉害呀。

总结

喔,这个Pulumi好像不需要了吧?因为Terraform单独就能管理包括远程状态历史在内的了。而且还可以进行团队管理。

这只是我试用后的初步感受。

2019年6月7日补记

好像不能与Git之类的工具进行协作。

关于团队/成员,我已经在评论中留下了验证的结果。

bannerAds