Skip to main content

Artificial Intelligence

AI-Driven Auto-Tagging of EC2 Instances Using Amazon SageMaker

Istock 1628553826

Managing cloud infrastructure effectively requires consistent and meaningful tagging of resources. Manual tagging is prone to errors and difficult to scale. In this blog, I’ll show you how to use Amazon SageMaker and Python to automatically apply intelligent tags to your EC2 instances using either rule-based logic or AI-powered enhancements.

What You Will Learn

  • How to use SageMaker Studio to run Python scripts for EC2 auto-tagging
  • How to set up SageMaker Domains and user profiles
  • How to securely grant EC2 permissions to SageMaker
  • How to fetch EC2 metadata and apply intelligent tag logic

Why This Is Needed

  • Manual tagging doesn’t scale across large environments
  • Lack of consistent tags causes billing, visibility, and compliance issues
  • AI can intelligently assign tags based on patterns in instance metadata

How It Works

  1. Launch SageMaker Studio (requires a domain and user profile)
  2. Use a notebook to run a Python script that fetches all EC2 instances
  3. Apply simple rule-based or AI-enhanced logic
  4. Use Boto3 to update tags automatically

Pre-requisites

  • AWS account
  • Existing EC2 instances
  • SageMaker execution role with permissions:
    • ec2:DescribeInstances
    • ec2:CreateTags
  • Familiarity with Python and basic AWS concepts

Architecture

Architecture Auto Tag Sagemaker

Step-by-Step Guide

1. Create a SageMaker Domain and User

  1. Go to the AWS Console → Amazon SageMaker → Domains
  2. Click “Create domain.”
  3. Use IAM authentication
  4. Create a user profile (e.g., ai-user)
  5. Choose ml.t3.medium for the Studio instance type
  6. Click Create and wait for provisioning

Sagemaker Ai

2. Add Permissions to the Execution Role

  1. Go to IAM Console → Roles
  2. Search and select the SageMaker execution role (e.g., AmazonSageMaker-ExecutionRole-*)
  3. Attach the AmazonEC2ReadOnlyAccess policy
  4. Add inline permissions for ec2:CreateTags if needed

Iam Role Picup

3. Launch SageMaker Studio

  1. Open the SageMaker domain
  2. Click on your user profile → Open Studio
  3. In Studio, go to File → New → Notebook
  4. Choose the Python 3 (Data Science) kernel

Studio CreateNoteboon CreateJupiter Lab Create

Python Scripting

Python CodeJupiter Note Book

Validate the Output

  • Go to EC2 Console → Instances
  • Check the Tags tab
  • You should see Environment and Tagged By tags

Results

Conclusion

This process leverages the power of SageMaker and Python to auto-tag EC2 instances. It enhances consistency, reduces manual effort, and lays the foundation for ML-driven infrastructure management.

Future improvements can include utilizing Amazon Comprehend or Bedrock for more intelligent tag inference, or automating the process using Lambda and EventBridge.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Karthikeyan Mahalingam

Karthikeyan Mahalingam is a senior technical consultant at Perficient with around 7 years of experience in the IT industry. He specializes in Multi-Cloud Services, including Linux, AWS, GCP, Azure, DevSecOps, Kubernetes, Terraform, Git, Pulumi, and AWS AI.

More from this Author

Follow Us