To manage AWS Bedrock prompts and check access, as well as list all available prompts in a particular environment, you can use the AWS SDK (e.g., Boto3 for Python). Below is an example Python script that accomplishes these tasks. This script assumes you have the necessary AWS credentials configured and the required permissions to access Bedrock resources.
Prerequisites:
- Install Boto3:
pip install boto3
- Configure AWS credentials (e.g., using
~/.aws/credentials or environment variables).
Python Script:
import boto3
from botocore.exceptions import NoCredentialsError, PartialCredentialsError, ClientError
# Initialize the Bedrock client
bedrock_client = boto3.client('bedrock', region_name='us-west-2') # Replace with your region
def check_bedrock_access():
"""
Check if the AWS credentials have access to Bedrock.
"""
try:
# Attempt to list foundation models to check access
response = bedrock_client.list_foundation_models()
print("Access to AWS Bedrock is available.")
return True
except (NoCredentialsError, PartialCredentialsError):
print("AWS credentials are missing or incomplete.")
return False
except ClientError as e:
if e.response['Error']['Code'] == 'AccessDeniedException':
print("Access to AWS Bedrock is denied.")
else:
print(f"An error occurred: {e}")
return False
def list_bedrock_prompts(environment_id):
"""
List all prompts available in a particular environment.
"""
try:
# List all prompts in the specified environment
response = bedrock_client.list_prompts(
environmentId=environment_id
)
prompts = response.get('prompts', [])
if prompts:
print(f"Prompts available in environment '{environment_id}':")
for prompt in prompts:
print(f"- {prompt['promptName']} ({prompt['promptId']})")
else:
print(f"No prompts found in environment '{environment_id}'.")
except ClientError as e:
print(f"Failed to list prompts: {e}")
if __name__ == "__main__":
# Check Bedrock access
if check_bedrock_access():
# Replace with your environment ID
environment_id = "your-environment-id"
list_bedrock_prompts(environment_id)
Explanation:
-
check_bedrock_access():
- Attempts to list foundation models using
list_foundation_models() to verify access to Bedrock.
- Handles credential errors and access denied exceptions.
-
list_bedrock_prompts(environment_id):
- Lists all prompts in the specified environment using
list_prompts().
- Prints the prompt names and IDs.
-
Main Execution:
- Checks Bedrock access first.
- If access is available, lists prompts for the specified environment.
Notes:
- Replace
'us-west-2' with your AWS region.
- Replace
'your-environment-id' with the actual environment ID you want to query.
- Ensure the IAM role or user has the necessary permissions for
bedrock:ListFoundationModels and bedrock:ListPrompts.
Let me know if you need further assistance!