How to use Boto3 to find whether a function can paginate or not in AWS Secret Manager

AWSBoto3PythonServer Side ProgrammingProgramming

Problem Statement: Use boto3 library in Python to find out whether a function can paginate or not in AWS secret.

Approach/Algorithm to solve this problem

  • Step 1: Import boto3 and botocore exceptions to handle exceptions.

  • Step 2: secret_function is the required parameter in this function.

  • 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 secretmanager.

  • Step 5: Now use the can_paginate function and pass the parameter secret_function.

  • Step 6: It returns True if the function can paginate; else False.

  • Step 7: Handle the generic exception if something went wrong while checking the pagination.

Example Code

Use the following code to check pagination −

import boto3
from botocore.exceptions import ClientError

def check_pagination(secret_function)
   session = boto3.session.Session()
   client = session.client('secretmanager')
   try:
      response = client.can_paginate(secret_function)
   return response
   except ClientError as e:
      raise Exception("boto3 client error in check_pagination: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in check_pagination: " + e.__str__())

print(check_pagination("list_secrets"))
print(check_pagination("get_secret_value"))

Output

True
False
raja
Published on 16-Apr-2021 07:57:08
Advertisements