上一篇文章咱们尝试了在本地环境应用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 loginTerraform 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 filefor use by subsequent commands: /home/mengz/.terraform.d/credentials.tfrc.jsonToken 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 initInitializing 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: yesReleasing 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_ID
和 AWS_SECRET_ACCESS_KEY
作为我的项目空间的环境变量。
在工作空间点击 Variables 标签页,点击 + Add Varaible 按钮
抉择 Environment Variables ,而后增加 AWS_ACCESS_KEY_ID
和 AWS_SECRET_ACCESS_KEY
两个环境变量,并设置相应的值。
实现之后,咱们就能够本地控制台运行 terraform plan
,terraform apply
将操作发送到Terraform Cloud端去运行,当然咱们还是能够在本地我的项目执行 terraform show
来查看以后的状态,状态将会治理在云端
> terraform planRunning plan in the remote backend. Output will stream here. Pressing Ctrl-Cwill 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-LzwFBbihffEKmucdWaiting for the plan to start...Terraform v1.0.11on linux_amd64Configuring 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 configurationand 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执行】