Skip to content
Ercan Ermis
Ercan Ermis

notes for everyone about cloud technology

  • Cloud
    • AWS
    • GCP
  • Container
    • Kubernetes
    • Docker
  • Linux
  • DevOps
  • Privacy Policy
  • Contact
Ercan Ermis

notes for everyone about cloud technology

Automating AWS CloudWatch Log Group Tagging with Python and Boto3

Ercan, April 16, 2025

Managing tags for AWS CloudWatch log groups is crucial for operational visibility, cost management, and effective resource organization. Tagging log groups manually can be cumbersome, especially when dealing with a large number of log groups. This article outlines a straightforward method to automate this task using Python and the AWS SDK for Python (Boto3).

Importance of Automating CloudWatch Log Group Tagging

Automation ensures:

  • Consistent tagging across your AWS resources.
  • Reduced manual effort and human errors.
  • Enhanced ability to track costs and usage accurately.

Prerequisites

  • Python 3 installed
  • AWS CLI configured with appropriate permissions
  • Boto3 (pip install boto3)
  • IAM permissions:
    • logs:DescribeLogGroups
    • logs:ListTagsLogGroup
    • logs:TagLogGroup

Python Script for Tagging CloudWatch Log Groups

Below is a Python script that automatically applies specific tags to AWS CloudWatch log groups that currently have no tags.

Python Script

import boto3

AWS_REGION = 'us-east-1'  # Replace with your region

# Tags to apply
TAGS_TO_APPLY = {
    'provisioned': 'manual',
    'saas_env': 'dev4',
    'projectid': 'APP-30328',
}

def tag_cloudwatch_log_groups():
    logs_client = boto3.client('logs', region_name=AWS_REGION)

    paginator = logs_client.get_paginator('describe_log_groups')
    for page in paginator.paginate():
        for log_group in page['logGroups']:
            log_group_name = log_group['logGroupName']
            existing_tags = logs_client.list_tags_log_group(logGroupName=log_group_name).get('tags', {})

            if not existing_tags:
                print(f"Adding tags to log group '{log_group_name}'")
                logs_client.tag_log_group(
                    logGroupName=log_group_name,
                    tags=TAGS_TO_APPLY
                )
            else:
                print(f"Log group '{log_group_name}' already has tags. Skipping.")

if __name__ == '__main__':
    tag_cloudwatch_log_groups()

This script will iterate through all your CloudWatch log groups, applying tags only to log groups that currently have none.

Advantages

  • Simplified resource management
  • Enhanced cost allocation and auditing
  • Streamlined operational tasks

Conclusion

Automating CloudWatch log group tagging using Python and Boto3 simplifies administrative tasks and helps maintain a well-organized and cost-effective AWS environment.

Share on Social Media
x facebook linkedin reddit
AWS

Post navigation

Previous post
  • AWS (45)
    • Serverless (4)
  • Best (9)
  • DevOps (16)
  • Docker (10)
  • GCP (3)
  • Kubernetes (3)
  • Linux (13)
  • Uncategorized (6)

Recent Posts

  • Automating AWS CloudWatch Log Group Tagging with Python and Boto3
  • Automating AWS ECR Tagging with Python and Boto3
  • Automating ECR Image Cleanup with Bash
  • Update ECR Repositories with Bash Script
  • Why Automated Tests Are Essential in Your CI/CD Pipeline and Development Flow
  • Streamline Your AWS ECR Management with This Powerful Bash Script
  • Setting up DKIM for Google Workspace (Gmail) using Terraform and AWS Route 53
  • Automate AWS Site-to-Site VPN Monitoring
  • Optimizing Docker Images: Tips for Reducing Image Size and Build Time
  • Monitoring EC2 Disk Space with a Simple Bash Script and Slack Alerts
  • Securing Docker Containers: Best Practices for Container Security
  • Mastering Dockerfile: Writing Efficient, Scalable Container Builds
  • Migrating a Git Repository from GitLab to GitHub with GPG-Signed Commits
  • Accessing AWS Services in Private Subnets Without 0.0.0.0/0
  • Understanding AWS Regions, Availability Zones, and VPCs: A Comprehensive Guide
©2025 Ercan Ermis | WordPress Theme by SuperbThemes