关于devops:Terraform初探迁移本地项目到Terraform-Cloud执行

8次阅读

共计 4537 个字符,预计需要花费 12 分钟才能阅读完成。

上一篇文章咱们尝试了在本地环境应用 Terraform 来创立和治理 AWS Lightsail 资源,对于治理一些云资源,咱们须要在本地装置相应的 CLI 工具和配置拜访相应云资源的凭据(例如 AWS CLI,AccessKeyID 等),Terraform 通过调用本地的 CLI 工具或者云 API 来治理云资源的状态,其默认应用的是 local 类型的 Backend,资源的状态文件 (.tfstate) 也是保留在本地文件目录中的。
这篇文章咱们将尝试应用 remote 类型的 Backend,将我的项目迁徙到 Terraform Cloud 去执行,并且由 Terraform Cloud 来治理资源状态。

什么是 Terraform Cloud

Terraform Cloud 是一个治理 Terraform 在统一且牢靠的环境中运行的 SaaS 利用,从而能够替换在本地机器是执行 Terraform 我的项目,其存储共享的状态和秘密数据,并能够连贯到版本控制系统(如 Git),使得咱们能够在团队中将基础设施作为代码进行工作。
Terraform 是一个商业利用,团队和商业应用将会收取费用并提供了更多的高级性能。但对于个人用户,能够收费应用根本的性能。对于费用和性能详情,能够参考 (https://www.hashicorp.com/pro…)。

首先咱们须要注册一个 Terraform Cloud 的账号,拜访 https://app.terraform.io/sign…,依据提醒注册要给收费的账号

注册实现后第一次登陆 Terraform Cloud,其会询问如何开始一个我的项目,这里咱们抉择 Start from scratch,也就是将从一个空模板开始

接下来咱们须要创立一个组织(Organization),例如这里我创立一个名为 learn-terraform 的组织,一个组织就相似要给命名空间,能够治理多个工作空间(Workspace),还能够治理其下工作空间共享的变量和环境变量。

接下来咱们须要在本地环境登录 Terraform Cloud,并增加相应的配置来从新初始化我的项目。

从新初始我的项目

实现了 Terraform Cloud 的账号注册之后,咱们须要在本地终端运行 terraform login,会关上浏览器来登录账号失去一个 Token 值,将其复制填入终端实现登录

> terrafrom login
Terraform must now open a web browser to the tokens page for app.terraform.io.

If a browser does not open this automatically, open the following URL to proceed:
    https://app.terraform.io/app/settings/tokens?source=terraform-login


---------------------------------------------------------------------------------

Generate a token using your browser, and copy-paste it into this prompt.

Terraform will store the token in plain text in the following file
for use by subsequent commands:
    /home/mengz/.terraform.d/credentials.tfrc.json

Token for app.terraform.io:
  Enter a value:

接着咱们批改我的项目的配置文件 main.tf,退出 backend "remote"

terraform {
  backend "remtoe" {
    organization = "learn-terraform"
    workspaces {name = "mylightsail"}
  }

  ...
}

执行 terraform init,Terraform 将下载 remote 的插件,连贯至 Terraform Cloud 的 learn-terraform/mylightsail 工作空间,并将本地的状态文件迁徙到云端

$ terraform init

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 "remote" backend. No existing state was found in the newly  configured "remote" backend. Do you want to copy this state to the new "remote"  backend? Enter "yes" to copy and "no" to start with an empty state.
  Enter a value: yes
Releasing state lock. This may take a few moments...
Successfully configured the backend "remote"! Terraform will automaticallyuse this backend unless the backend configuration changes....

浏览器拜访 Terraform Cloud WebUI,进入相应的工作空间能够查看状态信息。

实现之后能够将本地的 .terraform/terraform.tfstate 文件删除。本地我的项目已将 Terraform Cloud 作为近程后端(remote backend),并且关联了命令行 (CLI) 驱动的形式,因而后续能够在本地更新资源配置文件,而后在本地运行 plan & apply 命令,这将会触发在远端 Cloud 上执行具体的状态保护工作。不过要应用 Terraform Cloud 来执行状态保护,咱们还须要将 AWS 的拜访凭据配置到 Terraform Cloud 上。

配置工作空间的环境变量

应用 Terraform Cloud 来保护云资源(例如 AWS),咱们须要配置相应的拜访凭据。这里咱们须要配置 AWS 的 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY 作为我的项目空间的环境变量。
在工作空间点击 Variables 标签页,点击 + Add Varaible 按钮

抉择 Environment Variables,而后增加 AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY 两个环境变量,并设置相应的值。

实现之后,咱们就能够本地控制台运行 terraform planterraform apply 将操作发送到 Terraform Cloud 端去运行,当然咱们还是能够在本地我的项目执行 terraform show 来查看以后的状态,状态将会治理在云端

> terraform plan
Running plan in the remote backend. Output will stream here. Pressing Ctrl-C
will stop streaming the logs, but will not stop the plan running remotely.

Preparing the remote plan...

To view this run in a browser, visit:
https://app.terraform.io/app/mengz-infra/my-lightsail/runs/run-LzwFBbihffEKmucd

Waiting for the plan to start...

Terraform v1.0.11
on linux_amd64
Configuring remote state backend...
Initializing Terraform configuration...
aws_lightsail_static_ip.outline-sig-ip: Refreshing state... [id=Outline-EIP]
aws_lightsail_instance.outline-sig: Refreshing state... [id=Outline-Sig]
aws_lightsail_instance_public_ports.outline-sig-public-ports: Refreshing state... [id=Outline-Sig-987241840]
aws_lightsail_static_ip_attachment.outline-sig-ip-attache: Refreshing state... [id=Outline-EIP]

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.

能够看到打算是在 remote backend 运行的。

本版化治理我的项目

最初,咱们能够将我的项目的配置文件提交到版本控制系统(例如 Gitlab),并配置工作空间的版本控制

在 Terraform Cloud 工作空间的设置里,依照提醒配置关联相应的版本治理代码仓库。实现之后,咱们在本地提交更新的代码后,会主动触发 Terraform Cloud 去执行保护新的状态。不过这将不会容许在本地终端执行 terraform apply

> terraform apply

│ Error: Apply not allowed for workspaces with a VCS connection
│ A workspace that is connected to a VCS requires the VCS-driven workflow to ensure that the VCS remains the single source of truth.

只能通过更新代码,而后提交到近程代码仓库的形式来触发状态保护。这将更加便于与团队共享基础设施代码,以及独特保护基础设施状态,同时也更加趋于 GitOps 的工作形式。

总结

本文基于上一篇文章 – 尝试应用 Terraform 在本地环境治理 AWS Lightsail 资源,延申了将状态治理的操作迁徙到以 Terraform Cloud 作为近程后端的尝试,除了 Terraform Cloud 之后,还有其余类型的 Backend,能够参考 (https://www.terraform.io/docs…)。
自此,咱们初探了应用 Terraform 作为 IaC 工具来治理 AWS Lightsail 资源,当作 Terraform 学习的一个入门。Hashicrop 官网提供了更多的学习资源和文档,想深刻学习 Terrform,并投入到理论工作中,还请参考官网文档。

【同时公布在 Terraform 初探:迁徙本地我的项目到 Terraform Cloud 执行】

正文完
 0