Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ใช้ Boto3 ลบ Tags จาก AWS Glue Resources

ภาพรวม

สาธิตการลบ tags จากทรัพยากร AWS Glue ต่างๆ เช่น Database, Table, Crawler, Job, Trigger ฯลฯ โดยใช้ untag_resource

รูปแบบ Resource ARN

ทรัพยากรรูปแบบ ARN
Catalogarn:aws:glue:region:account-id:catalog
Databasearn:aws:glue:region:account-id:database/database-name
Tablearn:aws:glue:region:account-id:table/database-name/table-name
Connectionarn:aws:glue:region:account-id:connection/connection-name
Crawlerarn:aws:glue:region:account-id:crawler/crawler-name
Jobarn:aws:glue:region:account-id:job/job-name
Triggerarn:aws:glue:region:account-id:trigger/trigger-name
Dev Endpointarn:aws:glue:region:account-id:devEndpoint/endpoint-name
ML Transformarn:aws:glue:region:account-id:mlTransform/transform-id

ตัวอย่างโค้ด

import boto3
from botocore.exceptions import ClientError

def remove_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(f"boto3 client error: {e}")
    except Exception as e:
        raise Exception(f"Unexpected error: {e}")

# ใช้งาน: ลบ tag key "glue-db" จาก database
tags_to_remove = ["glue-db"]
arn = "arn:aws:glue:us-east-1:1122225*****88:database/test-db"
print(remove_tags(arn, tags_to_remove))

ผลลัพธ์

{
    'ResponseMetadata': {
        'RequestId': 'c9f418b0-...',
        'HTTPStatusCode': 200,
        ...
    }
}

ข้อควรระวัง

  • tags_list ต้องเป็น list ของ key เท่านั้น (ไม่ต้องระบุ value)
  • ต้องมี permission glue:UntagResource
  • ถ้า tag ไม่มีอยู่จริง จะไม่เกิด error