A tool for automatically tagging Infrastructure as Code (IaC) resources with Git commit information.
IAC Tagger helps track the relationship between infrastructure resources and their defining code by automatically adding Git commit information as tags. Currently supports:
- Terraform resources
- Kubernetes manifests
pip install iac-tagger
# Tag a specific file in the current directory
iac-tagger -f filename.tf
# Tag all IaC resources in the current directory
iac-tagger -d .
# Tag resources in a specific directory
iac-tagger -d path/to/iac/files
# Specify custom tag key (default is 'iac_tagger')
iac-tagger . --tag-key CustomGitTag
The tool:
- Scans the specified directory for IaC files (
.tf
,.yaml
,.yml
) - Calculates a hash of each resource's configuration
- Retrieves the latest Git commit information
- Adds or updates tags in the format:
resource_id:config_hash:commit_hash
- AWS resources (including S3, IAM, Neptune, Elasticsearch)
- Azure resources
- GCP resources
Example of a tagged Terraform resource:
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "web-server"
iac_tagger = "aws_instance.web:a1b2c3:d4e5f6"
}
}
- All resource types that support labels/annotations
- Supports ConfigMaps and Deployments
Example of a tagged Kubernetes resource:
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
iac_tagger: "pod/nginx:a1b2c3:d4e5f6"
- Python 3.7+
- Git repository
- Required Python packages (installed automatically):
- setuptools
- pyyaml
- hcl2
- Clone the repository:
git clone https://github.com/yourusername/iac-tagger.git
cd iac-tagger
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install development dependencies:
pip install -e ".[dev]"
iac-tagger/
├── src/
│ └── iac_tagger/
│ ├── __init__.py
│ ├── main.py
│ ├── iac_parser.py
│ ├── kubernetes_parser.py
│ └── terraform_parser.py
├── requirements.txt
└── setup.py
-
Git Repository Not Found
- Ensure you're running the tool within a Git repository
- Check that
.git
directory exists
-
Permission Denied
- Verify write permissions on IaC files
- Run with appropriate permissions
-
Parser Errors
- Ensure valid HCL syntax for Terraform files
- Verify YAML formatting for Kubernetes manifests
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.