本篇简述如何使用 terraform 中 debug 除错
赛後文章会整理放到个人的部落格上 http://chechia.net/
在 terraform 过程中,我们会遇到许多错误
最多的是 hcl 设定,也就是编辑 .tf 内容与 terraform 互动时,所遇到的错误
其次是 state 的错误
terraform core error
provider error
更多细节可以参考hashicorp learn 文件中建议的 trouble shooting
以上面这篇文章作为范例,hashicorp 建议的 debug 步骤
for_each
由於 hcl 的错误是最常见的,也就是使用者 .tf 写错,因此 debug 时我们会先 validate .tf code
其中 fmt, validate 很常使用,因此可以整合到工作流程中
有些时候,terraform 可以正常运作不出错,可是产生结果不如我们预期,可能就是 .tf 参数写错。然而,复杂module 中的参数十分复杂很难除错,这时我们会使用 terraform console 协助
我们使用用到烂的 azure/foundation/compute_network
做范例,如果今天 subnet 建立出来後,发现参数怪怪的(ex. address space 很怪)
cd azure/foundation/compute_network
terragrunt console
>
> help
The Terraform console allows you to experiment with Terraform interpolations.
You may access resources in the state (if you have one) just as you would
from a configuration. For example: "aws_instance.foo.id" would evaluate
to the ID of "aws_instance.foo" if it exists in your state.
Type in the interpolation to test and hit <enter> to see the result.
To exit the console, type "exit" and hit <enter>, or use Control-C or
Control-D.
进入 console 後,显示互动式令令列
可以印出带入的 input 参数
> module.network
{
"vnet_address_space" = tolist([
"10.2.0.0/16",
])
"vnet_id" = "/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet"
"vnet_location" = "southeastasia"
"vnet_name" = "acctvnet"
"vnet_subnets" = [
"/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-1",
"/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-2",
"/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet/subnets/dev-3",
]
}
可以印出 object 的 output,例如 module.network 的 output.vnet_id
> module.network.vnet_id
"/subscriptions/6fce7237-7e8e-4053-8e7d-ecf8a7c392ce/resourceGroups/terraform-30-days/providers/Microsoft.Network/virtualNetworks/acctvnet"
也可以在 console 中做实验
for
expression,或是 for_each
count
meta-argument 的产物> 1+6
7
> [for k in [1,2,3,4,5] : k]
[
1,
2,
3,
4,
5,
]
结束後 exit console,释放 state lock
> exit
Releasing state lock. This may take a few moments...
如果检查变数都没问题,但结果仍然有问题,我们就会怀疑是 provider,或是外部条件(网路环境)甚至是 core 的错误,这时可以打开 terraform debug log 来检查
TF_LOG
支援的的 log level,TRACE, DEBUG, INFO, WARN or ERRORcd azure/foundation/compute_network
export TF_LOG=DEBUG
TF_LOG=DEBUG terragrunt plan | tee plan_debug.log
...
vim plan_debug.log
内容有 terraform 自身更核心的 log,以及与 public cloud 互动的资讯
上面都是 terraform debug,由於使用 terragrunt 做了很多事情,我们有时也会因为错误的设定而需要 debug terragrunt
我们可以调整 terragrunt log level 或是启用 debug mode
terragrunt apply --terragrunt-log-level debug --terragrunt-debug
<<: Day28. Blue Prism最安全的管家 -BP自动登入Gmail
现况 以YOLOv4模型框选中文字後,将资料集(约7万张)区分为以下类别: 1.1 word(仅有1...
身为一个商业设计的转职者,从懵懂到认识 HTML 与 CSS 之後,接着来到进入 JavaScrip...
115. Distinct Subsequences https://leetcode.com/pr...
一、RPA是什麽? RPA 是 Robotic Process Automation的缩写,简称机器...
大家好,我高中生 姜义新 本次课程大纲 在本次的it帮铁人竞赛,我将会以swift及SwitchUI...