Esempi per Secrets Manager con SDK per Python (Boto3) - Esempi di codice per SDK AWS

Sono disponibili altri esempi per SDK AWS nel repository GitHub della documentazione degli esempi per SDK AWS.

Esempi per Secrets Manager con SDK per Python (Boto3)

Gli esempi di codice seguenti mostrano come eseguire operazioni e implementare scenari comuni utilizzando AWS SDK per Python (Boto3) con Secrets Manager.

Le azioni sono estratti di codice da programmi più grandi e devono essere eseguite nel contesto. Sebbene le operazioni mostrino come richiamare le singole funzioni del servizio, è possibile visualizzarle contestualizzate negli scenari correlati.

Scenari: esempi di codice che mostrano come eseguire un’attività specifica chiamando più funzioni all’interno dello stesso servizio o combinate con altri Servizi AWS.

Ogni esempio include un link al codice sorgente completo, in cui vengono fornite le istruzioni su come configurare ed eseguire il codice nel contesto.

Argomenti

Azioni

L’esempio di codice seguente mostra come utilizzare BatchGetSecretValue.

SDK per Python (Boto3)
Nota

Ulteriori informazioni su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel Repository di esempi di codice AWS.

class BatchGetSecretsWrapper: def __init__(self, secretsmanager_client): self.client = secretsmanager_client def batch_get_secrets(self, filter_name): """ Retrieve multiple secrets from AWS Secrets Manager using the batch_get_secret_value API. This function assumes the stack mentioned in the source code README has been successfully deployed. This stack includes 7 secrets, all of which have names beginning with "mySecret". :param filter_name: The full or partial name of secrets to be fetched. :type filter_name: str """ try: secrets = [] response = self.client.batch_get_secret_value( Filters=[{"Key": "name", "Values": [f"{filter_name}"]}] ) for secret in response["SecretValues"]: secrets.append(json.loads(secret["SecretString"])) if secrets: logger.info("Secrets retrieved successfully.") else: logger.info("Zero secrets returned without error.") return secrets except self.client.exceptions.ResourceNotFoundException: msg = f"One or more requested secrets were not found with filter: {filter_name}" logger.info(msg) return msg except Exception as e: logger.error(f"An unknown error occurred:\n{str(e)}.") raise
  • Per informazioni dettagliate sull’API, consulta BatchGetSecretValue nella documentazione di riferimento dell’API AWS SDK per Python (Boto3).

L’esempio di codice seguente mostra come utilizzare GetSecretValue.

SDK per Python (Boto3)
Nota

Ulteriori informazioni su GitHub. Trova l’esempio completo e scopri di più sulla configurazione e l’esecuzione nel Repository di esempi di codice AWS.

class GetSecretWrapper: def __init__(self, secretsmanager_client): self.client = secretsmanager_client def get_secret(self, secret_name): """ Retrieve individual secrets from AWS Secrets Manager using the get_secret_value API. This function assumes the stack mentioned in the source code README has been successfully deployed. This stack includes 7 secrets, all of which have names beginning with "mySecret". :param secret_name: The name of the secret fetched. :type secret_name: str """ try: get_secret_value_response = self.client.get_secret_value( SecretId=secret_name ) logging.info("Secret retrieved successfully.") return get_secret_value_response["SecretString"] except self.client.exceptions.ResourceNotFoundException: msg = f"The requested secret {secret_name} was not found." logger.info(msg) return msg except Exception as e: logger.error(f"An unknown error occurred: {str(e)}.") raise
  • Per informazioni dettagliate sull’API, consulta GetSecretValue nella documentazione di riferimento dell’API AWS SDK per Python (Boto3).

Scenari

L’esempio di codice seguente mostra come creare una libreria di prestiti in cui gli utenti possono prendere in prestito e restituire libri tramite una REST API supportata da un database Amazon Aurora.

SDK per Python (Boto3)

Mostra come usare AWS SDK per Python (Boto3) con l’API Amazon Relational Database Service (Amazon RDS) e AWS Chalice per creare una REST API supportata da un database Amazon Aurora. Il servizio Web è completamente serverless e rappresenta una semplice libreria di prestiti in cui gli utenti possono prendere in prestito e restituire libri. Scopri come:

  • Creare e gestire un cluster di database Aurora serverless.

  • Utilizzare Gestione dei segreti AWS per gestire le credenziali del database.

  • Implementare un livello di archiviazione di dati che utilizza Amazon RDS per spostare i dati dentro e fuori dal database.

  • Utilizzare AWS Chalice per distribuire una REST API serverless su Gateway Amazon API e AWS Lambda.

  • Utilizza il pacchetto Richieste per inviare le richieste al servizio Web.

Per il codice sorgente completo e le istruzioni su come configurare ed eseguire, consulta l’esempio completo su GitHub.

Servizi utilizzati in questo esempio
  • Gateway API

  • Aurora

  • Lambda

  • Secrets Manager