Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use Boto3 to remove tags from AWS Glue Resources
In this article, we will see how to remove tags from AWS Glue Resources using the boto3 library. AWS Glue resources can have tags for organization and cost tracking, and sometimes you need to remove specific tags programmatically.
Problem Statement
Use boto3 library in Python to remove tags from AWS Glue Resources like databases, tables, crawlers, and jobs.
Required Parameters
The untag_resource function requires two main parameters:
- resource_arn ? The Amazon Resource Name (ARN) of the Glue resource
- tags_list ? List of tag keys to remove (format: ["key1", "key2"])
AWS Glue Resource ARN Formats
| Resource Type | ARN Format |
|---|---|
| Catalog | arn:aws:glue:region:account-id:catalog |
| Database | arn:aws:glue:region:account-id:database/database-name |
| Table | arn:aws:glue:region:account-id:table/database-name/table-name |
| Connection | arn:aws:glue:region:account-id:connection/connection-name |
| Crawler | arn:aws:glue:region:account-id:crawler/crawler-name |
| Job | arn:aws:glue:region:account-id:job/job-name |
Implementation Steps
- Import boto3 and handle exceptions with botocore
- Create an AWS session and Glue client
- Use the
untag_resourcemethod with proper parameters - Handle potential errors during the operation
Example Code
Here's how to remove tags from an AWS Glue database −
import boto3
from botocore.exceptions import ClientError
def remove_tags_from_glue_resource(resource_arn, tags_to_remove):
"""
Remove specified tags from AWS Glue resource
Args:
resource_arn (str): ARN of the Glue resource
tags_to_remove (list): List of tag keys to remove
Returns:
dict: Response from AWS API
"""
try:
# Create AWS session and Glue client
session = boto3.session.Session()
glue_client = session.client('glue')
# Remove tags from the resource
response = glue_client.untag_resource(
ResourceArn=resource_arn,
TagsToRemove=tags_to_remove
)
return response
except ClientError as e:
raise Exception(f"AWS Client error in remove_tags_from_glue_resource: {str(e)}")
except Exception as e:
raise Exception(f"Unexpected error in remove_tags_from_glue_resource: {str(e)}")
# Example usage
if __name__ == "__main__":
# Define the resource ARN and tags to remove
database_arn = "arn:aws:glue:us-east-1:123456789012:database/sample-db"
tags_to_remove = ["glue-db", "environment"]
try:
result = remove_tags_from_glue_resource(database_arn, tags_to_remove)
print("Tags removed successfully!")
print(f"Response: {result}")
except Exception as e:
print(f"Error: {e}")
Expected Output
When the tags are successfully removed, you'll see a response like this −
Tags removed successfully!
Response: {
'ResponseMetadata': {
'RequestId': 'c9f418b0-****-****-****-************fb96',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'date': 'Fri, 02 Apr 2021 08:04:54 GMT',
'content-type': 'application/x-amz-json-1.1',
'content-length': '27',
'connection': 'keep-alive'
},
'RetryAttempts': 0
}
}
Key Points
- Ensure your AWS credentials are properly configured
- The resource ARN must be valid and accessible
- Only specify tag keys in the
TagsToRemoveparameter, not key-value pairs - Handle both
ClientErrorand generic exceptions for robust error handling
Conclusion
Using boto3's untag_resource method makes it easy to remove tags from AWS Glue resources programmatically. Always handle exceptions properly and ensure your AWS credentials have the necessary permissions for tag management operations.
