本篇延续 Terragrunt 的功能,介绍
课程内容与代码会放在 Github 上: https://github.com/chechiachang/terraform-30-days
赛後文章会整理放到个人的部落格上 http://chechia.net/
工程师应该注意 infrastructure 的安全性,然而并非人人都是资安专业背景,不一定都能捉到设定上资安风险。这时就要依赖外部的检查资料库,根据常见的安全性错误进行检查。tfsec 是一个很好的免费开源工具,针对 terraform 的 .tf 档案,针对 plan 直接进行分析,挑出安全性错误。底下介绍如何搭配 terragrunt 使用 tfsec。
sudo port install tfsec
sudo brew install tfsec
tfsec --version
0.57.0
找寻 root module 直接运行 tfsec
tfsec .
tfsec --exclude-downloaded-modules
为了避免以上的问题,可以使用 terragrunt before hook。这里我们随意拿一个 root module 作为范例
# azure/foundation/compute_network/terragrunt.hcl
terraform {
...
before_hook "tfsec" {
commands = ["apply", "plan"]
execute = ["tfsec", "."]
}
}
实际的效果
# azure/foundation/compute_network/terragrunt.hcl
cd azure/foundation/compute_network
terragrunt apply
Initializing modules...
Initializing the backend...
Initializing provider plugins...
Terraform has been successfully initialized!
INFO[0010] Executing hook: before_hook prefix=[/Users/che-chia/my-workspace/terraform-30-days/azure/foundation/compute_network]
times
------------------------------------------
disk i/o 2.952161ms
parsing HCL 29.157µs
evaluating values 1.307087ms
running checks 1.486024ms
counts
------------------------------------------
files loaded 7
blocks 8
evaluated blocks 26
modules 1
module blocks 18
results
------------------------------------------
critical 0
high 0
medium 0
low 0
ignored 0
No problems detected!
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.
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
只要每一次 apply 前,都会进行 tfsec
如果希望所有
使用注意 hook 与 git pre-commit hook,会需要时间,影响开发效率,可以依据团队的状况做调整
范例是 after hook,只要每一次 apply 完成後,做一次 tfsec,让工程师测试时就可以检查安全性问题
# azure/terragrunt.hcl
terraform {
...
after_hook "tfsec" {
commands = ["apply", "plan"]
execute = ["tfsec", "."]
}
}
tfsec 检查清单可以到 tfsec.dev 查阅。所有的检查内容都有附上原因,风险说明,问题范例以及改进范例。例如 azure storage 设定风险
tfsec 就是社群维护的安全守则,善用 tfsec 可以实现安全最佳实践,也同时提升自己的资安知识。
当扫出问题时,我们不一定能马上解决,也许是排时程稍後再修理,也许是被其他因素影响,暂时无法改正。然而每次扫瞄 tfsec 还是会跳出警告。收到警告,但大家又不会马上修改,就会无谓的警告,浪费团队的精神能量,消耗无谓的注意力。tfsec 提供 ignored 标记
在 Terraform 中,我们会 change directory 到一个一个 root module 中去执行 init, plan, apply 等工作。当 root module 数量很多的时候,这件事就变得很复杂。这时可以利用 Terragrunt 提供同时多 workspace 执行 的功能,一次控制多个 root module。例如以 azure/dev 为例,资料夹树状结构如下
tree azure/dev
.
├── env.tfvars
├── japanwest
└── southeastasia
├── container_registry
│ └── terragrunt.hcl
└── env.tfvars
cd foundation
terragrunt run-all init
#terragrunt run-all init --reconfigure
INFO[0000] Stack at /Users/che-chia/my-workspace/terraform-30-days/azure/foundation:
=> Module /Users/che-chia/my-workspace/terraform-30-days/azure/foundation/compute_network (excluded: false, dependencies: [])
=> Module /Users/che-chia/my-workspace/terraform-30-days/azure/foundation/service_principal (excluded: false, dependencies: [])
=> Module /Users/che-chia/my-workspace/terraform-30-days/azure/foundation/southeastasia/terraform_backend (excluded: false, dependencies: [])
Initializing modules...
Initializing the backend...
Initializing the backend...
Initializing the backend...
也可以进行 run-all plan
terragrunt run-all plan
INFO[0000] Stack at /Users/che-chia/my-workspace/terraform-30-days/azure/dev:
=> Module /Users/che-chia/my-workspace/terraform-30-days/azure/dev/japanwest/container_registry (excluded: false, dependencies: [])
=> Module /Users/che-chia/my-workspace/terraform-30-days/azure/dev/southeastasia/container_registry (excluded: false, dependencies: [])
...
Plan: 1 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ registry_login_server = (known after apply)
...
Plan: 1 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ registry_login_server = (known after apply)
然而,run-all 搭配 apply,要特别注意
terragrunt run-all apply
INFO[0000] Stack at /Users/che-chia/my-workspace/terraform-30-days/azure/dev:
=> Module /Users/che-chia/my-workspace/terraform-30-days/azure/dev/japanwest/container_registry (excluded: false, dependencies: [])
=> Module /Users/che-chia/my-workspace/terraform-30-days/azure/dev/southeastasia/container_registry (excluded: false, dependencies: [])
Are you sure you want to run 'terragrunt apply' in each folder of the stack described above? (y/n)
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
有些 module 其实是有依赖性,需要依照先後顺序 apply 到公有云上,後面的 module 才能够正常 provision。例如 compute VM 其实依赖 compute network 的 subnet id,才能把 compute VM 的 ip 分配到 subnet 上。实际的例子请见 azure/dev/southeastasia/chechia_net/compute
# azure/dev/southeastasia/chechia_net/compute/terragrunt.hcl
dependency "network"{
config_path = find_in_parent_folders("azure/foundation/compute_network")
}
inputs = {
...
vnet_subnet_id = dependency.network.outputs.vnet_subnets[0] # dev-1
}
inputs 中有个参数是 vnet_subnet_id
意思是这台 VM 应该使用指定的 subnet
这一个先後的动作,就是元件的依赖性
使用 terraform ,terraform 有提供 module depends_on
的 meta-argument,让 .tf 中可以描述 module 与 module 之间的关系
terragrunt 中提供的 dependency,进一步将这层依赖性,推广到 root module 之间也能建立依赖性,Terragrunt modules dependency,具体是有什麽差异:
config_path
指向
find_in_parent_folders
来寻找azure/foundation/compute_network
module 中的 output
vnet_subnet_id
azure/foundation/compute_network
module 要能 output 需要的值建立起这层依赖後对於 terragrunt 工作流程的影响是
chechia_net/compute
前,会先去取得 foundation/compute_network
state 中的 output
foundation/compute_network
state 中有发现问题,则 chechia_net/compute
的 plan 与 apply 会终止
Pros
Cons
实务建议:dependency 请适量使用,不用全部有关的依赖都套用上去,也不要都不用
在完成基础的表单画面後,接着需要将之前完成的爬虫功能整合至网站。 考量功能的独立性、扩充性和使用便利...
我们在Day 7的时候曾经介绍过基於达尔文物竞天择适者生存的演化式计算,那麽今天开始我们就来详细的介...
最近邻居法是什麽? 简称KNN,讲人话就是在现有历史资料的基础上,对於想预测的新资料,直接比对特徵最...
File message 应用 如果想写一个分析使用者传送的文章或是文件档的 Line bot,可以...
时间time模组 使用前要先import time Time()可以传回自1970/1/1以来的秒...