This tool allows you to easily tag any AWS resource in your account. It simplifies the process of applying tags to multiple resources across AWS services, making it especially useful for managing and organizing your infrastructure at scale.
Note: This project uses uv to manage dependencies.
- Tag Resources Easily: Apply tags to any AWS resource by simply providing a list of resources and the tags to be applied.
- Support for Multiple Input Formats: Handle different input formats, such as CSV files, ARNs, and others, by using custom parsers.
- Extendable: Add your own parsers and taggers with minimal effort.
- Automated Resource Identification: The tool automatically identifies the AWS service, region, account ID, and resource name from ARNs.
- Tagger instances caching: The tool caches tagger instances to avoid creating multiple instances for the same service.
To install the project and its dependencies, simply run:
pip install -r requirements.txtTo tag AWS resources, you need to pass a list of resources and tags to the script. You can provide the resources in a variety of formats, such as a CSV file or a plain ARN list. Additionally, you can specify the tags in a JSON file for simplicity.
Example: Tagging resources from a CSV file:
uv run main.py resources.csv tags.json --parser wizWhere:
resources.csvis a file containing the list of resources to be tagged.tags.jsonis a file containing the tags to be applied, in the following format:
[
{
"Key": "Environment",
"Value": "Production"
},
{
"Key": "Department",
"Value": "DevOps"
}
]- WIZ generated CSV Parser: Handles CSV files generated by WIZ. (Use
--parser wiz, default parser) - You can easily add your own custom parsers by creating a new class that follows the interface pattern.
This tool is designed to be extensible. Adding your own parsers and taggers is straightforward:
- Custom Parsers: Create a class that implements the Parser interface. Your parser class should have a method to read the input file and return a list of AWS resources (ARNs).
- Custom Taggers: Create a class that implements the Tagger interface. Your tagger class should handle the logic to apply tags to the resources.
Once created, simply register your parser or tagger with the tool, and it will be available for use.
Example: Creating a custom parser for a new input format:
from .base import BaseParser
from .registry import ParserRegistry
@ParserRegistry.register('custom') # Register the parser with the tool
class CustomParser(BaseParser):
@staticmethod
def parse(input_file: str) -> list:
resources = [] # List of resources (ARNs)
# Your logic to read the input file and extract resources
return resourcesExample: Creating a custom tager for a new AWS service:
import boto3
from .base import AwsResourceTagger
from .registry import TaggerRegistry
from utils.arn_parser import AWSArnParser
# Concrete class for tagging EC2 Resources
@TaggerRegistry.register("ec2")
class EC2Tagger(AwsResourceTagger):
def __init__(self, region: str):
self.ec2 = boto3.client('ec2', region_name=region)
def tag_resource(self, arn: str, tags: list):
try:
self.ec2.create_tags(Resources=[AWSArnParser.get_resource_id(arn)], Tags=tags)
except Exception as e:
print(f"Error tagging {arn}: {e}")Here’s how you can tag AWS resources using a CSV file and a custom set of tags:
uv run main.py resources.csv tags.json --parser wizresources.csv: A file containing AWS resources (ARNs) to be tagged.tags.json: A JSON file containing the tags in the expected format.--parser wiz: Specifies the parser to use for processing the input file (in this case, wiz format).
We encourage contributions to this project! If you have ideas for new parsers, taggers, or other improvements, feel free to submit a pull request.
- Fork the repository.
- Create a new branch for your feature (git checkout -b feature-name).
- Make your changes and commit them (git commit -am 'Add new feature').
- Push to the branch (git push origin feature-name).
- Open a pull request.
- Create a new Python file inside the parsers or taggers directory.
- Define a class that implements the appropriate interface (Parser or Tagger).
- Register your class using the register_parser or register_tagger function, as appropriate.
This project employs several design patterns to ensure flexibility, extensibility, and maintainability:
- Factory Pattern: Used for the dynamic creation of parsers and taggers based on user input. This allows the system to easily accommodate new types of parsers and taggers without changing the core logic.
- Strategy Pattern: Defines different parsing and tagging strategies (such as tagging EC2, S3, etc.), which can be easily switched and customized based on the resource type.
- Singleton Pattern: Ensures only one instance of each parser and tagger registries exists, preventing duplication and ensuring consistent behavior across the system.
These patterns work together to keep the project modular, maintainable, and easy to extend for future requirements.
classDiagram
class AwsResourceTagger {
<<interface>>
+__init__(region: String) : void
+tag_resource(arn: String, tags: List) : void
}
class ACMTagger {
-acm: boto3.client
+__init__(region: String) : void
+tag_resource(arn: String, tags: List) : void
}
class MoreTaggers {
-tagger: boto3.client
+__init__(region: String) : void
+tag_resource(arn: String, tags: List) : void
}
class TaggerRegistry {
-_taggers: dict
-_instances: dict
+register(name: String) : callable
+get_tagger(resource_type: String, region: String) : AwsResourceTagger
}
AwsResourceTagger <|-- ACMTagger
AwsResourceTagger <|-- MoreTaggers
TaggerRegistry ..> AwsResourceTagger : "manage instances of"