Simple Terraform Linter is a lightweight command-line tool for checking Terraform configuration files against custom rules. It supports input from Terraform folders or JSON files, with easy customization and extensibility through user-defined rules defined in YAML files.
Project description
Terraform Linter CLI
This is a simple Terraform Linter CLI that checks your Terraform configuration files for compliance against specified rules. It supports input from Terraform folders or JSON files, and various rule types.
Features
- Lint Terraform configurations in a folder or JSON file.
- Specify custom rule sets via YAML files.
- Add, modify or remove rules to suit your requirements.
- Easily extend the linter with additional custom rules.
Installation
- Make sure you have Python 3 installed on your computer.
- Install the Terraform Linter CLI package using pip:
pip install simpleTFLinter
Usage
After installing the simpleTFLinter package, you can use the simple_tf_linter command to run the linter.
- Set up your rule YAML file with the desired rules. You can either create your own or use the default
.tf_linter_rule.yamlprovided in theexamplesdirectory. - Run the Terraform Linter CLI using the following command:
simple_tf_linter -p [input_path] -r [rule_yaml_path] -t [input_type]
input_path: Path to the folder containing Terraform code or the JSON file you want to lint.rule_yaml_path: Path to the rule YAML fileinput_type: Input type, eithertf-folderfor a Terraform folder orjson-filefor a JSON file.
Example:
simple_tf_linter -p ./my_terraform_folder -r ./rule_file.yaml -t tf-folder
This will lint the Terraform files in my_terraform_folder using the rules specified in rule_file.yaml.
Generating a JSON file from a Terraform Plan
You can also lint a JSON file generated from your Terraform plan. To create a JSON file, follow the steps below:
- Initialize your Terraform working directory:
terraform init
- Create a Terraform plan and save it to a file:
terraform plan -out tf.plan
- Convert the Terraform plan to JSON format and save it to a file using the
terraform show -jsoncommand, and optionally usejqto pretty-print the JSON:
terraform show -json tf.plan | jq > tf.json
Now you have a JSON file tf.json containing your Terraform plan, which can be used as an input for the Terraform Linter CLI.
To lint the JSON file, run the following command:
simple_tf_linter -p ./tf.json -r ./rule_file.yaml -t json-file
This will lint the tf.json file using the rules specified in rule_file.yaml.
Custom Rules
You can add, modify or remove rules in the rule YAML file. Each rule consists of the following properties:
key: The key to check.regex: The regular expression to match against the value of the key.name: (Optional) The name of the rule.description: (Optional) A brief description of the rule.file_path: (Optional) The file path where the rule should be applied. You can use*to match one level in the path and**to match zero, one, or multiple levels in the path.dict_path: (Optional) The dictionary path where the key should be checked. Similar tofile_path, use*to match one level and**to match zero, one, or multiple levels in the path.match_is_good: (Optional) Set toTrueif the value should match the regex; set toFalseif the value should not match the regex.
Example of a rule in YAML format:
- key: aws_instance_type
regex: ^t2\..+$
name: rule_for_aws_instance_type
description: Check if the instance
type is t2.*
file_path: "**/*.tf"
dict_path: resource/*/aws_instance_type
match_is_good: true
In this example, the file_path is set to **/*.tf, which means that the rule should be applied to any .tf file at any level of the directory structure. The dict_path is set to resource/*/aws_instance_type, which means that the rule should be checked on the aws_instance_type key within any single level of the resource dictionary.
This rule checks if the aws_instance_type key has a value that starts with t2. and any characters following it.
Extending the Linter
To create custom rules, you can extend the Rule class and implement the check method. Then, register the rule with the TerraformLinter instance.
Example:
class CustomRule(Rule):
def __init__(self, ...):
super().__init__(name, description)
# Initialize your rule properties here.
def check(self, parsed_data, file_name, path, ignore_check_file_path=False):
# Implement your custom rule logic and return the result.
pass
linter = TerraformLinter()
custom_rule = CustomRule(...)
linter.register_rule(custom_rule)
For more examples of custom rules, refer to the source code provided.
Popular Regex Patterns
Here are some popular regex patterns that can be used in the rule YAML file:
- For
trusted-arn:
- key: trusted-arn
regex: "arn:aws:iam::\\d{12}:role(?:/.+)?$"
- For
stgdomain:
- key: stg-domain
regex: "\b(?:[A-Za-z0-9_]*\.*\s*)*stg\.[A-Za-z0-9_.-]+"
match_is_good: False
Add these rules to your rule YAML file to easily check for these patterns in your Terraform configurations.
Testing
The Terraform Linter CLI comes with a test suite to ensure its functionality. To run the tests, first, make sure you have pytest installed. If not, install it using:
pip install pytest
Then, run the tests using the following command:
pytest tests
This will execute the test suite and display the results.
Contributing
If you find any issues or have suggestions for improvements, feel free to submit an issue or create a pull request. Your feedback is always welcome!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file SimpleTFLinter-0.2.2.tar.gz.
File metadata
- Download URL: SimpleTFLinter-0.2.2.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fcd7279c33840a577d2c6136556a5c68f0378142deed9728807e0495b9511acf
|
|
| MD5 |
9c7add57d4f0df00b089905ddbc630f8
|
|
| BLAKE2b-256 |
76064973b6d9d9e8a5d4079bd05a5fde85da3ea9010313c7eb3fc18cd52db1db
|
File details
Details for the file SimpleTFLinter-0.2.2-py3-none-any.whl.
File metadata
- Download URL: SimpleTFLinter-0.2.2-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7831653874de332406945f34aa2368afb0baafb9ad425908722fe5444457d32c
|
|
| MD5 |
7a58ab0132a20fe4343cf8c80c42a008
|
|
| BLAKE2b-256 |
c6a95ae094ea79e750b740a4ddda67405069806145446e639b3a3c53546dcec0
|