Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.
|
|
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||
|
|
# SPDX-License-Identifier: Apache-2.0
|
||
|
|
|
||
|
|
# snippet-start:[python.example_code.rds.hello]
|
||
|
|
"""
|
||
|
|
Purpose
|
||
|
|
|
||
|
|
Shows how to use the AWS SDK for Python (Boto3) with the Amazon Relational Database Service
|
||
|
|
(Amazon RDS) to list the databases in your account.
|
||
|
|
"""
|
||
|
|
|
||
|
|
import boto3
|
||
|
|
from botocore.exceptions import ClientError
|
||
|
|
|
||
|
|
# Create an RDS client
|
||
|
|
rds_client = boto3.client("rds")
|
||
|
|
|
||
|
|
# Create a paginator for the describe_db_instances operation
|
||
|
|
paginator = rds_client.get_paginator("describe_db_instances")
|
||
|
|
|
||
|
|
try:
|
||
|
|
# Use the paginator to get a list of DB instances
|
||
|
|
response_iterator = paginator.paginate(
|
||
|
|
PaginationConfig={
|
||
|
|
"MaxItems": 123,
|
||
|
|
"PageSize": 50, # Adjust PageSize as needed
|
||
|
|
"StartingToken": None,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
# Iterate through the pages of the response
|
||
|
|
instances_found = False
|
||
|
|
for page in response_iterator:
|
||
|
|
if "DBInstances" in page and page["DBInstances"]:
|
||
|
|
instances_found = True
|
||
|
|
print("Your RDS instances are:")
|
||
|
|
for db in page["DBInstances"]:
|
||
|
|
print(db["DBInstanceIdentifier"])
|
||
|
|
|
||
|
|
if not instances_found:
|
||
|
|
print("No RDS instances found!")
|
||
|
|
|
||
|
|
except ClientError as e:
|
||
|
|
print(f"Couldn't list RDS instances. Here's why: {e.response['Error']['Message']}")
|
||
|
|
|
||
|
|
# snippet-end:[python.example_code.rds.hello]
|