Terrascan

Terrascan is a static code analyzer for Infrastructure as Code that detects security vulnerabilities and compliance violations.

Terrascan is a static code analyzer for Infrastructure as Code that detects security vulnerabilities and compliance violations.

Installation

Using Homebrew

brew install terrascan

Using Docker

docker pull accurics/terrascan:latest

Using Binary

curl -L "$(curl -s https://api.github.com/repos/tenable/terrascan/releases/latest | grep -o -E "https://.+?_Linux_x86_64.tar.gz")" > terrascan.tar.gz
tar -xf terrascan.tar.gz terrascan && rm terrascan.tar.gz
sudo install terrascan /usr/local/bin && rm terrascan

Basic Usage

Scan Directory

terrascan scan -d /path/to/terraform/code

Scan with Specific IAC Provider

Output Formats

Configuration

Config File

Create terrascan.toml in your project root:

Policy Categories

  1. Security

    • Access Control

    • Network Security

    • Data Protection

    • Identity Management

  2. Compliance

    • CIS Benchmarks

    • HIPAA

    • PCI

    • SOC 2

  3. Best Practices

    • Resource Configuration

    • Tagging

    • Monitoring

    • Cost Optimization

CI/CD Integration

GitHub Actions

Azure DevOps Pipeline

Pre-commit Hook

Add to .pre-commit-config.yaml:

Writing Custom Policies

Create custom_policy.rego:

Common Security Checks

1. IAM Policy Validation

2. Network Security

Best Practices

  1. Policy Management

    • Use baseline policies

    • Document exceptions

    • Regular policy reviews

    • Version control policies

  2. Scan Configuration

    • Define severity levels

    • Set appropriate thresholds

    • Configure notifications

    • Enable detailed logging

  3. Integration Strategy

    • Early pipeline integration

    • Break builds on high severity

    • Track findings over time

    • Regular reporting

Troubleshooting

  1. Common Issues

    • Policy parsing errors

    • Resource validation failures

    • Rule conflicts

    • Performance problems

  2. Solutions

    • Validate policy syntax

    • Check resource configurations

    • Review rule dependencies

    • Optimize scan scope

Checklist

Last updated