How to use Boto3 to get the status of a migration operation?


Problem Statement − Use boto3 library in Python to get the status of a migrating operation.

Example − Get the status of migration operation in an account.

Approach/Algorithm to solve this problem

Step 1 − Import boto3 and botocore exceptions to handle exceptions.

Step 2 − Pass the parameter catalog_id whose migrating status needs to check. However, this is an optional parameter. If it is not provided, by default it checks for logged in user account whether migration is in progress or completed. catalog_id is nothing other than user account id.

Step 3 − Create an AWS session using boto3 library. Make sure region_name is mentioned in 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 − Call get_catalog_import_status and pass the catalog_id as CatalogId parameter.

Step 6 − It will fetch details of ongoing migrating operation. Otherwise, it will fetch last migration details.

Step 7 − Handle the generic exception if something went wrong while checking the job.

Example

Use the following code to get the status of a migrating operation −

import boto3
from botocore.exceptions import ClientError

def status_of_migration(catalog_id = None):
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.get_catalog_import_status(CatalogId = catalog_id)
      return response
   except ClientError as e:
      raise Exception( "boto3 client error in status_of_migration: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in status_of_migration: " + e.__str__())

print(status_of_migration())

Output

{'ImportStatus': {'ImportCompleted': True, 'ImportTime':
datetime.datetime(2017, 11, 17, 1, 32, 44, tzinfo=tzlocal()),
'ImportedBy': 'StatusSetByDefault'}, 'ResponseMetadata': {'RequestId':
'7c33d6f9-……………..-3b202961e3e7', 'HTTPStatusCode': 200, 'HTTPHeaders':
{'date': 'Sun, 21 Feb 2021 05:40:06 GMT', 'content-type':
'application/x-amz-json-1.1', 'content-length': '102', 'connection':
'keep-alive', 'x-amzn-requestid': '7c33d6f9-…….…………-3b202961e3e7'},
'RetryAttempts': 0}}

Updated on: 22-Mar-2021

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements