Prevent cloud misconfigurations in HashiCorp Terraform with Snyk IaC
We’re delighted to share new features of Snyk Infrastructure as Code (Snyk IaC) designed to support how Terraform users write, plan, and apply their configurations. With Snyk IaC, you can get immediate guidance on security configurations as you write, and scan your Terraform plans in your deployment pipelines to ensure your changes and complete configuration are safe. Plus, we’ve added major performance improvements to handle configurations with thousands of files, and security improvements to ensure your private environment details stay private.
Eliminating Terraform misconfigurations throughout the lifecycle
With these new Snyk IaC capabilities, you have the power to treat Terraform like any other form of code and shift left, making configuration security an integral part of the full Terraform lifecycle. Snyk IaC tests for misconfigurations from the time the code is written, to provide immediate feedback and guidance to your engineers based on cloud and industry best practices and your organization’s policies. As Terraform modules are committed and automated tests begin, Snyk IaC tests your Terraform plans, providing a comprehensive view of the changes being made to ensure your environments will be safe when they are deployed, preventing post-deployments surprises and regression.
The importance of scanning Terraform Plans for cloud misconfigurations
The Snyk CLI already allows users to scan Terraform and Kubernetes files locally and in CI/CD, providing instant feedback as you write and in your code repositories. That works well for individual changes, but if you want to get a collective look at all the changes you’re about to apply, including the effects of your included modules and variables, scanning the Terraform plan file is the way to go.
With this release, the Snyk CLI workflow now supports scanning a Terraform plan (tf-plan) file, to detect if the proposed changes will introduce any new security vulnerabilities to your current cloud infrastructure. To demonstrate how Snyk IaC can now be used throughout your Terraform workflow, I’ll run through an example using the code from Hashicorp’s Use Application Load Balancers for Blue-Green and Canary Deployments tutorial so you can see a simple local “write, test, plan, test, apply” workflow with Snyk IaC.
Testing for cloud misconfigurations in Terraform source code
Before starting with Snyk, I’ve already applied the Terraform as supplied by Hashicorp, so my environment is up and running, and my Terraform code is in git. But now that I have Snyk IaC, I’d like to see if there are any concerning issues, so I’ve integrated Snyk with my GitHub repository and we can see the results in the Snyk console:
There are no high severity issues, but clicking into the
main.tf I notice that the load balancer is set to use
HTTP instead of
HTTPS. A medium risk issue by default, but one that my security team frowns upon. The Snyk UI shows me where the issue exists in my code, the impact, and how to resolve it:
Terraform security scanning as you code
I made the change noted by Snyk IaC to my Terraform file to get the load balancer to use HTTPS, which also requires the creation and use of a certificate, so I added that to my configuration too. Now, before I push my changes I want to run a quick check to make sure that fixing the HTTPS issue didn’t introduce any new problems. And while I’m at it, since this is an internet-facing application, my team and I don’t need to see that “Load balancer is internet facing” alert from Snyk IaC anymore, so I’ve requested the security admins turn it off for me, which is easy to do in Snyk IaC. My only changes were in
main.tf so that’s what I’ll test locally before I commit my changes:
$ snyk iac test main.tf Testing main.tf... Infrastructure as code issues: Organization: purple-dobie Type: Terraform Target file: main.tf Project name: learn-terraform-advanced-deployments Open source: no Project path: main.tf Tested main.tf for known issues, found 0 issues
Perfect! I fixed the HTTPS issue and my security team disabled the “internet facing” check so I’m down to zero issues, at least in
main.tf. I’m happy with that. Time to deploy my changes!
Scanning for Terraform misconfigurations in your pipelines
Before actually applying my changes, I need to run a Terraform plan so I know what’s going to change. Since the Terraform plan incorporates changes across all modules — and includes all of our variables as well — we get a better picture of the impact of our changes. This presents another opportunity to integrate Snyk IaC into our Terraform lifecycle, ensuring that my changes don’t impact other modules in my configuration. In the example below, I’m running everything locally for simplicity, but it’s easy enough to use the Snyk CLI in any CI tools you might use to automate planning and testing:
# generate a Terraform plan in JSON format: $ terraform plan -out=tfplan.bin $ terraform show --json tfplan.bin > tfplan.json # And use Snyk to test the plan to see I caused any issues: $ snyk iac test tfplan.json Testing tfplan.json... Infrastructure as code issues: Organization: purple-dobie Type: Terraform Target file: tfplan.json Project name: learn-terraform-advanced-deployments Open source: no Project path: tfplan.json Tested tfplan.json for known issues, found 0 issues
Looks like my changes are good — no security issues! Except there’s one nagging thought that’s bothering me. All I tested above was the resource changes (the default for Snyk IaC), and I know I only edited a single file (
main.tf), so this clean result isn’t surprising. However, my Terraform configuration was already applied and running before I started using Snyk IaC, so I’d really like to check all the planned values so I can see not only my new changes, but also the known configuration details for the already running deployment.
Enriching cloud misconfiguration checks by incorporating Terraform state
The new Snyk IaC tf-plan feature has the ability to check the planned values for me! I just need to specify that I want to scan the planned values using the
--scan=planned-values option. Again, I recommend doing this in CI, but I’m using the CLI for simplicity:
$ snyk iac test --scan=planned-values tfplan.json Testing tfplan.json... Infrastructure as code issues: ✗ Security Group allows open ingress [High Severity] [SNYK-CC-TF-1] in VPC introduced by resource > aws_security_group[this_name_prefix] > ingress . . . ✗ Non-Encrypted root block device [Medium Severity] [SNYK-CC-TF-53] in EC2 introduced by aws_instance[blue] > root_block_device > encrypted . . . Organization: purple-dobie Type: Terraform Target file: tfplan.json Project name: learn-terraform-advanced-deployments Open source: no Project path: tfplan.json Tested tfplan.json for known issues, found 20 issues
Oops! We have 20 issues this time, with some `High Severity` issues mixed in (I’ve cut out all but one high and one medium issue for brevity in the output above).
Terraform scanning including modules and variables
This new result tells me there’s more work I might need to do, but there’s something else interesting that comes as the result of scanning the tf-plan, beyond just scanning the files: Those high severity issues didn’t come up in my initial git repo scan at the beginning of this example, nor did they appear when I scanned the
main.tf file by itself after I edited it.
The reason for this is that our configuration is using several AWS provider modules and variables, and in the repo and single file scans, we’re statically checking the file, which means we only have details on the resources that are explicitly specified in the file but we cannot tell what resources will be created by the modules simply by looking at the file on its own. The Terraform plan takes care of incorporating all the logic from the included modules and variables for us, so we can get a more complete picture of our configuration.
Another benefit of being able to scan the Terraform plan is that tools in the Terraform ecosystem should easily work with Snyk IaC, too. We have customers using tools like Terraform Cloud, Terragrunt and Atlantis, and since these tools all output a standard Terraform plan, we can scan them with Snyk IaC. We’ll cover examples of some of these ecosystem tools in future posts.
Keeping your private infrastructure data confidential and boosting performance
As shown in even our simple example above, scanning a Terraform plan covers many additional misconfiguration risks that come up in the normal lifecycle of IaC. But scanning at this stage also involves handling sensitive data that we know you do not want to leave your network. So in this release, the Snyk CLI runs IaC tests locally, with nothing but your Snyk authentication and CLI metadata sent back to us. There is no special step or setup you need to undergo for this to work: it’s built-in to Snyk IaC and is the default way the Snyk CLI for IaC will work from here.
There’s another bonus to scanning locally: a huge boost to speed! This is particularly impactful for our customers who have hundreds and in some cases thousands of files in a single Terraform repo. Our own testing shows a 20x improvement when scanning these large directories.
Get started with the Snyk IaC for free
These new features are available to everyone to try, including users on a free plan. Getting started is simple:
- If you don’t already have a Snyk account, sign up for free.
- Install the Snyk CLI. If you already have the Snyk CLI installed, verify you have CLI 1.594.0 or newer to get these features.
- Scan your Terraform and Kubernetes files.
- The main command for scanning IaC with the new features is:
snyk iac test <path-to-file-or-folder>
You can learn more about the various scanning options we support through our docs or via the CLI docs by running:
snyk iac --help
- To scan Terraform plans, you’ll need to generate a JSON formatted output which you can get via
terraform show, as shown in the examples above and documented by Terraform.
- The main command for scanning IaC with the new features is:
We’d love to hear your feedback! If you’re already a Snyk customer your customer success team is a great route for product feedback, but we welcome comments in our Snyk Community forum from all.
Prevent cloud misconfigurations
Start using Snyk IaC to secure your Terraform configs.