ภาพรวม
สาธิตการเพิ่ม tags ให้ทรัพยากร AWS Glue โดยใช้ tag_resource ของ boto3
รูปแบบ Tags
tags_dict ต้องเป็น dictionary รูปแบบ {"key": "value", ...}
ตัวอย่างโค้ด
import boto3
from botocore.exceptions import ClientError
def add_tags(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(f"boto3 client error: {e}")
except Exception as e:
raise Exception(f"Unexpected error: {e}")
# ใช้งาน
tags = {"glue-db": "test", "environment": "dev"}
arn = "arn:aws:glue:us-east-1:1122225*****88:database/test-db"
print(add_tags(arn, tags))ผลลัพธ์
{
'ResponseMetadata': {
'RequestId': 'c9f418b0-...',
'HTTPStatusCode': 200,
...
}
}ข้อสังเกต
- ถ้า key ซ้ำ จะอัปเดต value ใหม่
- ต้องมี permission
glue:TagResource - ใช้จัดกลุ่มทรัพยากร ควบคุมต้นทุน หรือการเข้าถึง (IAM conditions)