Strengthening the Security of Terraform-Managed Resources with tfsec
This article describes how to use tfsec to strengthen the security of resources managed with Terraform.
Installing tfsec locally
In the example below, I install it using asdf.
asdf supports many languages, and it is useful because it lets you switch versions with the same procedure across languages, rather than relying on per-language version switchers like rubyenv, nodenv, or goenv.
Reference: https://github.com/woneill/asdf-tfsec
1 | asdf plugin-add tfsec https://github.com/woneill/asdf-tfsec |
When you want to exclude a rule for an individual resource
You can exclude a rule on each resource with tfsec:ignore.
1 | resource "aws_lb" "dummy" { |
You can also exclude a rule by adding a comment above the resource as shown below, but
when there are several of them it becomes hard to read, so personally I find it clearer to set them on each individual parameter as shown above.
1 | # tfsec:ignore:aws-elb-alb-not-public インターネットからのアクセスをする為、許容する |
When you want to exclude a rule across the whole project
Reference: https://aquasecurity.github.io/tfsec/v1.27.6/getting-started/configuration/config/
Create .tfsec/config.yml and add the rules you want to exclude.
The following is just a sample; change the excluded rules according to your own operations.
1 | --- |
1 | // The tfsec execution directory is set to ./envs/prd here. |
Running it in GitHub Actions
tfsec will now run triggered by the creation of a Pull Request.
1 | name: tfsec |
Summary
By introducing tfsec, you can address many of the rules that are non-compliant with AWS Config, contributing to building a more secure architecture.
It is even more recommended to set things up so that a Pull Request cannot be merged unless it passes tfsec, since this automatically enforces it as an operational rule.
Also, SaaS services evolve day by day, and tfsec keeps up with them.
Please be careful, as regularly upgrading the tfsec version is essential.
That’s all.
I hope you find it helpful.
Strengthening the Security of Terraform-Managed Resources with tfsec
