- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use Boto3 to get tags from an AWS Glue Resources
In this article, we will see how the users can get the tags associated in AWS Glue Resources.
Example
Get tags “glue-db: tests” from AWS glue database.
Problem Statement: Use boto3 library in Python to get the tags from AWS Glue Resources.
Approach/Algorithm to solve this problem
Step 1: Import boto3 and botocore exceptions to handle exceptions.
Step 2: resource_arn is the required parameter in this function.
Format of resource_arn should be as following −
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 |
Machine learning transform | arn:aws:glue:region:account-id:mlTransform/transform-id |
Step 3: Create an AWS session using boto3 lib. Make sure region_name is mentioned in the default profile. If it is not mentioned, then explicitly pass the region_name while creating the session.
Step 4: Create an AWS client for glue.
Step 5: Now use the get_tags function and pass the parameter resource_arn as ResourceArn.
Step 6: It returns the tags and response metadata from the resources.
Step 7: Handle the generic exception if something went wrong while getting tags.
Example Code
Use the following code to get the tags −
import boto3 from botocore.exceptions import ClientError def get_tags_from_resource(resource_arn) session = boto3.session.Session() glue_client = session.client('glue') try: response = glue_client.get_tags(ResourceArn= resource_arn) return response except ClientError as e: raise Exception("boto3 client error in get_tags_from_resource: " + e.__str__()) except Exception as e: raise Exception("Unexpected error in get_tags_from_resource: " + e.__str__()) print(add_tags_in_resource("arn:aws:glue:us-east-1:1122225*****88:database/test-db"))
Output
{'Tags': {'glue-job': 'test'}, 'ResponseMetadata': {'RequestId': 'c9f418b0-8d02-4a26-*************', '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-8d02-4a26-**************'}, 'RetryAttempts': 0}}