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 add tags in AWS Glue Resources
In this article, we will see how to add tags to AWS Glue resources using the Boto3 library in Python. Tags help organize and manage your AWS resources effectively.
Problem Statement
Use the boto3 library in Python to add tags like "glue-db: test" to AWS Glue resources such as databases, tables, crawlers, and jobs.
Understanding AWS Glue Resource ARNs
Before adding tags, you need to understand the ARN format for different AWS Glue resources ?
| 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 |
| Trigger | arn:aws:glue:region:account-id:trigger/trigger-name |
| Development Endpoint | arn:aws:glue:region:account-id:devEndpoint/development-endpoint-name |
Implementation Steps
Follow these steps to add tags to AWS Glue resources ?
-
Step 1: Import
boto3andbotocore.exceptionsto handle exceptions -
Step 2: Define the resource ARN and tags dictionary in the format
{"key": "value"} -
Step 3: Create an AWS session using
boto3with proper region configuration - Step 4: Create an AWS Glue client
-
Step 5: Use the
tag_resource()function with ResourceArn and TagsToAdd parameters - Step 6: Handle exceptions appropriately
Example Code
Here's a complete example to add tags to an AWS Glue database ?
import boto3
from botocore.exceptions import ClientError
def add_tags_in_resource(resource_arn, tags_dict):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.tag_resource(
ResourceArn=resource_arn,
TagsToAdd=tags_dict
)
return response
except ClientError as e:
raise Exception("boto3 client error in add_tags_in_resource: " + str(e))
except Exception as e:
raise Exception("Unexpected error in add_tags_in_resource: " + str(e))
# Example usage
tags_dict = {"glue-db": "test", "environment": "development"}
resource_arn = "arn:aws:glue:us-east-1:123456789012:database/test-db"
print(add_tags_in_resource(resource_arn, tags_dict))
Output
The function returns a response metadata confirming successful tag addition ?
{
'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',
'x-amzn-requestid': 'c9f418b0-****-****-****-************fb96'
},
'RetryAttempts': 0
}
}
Key Points
- Ensure your AWS credentials are properly configured
- The resource ARN must match the exact format for each resource type
- Tags dictionary should contain string key-value pairs
- Handle both ClientError and generic exceptions for robust error handling
Conclusion
Using Boto3's tag_resource() function makes it easy to add tags to AWS Glue resources programmatically. This approach helps automate resource management and maintain consistent tagging across your AWS infrastructure.
