Minh họa cách dùng boto3 để gỡ bỏ tags khỏi các tài nguyên AWS Glue (Database, Table, Crawler, Job, v.v.).
Quy trình
- Import boto3, ClientError.
- Hàm nhận
resource_arn(định dạng ARN tùy loại tài nguyên) vàtags_list(danh sách key cần xóa). - Tạo session, client Glue.
- Gọi
glue_client.untag_resource(ResourceArn=..., TagsToRemove=...). - Trả về response metadata.
- Xử lý ngoại lệ.
Bảng định dạng ARN
| Loại tài nguyên | Định dạng ARN |
|---|---|
| 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 |
| DevEndpoint | arn:aws:glue:region:account-id:devEndpoint/endpoint-name |
| ML Transform | arn:aws:glue:region:account-id:mlTransform/transform-id |
Code mẫu
import boto3
from botocore.exceptions import ClientError
def xoa_tags(resource_arn, tags_list):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.untag_resource(ResourceArn=resource_arn, TagsToRemove=tags_list)
return response
except ClientError as e:
raise Exception("Lỗi boto3 client: " + str(e))
except Exception as e:
raise Exception("Lỗi không mong đợi: " + str(e))
tags_dict = ["glue-db"]
print(xoa_tags("arn:aws:glue:us-east-1:1122225*****88:database/test-db", tags_dict))
Kết quả
{'ResponseMetadata': {'RequestId': 'c9f418b0-...', 'HTTPStatusCode': 200, ...}}