Seleccione sus preferencias de cookies

Usamos cookies esenciales y herramientas similares que son necesarias para proporcionar nuestro sitio y nuestros servicios. Usamos cookies de rendimiento para recopilar estadísticas anónimas para que podamos entender cómo los clientes usan nuestro sitio y hacer mejoras. Las cookies esenciales no se pueden desactivar, pero puede hacer clic en “Personalizar” o “Rechazar” para rechazar las cookies de rendimiento.

Si está de acuerdo, AWS y los terceros aprobados también utilizarán cookies para proporcionar características útiles del sitio, recordar sus preferencias y mostrar contenido relevante, incluida publicidad relevante. Para aceptar o rechazar todas las cookies no esenciales, haga clic en “Aceptar” o “Rechazar”. Para elegir opciones más detalladas, haga clic en “Personalizar”.

Ejemplos de Amazon RDS usando SDK para Python (Boto3) - AWS Ejemplos de código de SDK

Hay más ejemplos de AWS SDK disponibles en el GitHub repositorio de ejemplos de AWS Doc SDK.

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

Hay más ejemplos de AWS SDK disponibles en el GitHub repositorio de ejemplos de AWS Doc SDK.

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

Ejemplos de Amazon RDS usando SDK para Python (Boto3)

Los siguientes ejemplos de código muestran cómo realizar acciones e implementar escenarios comunes AWS SDK for Python (Boto3) mediante Amazon RDS.

Los conceptos básicos son ejemplos de código que muestran cómo realizar las operaciones esenciales dentro de un servicio.

Las acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Mientras las acciones muestran cómo llamar a las distintas funciones de servicio, es posible ver las acciones en contexto en los escenarios relacionados.

Los escenarios son ejemplos de código que muestran cómo llevar a cabo una tarea específica a través de llamadas a varias funciones dentro del servicio o combinado con otros Servicios de AWS.

En cada ejemplo se incluye un enlace al código de origen completo, con instrucciones de configuración y ejecución del código en el contexto.

Introducción

En los siguientes ejemplos de código se muestra cómo empezar a utilizar Amazon RDS.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

""" 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']}")
  • Para obtener más información sobre la API, consulta la descripción DBInstances en la referencia de la API del AWS SDK for Python (Boto3).

En los siguientes ejemplos de código se muestra cómo empezar a utilizar Amazon RDS.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

""" 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']}")
  • Para obtener más información sobre la API, consulta la descripción DBInstances en la referencia de la API del AWS SDK for Python (Boto3).

Conceptos básicos

En el siguiente ejemplo de código, se muestra cómo:

  • Cree un grupo de parámetros de base de datos personalizado y defina los valores de los parámetros.

  • Cree una instancia de base de datos que esté configurada para utilizar el grupo de parámetros. La instancia de base de datos también contiene una base de datos.

  • Cree una instantánea de la instancia.

  • Elimine la instancia y el grupo de parámetros.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Ejecutar un escenario interactivo en un símbolo del sistema

class RdsInstanceScenario: """Runs a scenario that shows how to get started using Amazon RDS DB instances.""" def __init__(self, instance_wrapper): """ :param instance_wrapper: An object that wraps Amazon RDS DB instance actions. """ self.instance_wrapper = instance_wrapper def create_parameter_group(self, parameter_group_name, db_engine): """ Shows how to get available engine versions for a specified database engine and create a DB parameter group that is compatible with a selected engine family. :param parameter_group_name: The name given to the newly created parameter group. :param db_engine: The database engine to use as a basis. :return: The newly created parameter group. """ print( f"Checking for an existing DB instance parameter group named {parameter_group_name}." ) parameter_group = self.instance_wrapper.get_parameter_group( parameter_group_name ) if parameter_group is None: print(f"Getting available database engine versions for {db_engine}.") engine_versions = self.instance_wrapper.get_engine_versions(db_engine) families = list({ver["DBParameterGroupFamily"] for ver in engine_versions}) family_index = q.choose("Which family do you want to use? ", families) print(f"Creating a parameter group.") self.instance_wrapper.create_parameter_group( parameter_group_name, families[family_index], "Example parameter group." ) parameter_group = self.instance_wrapper.get_parameter_group( parameter_group_name ) print(f"Parameter group {parameter_group['DBParameterGroupName']}:") pp(parameter_group) print("-" * 88) return parameter_group def update_parameters(self, parameter_group_name): """ Shows how to get the parameters contained in a custom parameter group and update some of the parameter values in the group. :param parameter_group_name: The name of the parameter group to query and modify. """ print("Let's set some parameter values in your parameter group.") auto_inc_parameters = self.instance_wrapper.get_parameters( parameter_group_name, name_prefix="auto_increment" ) update_params = [] for auto_inc in auto_inc_parameters: if auto_inc["IsModifiable"] and auto_inc["DataType"] == "integer": print(f"The {auto_inc['ParameterName']} parameter is described as:") print(f"\t{auto_inc['Description']}") param_range = auto_inc["AllowedValues"].split("-") auto_inc["ParameterValue"] = str( q.ask( f"Enter a value between {param_range[0]} and {param_range[1]}: ", q.is_int, q.in_range(int(param_range[0]), int(param_range[1])), ) ) update_params.append(auto_inc) self.instance_wrapper.update_parameters(parameter_group_name, update_params) print( "You can get a list of parameters you've set by specifying a source of 'user'." ) user_parameters = self.instance_wrapper.get_parameters( parameter_group_name, source="user" ) pp(user_parameters) print("-" * 88) def create_instance(self, instance_name, db_name, db_engine, parameter_group): """ Shows how to create a DB instance that contains a database of a specified type and is configured to use a custom DB parameter group. :param instance_name: The name given to the newly created DB instance. :param db_name: The name given to the created database. :param db_engine: The engine of the created database. :param parameter_group: The parameter group that is associated with the DB instance. :return: The newly created DB instance. """ print("Checking for an existing DB instance.") db_inst = self.instance_wrapper.get_db_instance(instance_name) if db_inst is None: print("Let's create a DB instance.") admin_username = q.ask( "Enter an administrator user name for the database: ", q.non_empty ) admin_password = q.ask( "Enter a password for the administrator (at least 8 characters): ", q.non_empty, ) engine_versions = self.instance_wrapper.get_engine_versions( db_engine, parameter_group["DBParameterGroupFamily"] ) engine_choices = [ver["EngineVersion"] for ver in engine_versions] print("The available engines for your parameter group are:") engine_index = q.choose("Which engine do you want to use? ", engine_choices) engine_selection = engine_versions[engine_index] print( "The available micro DB instance classes for your database engine are:" ) inst_opts = self.instance_wrapper.get_orderable_instances( engine_selection["Engine"], engine_selection["EngineVersion"] ) inst_choices = list( { opt["DBInstanceClass"] for opt in inst_opts if "micro" in opt["DBInstanceClass"] } ) inst_index = q.choose( "Which micro DB instance class do you want to use? ", inst_choices ) group_name = parameter_group["DBParameterGroupName"] storage_type = "standard" allocated_storage = 5 print( f"Creating a DB instance named {instance_name} and database {db_name}.\n" f"The DB instance is configured to use your custom parameter group {group_name},\n" f"selected engine {engine_selection['EngineVersion']},\n" f"selected DB instance class {inst_choices[inst_index]}," f"and {allocated_storage} GiB of {storage_type} storage.\n" f"This typically takes several minutes." ) db_inst = self.instance_wrapper.create_db_instance( db_name, instance_name, group_name, engine_selection["Engine"], engine_selection["EngineVersion"], inst_choices[inst_index], storage_type, allocated_storage, admin_username, admin_password, ) while db_inst.get("DBInstanceStatus") != "available": wait(10) db_inst = self.instance_wrapper.get_db_instance(instance_name) print("Instance data:") pp(db_inst) print("-" * 88) return db_inst @staticmethod def display_connection(db_inst): """ Displays connection information about a DB instance and tips on how to connect to it. :param db_inst: The DB instance to display. """ print( "You can now connect to your database using your favorite MySql client.\n" "One way to connect is by using the 'mysql' shell on an Amazon EC2 instance\n" "that is running in the same VPC as your DB instance. Pass the endpoint,\n" "port, and administrator user name to 'mysql' and enter your password\n" "when prompted:\n" ) print( f"\n\tmysql -h {db_inst['Endpoint']['Address']} -P {db_inst['Endpoint']['Port']} " f"-u {db_inst['MasterUsername']} -p\n" ) print( "For more information, see the User Guide for Amazon RDS:\n" "\thttps://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.MySQL.html#CHAP_GettingStarted.Connecting.MySQL" ) print("-" * 88) def create_snapshot(self, instance_name): """ Shows how to create a DB instance snapshot and wait until it's available. :param instance_name: The name of a DB instance to snapshot. """ if q.ask( "Do you want to create a snapshot of your DB instance (y/n)? ", q.is_yesno ): snapshot_id = f"{instance_name}-{uuid.uuid4()}" print( f"Creating a snapshot named {snapshot_id}. This typically takes a few minutes." ) snapshot = self.instance_wrapper.create_snapshot(snapshot_id, instance_name) while snapshot.get("Status") != "available": wait(10) snapshot = self.instance_wrapper.get_snapshot(snapshot_id) pp(snapshot) print("-" * 88) def cleanup(self, db_inst, parameter_group_name): """ Shows how to clean up a DB instance and parameter group. Before the parameter group can be deleted, all associated DB instances must first be deleted. :param db_inst: The DB instance to delete. :param parameter_group_name: The DB parameter group to delete. """ if q.ask( "\nDo you want to delete the DB instance and parameter group (y/n)? ", q.is_yesno, ): print(f"Deleting DB instance {db_inst['DBInstanceIdentifier']}.") self.instance_wrapper.delete_db_instance(db_inst["DBInstanceIdentifier"]) print( "Waiting for the DB instance to delete. This typically takes several minutes." ) while db_inst is not None: wait(10) db_inst = self.instance_wrapper.get_db_instance( db_inst["DBInstanceIdentifier"] ) print(f"Deleting parameter group {parameter_group_name}.") self.instance_wrapper.delete_parameter_group(parameter_group_name) def run_scenario(self, db_engine, parameter_group_name, instance_name, db_name): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") print("-" * 88) print( "Welcome to the Amazon Relational Database Service (Amazon RDS)\n" "get started with DB instances demo." ) print("-" * 88) parameter_group = self.create_parameter_group(parameter_group_name, db_engine) self.update_parameters(parameter_group_name) db_inst = self.create_instance( instance_name, db_name, db_engine, parameter_group ) self.display_connection(db_inst) self.create_snapshot(instance_name) self.cleanup(db_inst, parameter_group_name) print("\nThanks for watching!") print("-" * 88) if __name__ == "__main__": try: scenario = RdsInstanceScenario(InstanceWrapper.from_client()) scenario.run_scenario( "mysql", "doc-example-parameter-group", "doc-example-instance", "docexampledb", ) except Exception: logging.exception("Something went wrong with the demo.")

Defina las funciones a las que llama el escenario para administrar las acciones de Amazon RDS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_parameter_group(self, parameter_group_name): """ Gets a DB parameter group. :param parameter_group_name: The name of the parameter group to retrieve. :return: The parameter group. """ try: response = self.rds_client.describe_db_parameter_groups( DBParameterGroupName=parameter_group_name ) parameter_group = response["DBParameterGroups"][0] except ClientError as err: if err.response["Error"]["Code"] == "DBParameterGroupNotFound": logger.info("Parameter group %s does not exist.", parameter_group_name) else: logger.error( "Couldn't get parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return parameter_group def create_parameter_group( self, parameter_group_name, parameter_group_family, description ): """ Creates a DB parameter group that is based on the specified parameter group family. :param parameter_group_name: The name of the newly created parameter group. :param parameter_group_family: The family that is used as the basis of the new parameter group. :param description: A description given to the parameter group. :return: Data about the newly created parameter group. """ try: response = self.rds_client.create_db_parameter_group( DBParameterGroupName=parameter_group_name, DBParameterGroupFamily=parameter_group_family, Description=description, ) except ClientError as err: logger.error( "Couldn't create parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response def delete_parameter_group(self, parameter_group_name): """ Deletes a DB parameter group. :param parameter_group_name: The name of the parameter group to delete. :return: Data about the parameter group. """ try: self.rds_client.delete_db_parameter_group( DBParameterGroupName=parameter_group_name ) except ClientError as err: logger.error( "Couldn't delete parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def get_parameters(self, parameter_group_name, name_prefix="", source=None): """ Gets the parameters that are contained in a DB parameter group. :param parameter_group_name: The name of the parameter group to query. :param name_prefix: When specified, the retrieved list of parameters is filtered to contain only parameters that start with this prefix. :param source: When specified, only parameters from this source are retrieved. For example, a source of 'user' retrieves only parameters that were set by a user. :return: The list of requested parameters. """ try: kwargs = {"DBParameterGroupName": parameter_group_name} if source is not None: kwargs["Source"] = source parameters = [] paginator = self.rds_client.get_paginator("describe_db_parameters") for page in paginator.paginate(**kwargs): parameters += [ p for p in page["Parameters"] if p["ParameterName"].startswith(name_prefix) ] except ClientError as err: logger.error( "Couldn't get parameters for %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return parameters def update_parameters(self, parameter_group_name, update_parameters): """ Updates parameters in a custom DB parameter group. :param parameter_group_name: The name of the parameter group to update. :param update_parameters: The parameters to update in the group. :return: Data about the modified parameter group. """ try: response = self.rds_client.modify_db_parameter_group( DBParameterGroupName=parameter_group_name, Parameters=update_parameters ) except ClientError as err: logger.error( "Couldn't update parameters in %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response def create_snapshot(self, snapshot_id, instance_id): """ Creates a snapshot of a DB instance. :param snapshot_id: The ID to give the created snapshot. :param instance_id: The ID of the DB instance to snapshot. :return: Data about the newly created snapshot. """ try: response = self.rds_client.create_db_snapshot( DBSnapshotIdentifier=snapshot_id, DBInstanceIdentifier=instance_id ) snapshot = response["DBSnapshot"] except ClientError as err: logger.error( "Couldn't create snapshot of %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot def get_snapshot(self, snapshot_id): """ Gets a DB instance snapshot. :param snapshot_id: The ID of the snapshot to retrieve. :return: The retrieved snapshot. """ try: response = self.rds_client.describe_db_snapshots( DBSnapshotIdentifier=snapshot_id ) snapshot = response["DBSnapshots"][0] except ClientError as err: logger.error( "Couldn't get snapshot %s. Here's why: %s: %s", snapshot_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot def get_engine_versions(self, engine, parameter_group_family=None): """ Gets database engine versions that are available for the specified engine and parameter group family. :param engine: The database engine to look up. :param parameter_group_family: When specified, restricts the returned list of engine versions to those that are compatible with this parameter group family. :return: The list of database engine versions. """ try: kwargs = {"Engine": engine} if parameter_group_family is not None: kwargs["DBParameterGroupFamily"] = parameter_group_family response = self.rds_client.describe_db_engine_versions(**kwargs) versions = response["DBEngineVersions"] except ClientError as err: logger.error( "Couldn't get engine versions for %s. Here's why: %s: %s", engine, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return versions def get_orderable_instances(self, db_engine, db_engine_version): """ Gets DB instance options that can be used to create DB instances that are compatible with a set of specifications. :param db_engine: The database engine that must be supported by the DB instance. :param db_engine_version: The engine version that must be supported by the DB instance. :return: The list of DB instance options that can be used to create a compatible DB instance. """ try: inst_opts = [] paginator = self.rds_client.get_paginator( "describe_orderable_db_instance_options" ) for page in paginator.paginate( Engine=db_engine, EngineVersion=db_engine_version ): inst_opts += page["OrderableDBInstanceOptions"] except ClientError as err: logger.error( "Couldn't get orderable DB instances. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return inst_opts def get_db_instance(self, instance_id): """ Gets data about a DB instance. :param instance_id: The ID of the DB instance to retrieve. :return: The retrieved DB instance. """ try: response = self.rds_client.describe_db_instances( DBInstanceIdentifier=instance_id ) db_inst = response["DBInstances"][0] except ClientError as err: if err.response["Error"]["Code"] == "DBInstanceNotFound": logger.info("Instance %s does not exist.", instance_id) else: logger.error( "Couldn't get DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst def create_db_instance( self, db_name, instance_id, parameter_group_name, db_engine, db_engine_version, instance_class, storage_type, allocated_storage, admin_name, admin_password, ): """ Creates a DB instance. :param db_name: The name of the database that is created in the DB instance. :param instance_id: The ID to give the newly created DB instance. :param parameter_group_name: A parameter group to associate with the DB instance. :param db_engine: The database engine of a database to create in the DB instance. :param db_engine_version: The engine version for the created database. :param instance_class: The DB instance class for the newly created DB instance. :param storage_type: The storage type of the DB instance. :param allocated_storage: The amount of storage allocated on the DB instance, in GiBs. :param admin_name: The name of the admin user for the created database. :param admin_password: The admin password for the created database. :return: Data about the newly created DB instance. """ try: response = self.rds_client.create_db_instance( DBName=db_name, DBInstanceIdentifier=instance_id, DBParameterGroupName=parameter_group_name, Engine=db_engine, EngineVersion=db_engine_version, DBInstanceClass=instance_class, StorageType=storage_type, AllocatedStorage=allocated_storage, MasterUsername=admin_name, MasterUserPassword=admin_password, ) db_inst = response["DBInstance"] except ClientError as err: logger.error( "Couldn't create DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst def delete_db_instance(self, instance_id): """ Deletes a DB instance. :param instance_id: The ID of the DB instance to delete. :return: Data about the deleted DB instance. """ try: response = self.rds_client.delete_db_instance( DBInstanceIdentifier=instance_id, SkipFinalSnapshot=True, DeleteAutomatedBackups=True, ) db_inst = response["DBInstance"] except ClientError as err: logger.error( "Couldn't delete DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst

En el siguiente ejemplo de código, se muestra cómo:

  • Cree un grupo de parámetros de base de datos personalizado y defina los valores de los parámetros.

  • Cree una instancia de base de datos que esté configurada para utilizar el grupo de parámetros. La instancia de base de datos también contiene una base de datos.

  • Cree una instantánea de la instancia.

  • Elimine la instancia y el grupo de parámetros.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

Ejecutar un escenario interactivo en un símbolo del sistema

class RdsInstanceScenario: """Runs a scenario that shows how to get started using Amazon RDS DB instances.""" def __init__(self, instance_wrapper): """ :param instance_wrapper: An object that wraps Amazon RDS DB instance actions. """ self.instance_wrapper = instance_wrapper def create_parameter_group(self, parameter_group_name, db_engine): """ Shows how to get available engine versions for a specified database engine and create a DB parameter group that is compatible with a selected engine family. :param parameter_group_name: The name given to the newly created parameter group. :param db_engine: The database engine to use as a basis. :return: The newly created parameter group. """ print( f"Checking for an existing DB instance parameter group named {parameter_group_name}." ) parameter_group = self.instance_wrapper.get_parameter_group( parameter_group_name ) if parameter_group is None: print(f"Getting available database engine versions for {db_engine}.") engine_versions = self.instance_wrapper.get_engine_versions(db_engine) families = list({ver["DBParameterGroupFamily"] for ver in engine_versions}) family_index = q.choose("Which family do you want to use? ", families) print(f"Creating a parameter group.") self.instance_wrapper.create_parameter_group( parameter_group_name, families[family_index], "Example parameter group." ) parameter_group = self.instance_wrapper.get_parameter_group( parameter_group_name ) print(f"Parameter group {parameter_group['DBParameterGroupName']}:") pp(parameter_group) print("-" * 88) return parameter_group def update_parameters(self, parameter_group_name): """ Shows how to get the parameters contained in a custom parameter group and update some of the parameter values in the group. :param parameter_group_name: The name of the parameter group to query and modify. """ print("Let's set some parameter values in your parameter group.") auto_inc_parameters = self.instance_wrapper.get_parameters( parameter_group_name, name_prefix="auto_increment" ) update_params = [] for auto_inc in auto_inc_parameters: if auto_inc["IsModifiable"] and auto_inc["DataType"] == "integer": print(f"The {auto_inc['ParameterName']} parameter is described as:") print(f"\t{auto_inc['Description']}") param_range = auto_inc["AllowedValues"].split("-") auto_inc["ParameterValue"] = str( q.ask( f"Enter a value between {param_range[0]} and {param_range[1]}: ", q.is_int, q.in_range(int(param_range[0]), int(param_range[1])), ) ) update_params.append(auto_inc) self.instance_wrapper.update_parameters(parameter_group_name, update_params) print( "You can get a list of parameters you've set by specifying a source of 'user'." ) user_parameters = self.instance_wrapper.get_parameters( parameter_group_name, source="user" ) pp(user_parameters) print("-" * 88) def create_instance(self, instance_name, db_name, db_engine, parameter_group): """ Shows how to create a DB instance that contains a database of a specified type and is configured to use a custom DB parameter group. :param instance_name: The name given to the newly created DB instance. :param db_name: The name given to the created database. :param db_engine: The engine of the created database. :param parameter_group: The parameter group that is associated with the DB instance. :return: The newly created DB instance. """ print("Checking for an existing DB instance.") db_inst = self.instance_wrapper.get_db_instance(instance_name) if db_inst is None: print("Let's create a DB instance.") admin_username = q.ask( "Enter an administrator user name for the database: ", q.non_empty ) admin_password = q.ask( "Enter a password for the administrator (at least 8 characters): ", q.non_empty, ) engine_versions = self.instance_wrapper.get_engine_versions( db_engine, parameter_group["DBParameterGroupFamily"] ) engine_choices = [ver["EngineVersion"] for ver in engine_versions] print("The available engines for your parameter group are:") engine_index = q.choose("Which engine do you want to use? ", engine_choices) engine_selection = engine_versions[engine_index] print( "The available micro DB instance classes for your database engine are:" ) inst_opts = self.instance_wrapper.get_orderable_instances( engine_selection["Engine"], engine_selection["EngineVersion"] ) inst_choices = list( { opt["DBInstanceClass"] for opt in inst_opts if "micro" in opt["DBInstanceClass"] } ) inst_index = q.choose( "Which micro DB instance class do you want to use? ", inst_choices ) group_name = parameter_group["DBParameterGroupName"] storage_type = "standard" allocated_storage = 5 print( f"Creating a DB instance named {instance_name} and database {db_name}.\n" f"The DB instance is configured to use your custom parameter group {group_name},\n" f"selected engine {engine_selection['EngineVersion']},\n" f"selected DB instance class {inst_choices[inst_index]}," f"and {allocated_storage} GiB of {storage_type} storage.\n" f"This typically takes several minutes." ) db_inst = self.instance_wrapper.create_db_instance( db_name, instance_name, group_name, engine_selection["Engine"], engine_selection["EngineVersion"], inst_choices[inst_index], storage_type, allocated_storage, admin_username, admin_password, ) while db_inst.get("DBInstanceStatus") != "available": wait(10) db_inst = self.instance_wrapper.get_db_instance(instance_name) print("Instance data:") pp(db_inst) print("-" * 88) return db_inst @staticmethod def display_connection(db_inst): """ Displays connection information about a DB instance and tips on how to connect to it. :param db_inst: The DB instance to display. """ print( "You can now connect to your database using your favorite MySql client.\n" "One way to connect is by using the 'mysql' shell on an Amazon EC2 instance\n" "that is running in the same VPC as your DB instance. Pass the endpoint,\n" "port, and administrator user name to 'mysql' and enter your password\n" "when prompted:\n" ) print( f"\n\tmysql -h {db_inst['Endpoint']['Address']} -P {db_inst['Endpoint']['Port']} " f"-u {db_inst['MasterUsername']} -p\n" ) print( "For more information, see the User Guide for Amazon RDS:\n" "\thttps://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.MySQL.html#CHAP_GettingStarted.Connecting.MySQL" ) print("-" * 88) def create_snapshot(self, instance_name): """ Shows how to create a DB instance snapshot and wait until it's available. :param instance_name: The name of a DB instance to snapshot. """ if q.ask( "Do you want to create a snapshot of your DB instance (y/n)? ", q.is_yesno ): snapshot_id = f"{instance_name}-{uuid.uuid4()}" print( f"Creating a snapshot named {snapshot_id}. This typically takes a few minutes." ) snapshot = self.instance_wrapper.create_snapshot(snapshot_id, instance_name) while snapshot.get("Status") != "available": wait(10) snapshot = self.instance_wrapper.get_snapshot(snapshot_id) pp(snapshot) print("-" * 88) def cleanup(self, db_inst, parameter_group_name): """ Shows how to clean up a DB instance and parameter group. Before the parameter group can be deleted, all associated DB instances must first be deleted. :param db_inst: The DB instance to delete. :param parameter_group_name: The DB parameter group to delete. """ if q.ask( "\nDo you want to delete the DB instance and parameter group (y/n)? ", q.is_yesno, ): print(f"Deleting DB instance {db_inst['DBInstanceIdentifier']}.") self.instance_wrapper.delete_db_instance(db_inst["DBInstanceIdentifier"]) print( "Waiting for the DB instance to delete. This typically takes several minutes." ) while db_inst is not None: wait(10) db_inst = self.instance_wrapper.get_db_instance( db_inst["DBInstanceIdentifier"] ) print(f"Deleting parameter group {parameter_group_name}.") self.instance_wrapper.delete_parameter_group(parameter_group_name) def run_scenario(self, db_engine, parameter_group_name, instance_name, db_name): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") print("-" * 88) print( "Welcome to the Amazon Relational Database Service (Amazon RDS)\n" "get started with DB instances demo." ) print("-" * 88) parameter_group = self.create_parameter_group(parameter_group_name, db_engine) self.update_parameters(parameter_group_name) db_inst = self.create_instance( instance_name, db_name, db_engine, parameter_group ) self.display_connection(db_inst) self.create_snapshot(instance_name) self.cleanup(db_inst, parameter_group_name) print("\nThanks for watching!") print("-" * 88) if __name__ == "__main__": try: scenario = RdsInstanceScenario(InstanceWrapper.from_client()) scenario.run_scenario( "mysql", "doc-example-parameter-group", "doc-example-instance", "docexampledb", ) except Exception: logging.exception("Something went wrong with the demo.")

Defina las funciones a las que llama el escenario para administrar las acciones de Amazon RDS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_parameter_group(self, parameter_group_name): """ Gets a DB parameter group. :param parameter_group_name: The name of the parameter group to retrieve. :return: The parameter group. """ try: response = self.rds_client.describe_db_parameter_groups( DBParameterGroupName=parameter_group_name ) parameter_group = response["DBParameterGroups"][0] except ClientError as err: if err.response["Error"]["Code"] == "DBParameterGroupNotFound": logger.info("Parameter group %s does not exist.", parameter_group_name) else: logger.error( "Couldn't get parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return parameter_group def create_parameter_group( self, parameter_group_name, parameter_group_family, description ): """ Creates a DB parameter group that is based on the specified parameter group family. :param parameter_group_name: The name of the newly created parameter group. :param parameter_group_family: The family that is used as the basis of the new parameter group. :param description: A description given to the parameter group. :return: Data about the newly created parameter group. """ try: response = self.rds_client.create_db_parameter_group( DBParameterGroupName=parameter_group_name, DBParameterGroupFamily=parameter_group_family, Description=description, ) except ClientError as err: logger.error( "Couldn't create parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response def delete_parameter_group(self, parameter_group_name): """ Deletes a DB parameter group. :param parameter_group_name: The name of the parameter group to delete. :return: Data about the parameter group. """ try: self.rds_client.delete_db_parameter_group( DBParameterGroupName=parameter_group_name ) except ClientError as err: logger.error( "Couldn't delete parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise def get_parameters(self, parameter_group_name, name_prefix="", source=None): """ Gets the parameters that are contained in a DB parameter group. :param parameter_group_name: The name of the parameter group to query. :param name_prefix: When specified, the retrieved list of parameters is filtered to contain only parameters that start with this prefix. :param source: When specified, only parameters from this source are retrieved. For example, a source of 'user' retrieves only parameters that were set by a user. :return: The list of requested parameters. """ try: kwargs = {"DBParameterGroupName": parameter_group_name} if source is not None: kwargs["Source"] = source parameters = [] paginator = self.rds_client.get_paginator("describe_db_parameters") for page in paginator.paginate(**kwargs): parameters += [ p for p in page["Parameters"] if p["ParameterName"].startswith(name_prefix) ] except ClientError as err: logger.error( "Couldn't get parameters for %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return parameters def update_parameters(self, parameter_group_name, update_parameters): """ Updates parameters in a custom DB parameter group. :param parameter_group_name: The name of the parameter group to update. :param update_parameters: The parameters to update in the group. :return: Data about the modified parameter group. """ try: response = self.rds_client.modify_db_parameter_group( DBParameterGroupName=parameter_group_name, Parameters=update_parameters ) except ClientError as err: logger.error( "Couldn't update parameters in %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response def create_snapshot(self, snapshot_id, instance_id): """ Creates a snapshot of a DB instance. :param snapshot_id: The ID to give the created snapshot. :param instance_id: The ID of the DB instance to snapshot. :return: Data about the newly created snapshot. """ try: response = self.rds_client.create_db_snapshot( DBSnapshotIdentifier=snapshot_id, DBInstanceIdentifier=instance_id ) snapshot = response["DBSnapshot"] except ClientError as err: logger.error( "Couldn't create snapshot of %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot def get_snapshot(self, snapshot_id): """ Gets a DB instance snapshot. :param snapshot_id: The ID of the snapshot to retrieve. :return: The retrieved snapshot. """ try: response = self.rds_client.describe_db_snapshots( DBSnapshotIdentifier=snapshot_id ) snapshot = response["DBSnapshots"][0] except ClientError as err: logger.error( "Couldn't get snapshot %s. Here's why: %s: %s", snapshot_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot def get_engine_versions(self, engine, parameter_group_family=None): """ Gets database engine versions that are available for the specified engine and parameter group family. :param engine: The database engine to look up. :param parameter_group_family: When specified, restricts the returned list of engine versions to those that are compatible with this parameter group family. :return: The list of database engine versions. """ try: kwargs = {"Engine": engine} if parameter_group_family is not None: kwargs["DBParameterGroupFamily"] = parameter_group_family response = self.rds_client.describe_db_engine_versions(**kwargs) versions = response["DBEngineVersions"] except ClientError as err: logger.error( "Couldn't get engine versions for %s. Here's why: %s: %s", engine, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return versions def get_orderable_instances(self, db_engine, db_engine_version): """ Gets DB instance options that can be used to create DB instances that are compatible with a set of specifications. :param db_engine: The database engine that must be supported by the DB instance. :param db_engine_version: The engine version that must be supported by the DB instance. :return: The list of DB instance options that can be used to create a compatible DB instance. """ try: inst_opts = [] paginator = self.rds_client.get_paginator( "describe_orderable_db_instance_options" ) for page in paginator.paginate( Engine=db_engine, EngineVersion=db_engine_version ): inst_opts += page["OrderableDBInstanceOptions"] except ClientError as err: logger.error( "Couldn't get orderable DB instances. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return inst_opts def get_db_instance(self, instance_id): """ Gets data about a DB instance. :param instance_id: The ID of the DB instance to retrieve. :return: The retrieved DB instance. """ try: response = self.rds_client.describe_db_instances( DBInstanceIdentifier=instance_id ) db_inst = response["DBInstances"][0] except ClientError as err: if err.response["Error"]["Code"] == "DBInstanceNotFound": logger.info("Instance %s does not exist.", instance_id) else: logger.error( "Couldn't get DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst def create_db_instance( self, db_name, instance_id, parameter_group_name, db_engine, db_engine_version, instance_class, storage_type, allocated_storage, admin_name, admin_password, ): """ Creates a DB instance. :param db_name: The name of the database that is created in the DB instance. :param instance_id: The ID to give the newly created DB instance. :param parameter_group_name: A parameter group to associate with the DB instance. :param db_engine: The database engine of a database to create in the DB instance. :param db_engine_version: The engine version for the created database. :param instance_class: The DB instance class for the newly created DB instance. :param storage_type: The storage type of the DB instance. :param allocated_storage: The amount of storage allocated on the DB instance, in GiBs. :param admin_name: The name of the admin user for the created database. :param admin_password: The admin password for the created database. :return: Data about the newly created DB instance. """ try: response = self.rds_client.create_db_instance( DBName=db_name, DBInstanceIdentifier=instance_id, DBParameterGroupName=parameter_group_name, Engine=db_engine, EngineVersion=db_engine_version, DBInstanceClass=instance_class, StorageType=storage_type, AllocatedStorage=allocated_storage, MasterUsername=admin_name, MasterUserPassword=admin_password, ) db_inst = response["DBInstance"] except ClientError as err: logger.error( "Couldn't create DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst def delete_db_instance(self, instance_id): """ Deletes a DB instance. :param instance_id: The ID of the DB instance to delete. :return: Data about the deleted DB instance. """ try: response = self.rds_client.delete_db_instance( DBInstanceIdentifier=instance_id, SkipFinalSnapshot=True, DeleteAutomatedBackups=True, ) db_inst = response["DBInstance"] except ClientError as err: logger.error( "Couldn't delete DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst

Acciones

En el siguiente ejemplo de código, se muestra cómo utilizar CreateDBInstance.

SDK para Python (Boto3)
nota

Hay más en marcha GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def create_db_instance( self, db_name, instance_id, parameter_group_name, db_engine, db_engine_version, instance_class, storage_type, allocated_storage, admin_name, admin_password, ): """ Creates a DB instance. :param db_name: The name of the database that is created in the DB instance. :param instance_id: The ID to give the newly created DB instance. :param parameter_group_name: A parameter group to associate with the DB instance. :param db_engine: The database engine of a database to create in the DB instance. :param db_engine_version: The engine version for the created database. :param instance_class: The DB instance class for the newly created DB instance. :param storage_type: The storage type of the DB instance. :param allocated_storage: The amount of storage allocated on the DB instance, in GiBs. :param admin_name: The name of the admin user for the created database. :param admin_password: The admin password for the created database. :return: Data about the newly created DB instance. """ try: response = self.rds_client.create_db_instance( DBName=db_name, DBInstanceIdentifier=instance_id, DBParameterGroupName=parameter_group_name, Engine=db_engine, EngineVersion=db_engine_version, DBInstanceClass=instance_class, StorageType=storage_type, AllocatedStorage=allocated_storage, MasterUsername=admin_name, MasterUserPassword=admin_password, ) db_inst = response["DBInstance"] except ClientError as err: logger.error( "Couldn't create DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst
  • Para obtener más información sobre la API, consulta la referencia de la API Create DBInstance in AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar CreateDBInstance.

SDK para Python (Boto3)
nota

Hay más en marcha GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def create_db_instance( self, db_name, instance_id, parameter_group_name, db_engine, db_engine_version, instance_class, storage_type, allocated_storage, admin_name, admin_password, ): """ Creates a DB instance. :param db_name: The name of the database that is created in the DB instance. :param instance_id: The ID to give the newly created DB instance. :param parameter_group_name: A parameter group to associate with the DB instance. :param db_engine: The database engine of a database to create in the DB instance. :param db_engine_version: The engine version for the created database. :param instance_class: The DB instance class for the newly created DB instance. :param storage_type: The storage type of the DB instance. :param allocated_storage: The amount of storage allocated on the DB instance, in GiBs. :param admin_name: The name of the admin user for the created database. :param admin_password: The admin password for the created database. :return: Data about the newly created DB instance. """ try: response = self.rds_client.create_db_instance( DBName=db_name, DBInstanceIdentifier=instance_id, DBParameterGroupName=parameter_group_name, Engine=db_engine, EngineVersion=db_engine_version, DBInstanceClass=instance_class, StorageType=storage_type, AllocatedStorage=allocated_storage, MasterUsername=admin_name, MasterUserPassword=admin_password, ) db_inst = response["DBInstance"] except ClientError as err: logger.error( "Couldn't create DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst
  • Para obtener más información sobre la API, consulta la referencia de la API Create DBInstance in AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar CreateDBParameterGroup.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def create_parameter_group( self, parameter_group_name, parameter_group_family, description ): """ Creates a DB parameter group that is based on the specified parameter group family. :param parameter_group_name: The name of the newly created parameter group. :param parameter_group_family: The family that is used as the basis of the new parameter group. :param description: A description given to the parameter group. :return: Data about the newly created parameter group. """ try: response = self.rds_client.create_db_parameter_group( DBParameterGroupName=parameter_group_name, DBParameterGroupFamily=parameter_group_family, Description=description, ) except ClientError as err: logger.error( "Couldn't create parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response
  • Para obtener más información sobre la API, consulta Crear un DBParameter grupo en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar CreateDBParameterGroup.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def create_parameter_group( self, parameter_group_name, parameter_group_family, description ): """ Creates a DB parameter group that is based on the specified parameter group family. :param parameter_group_name: The name of the newly created parameter group. :param parameter_group_family: The family that is used as the basis of the new parameter group. :param description: A description given to the parameter group. :return: Data about the newly created parameter group. """ try: response = self.rds_client.create_db_parameter_group( DBParameterGroupName=parameter_group_name, DBParameterGroupFamily=parameter_group_family, Description=description, ) except ClientError as err: logger.error( "Couldn't create parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response
  • Para obtener más información sobre la API, consulta Crear un DBParameter grupo en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar CreateDBSnapshot.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def create_snapshot(self, snapshot_id, instance_id): """ Creates a snapshot of a DB instance. :param snapshot_id: The ID to give the created snapshot. :param instance_id: The ID of the DB instance to snapshot. :return: Data about the newly created snapshot. """ try: response = self.rds_client.create_db_snapshot( DBSnapshotIdentifier=snapshot_id, DBInstanceIdentifier=instance_id ) snapshot = response["DBSnapshot"] except ClientError as err: logger.error( "Couldn't create snapshot of %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot
  • Para obtener más información sobre la API, consulta la referencia de la API Create DBSnapshot in AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar CreateDBSnapshot.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def create_snapshot(self, snapshot_id, instance_id): """ Creates a snapshot of a DB instance. :param snapshot_id: The ID to give the created snapshot. :param instance_id: The ID of the DB instance to snapshot. :return: Data about the newly created snapshot. """ try: response = self.rds_client.create_db_snapshot( DBSnapshotIdentifier=snapshot_id, DBInstanceIdentifier=instance_id ) snapshot = response["DBSnapshot"] except ClientError as err: logger.error( "Couldn't create snapshot of %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot
  • Para obtener más información sobre la API, consulta la referencia de la API Create DBSnapshot in AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DeleteDBInstance.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def delete_db_instance(self, instance_id): """ Deletes a DB instance. :param instance_id: The ID of the DB instance to delete. :return: Data about the deleted DB instance. """ try: response = self.rds_client.delete_db_instance( DBInstanceIdentifier=instance_id, SkipFinalSnapshot=True, DeleteAutomatedBackups=True, ) db_inst = response["DBInstance"] except ClientError as err: logger.error( "Couldn't delete DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst
  • Para obtener más información sobre la API, consulta Eliminar DBInstance en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DeleteDBInstance.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def delete_db_instance(self, instance_id): """ Deletes a DB instance. :param instance_id: The ID of the DB instance to delete. :return: Data about the deleted DB instance. """ try: response = self.rds_client.delete_db_instance( DBInstanceIdentifier=instance_id, SkipFinalSnapshot=True, DeleteAutomatedBackups=True, ) db_inst = response["DBInstance"] except ClientError as err: logger.error( "Couldn't delete DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst
  • Para obtener más información sobre la API, consulta Eliminar DBInstance en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DeleteDBParameterGroup.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def delete_parameter_group(self, parameter_group_name): """ Deletes a DB parameter group. :param parameter_group_name: The name of the parameter group to delete. :return: Data about the parameter group. """ try: self.rds_client.delete_db_parameter_group( DBParameterGroupName=parameter_group_name ) except ClientError as err: logger.error( "Couldn't delete parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • Para obtener más información sobre la API, consulta Eliminar DBParameter grupo en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DeleteDBParameterGroup.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def delete_parameter_group(self, parameter_group_name): """ Deletes a DB parameter group. :param parameter_group_name: The name of the parameter group to delete. :return: Data about the parameter group. """ try: self.rds_client.delete_db_parameter_group( DBParameterGroupName=parameter_group_name ) except ClientError as err: logger.error( "Couldn't delete parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • Para obtener más información sobre la API, consulta Eliminar DBParameter grupo en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeDBEngineVersions.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_engine_versions(self, engine, parameter_group_family=None): """ Gets database engine versions that are available for the specified engine and parameter group family. :param engine: The database engine to look up. :param parameter_group_family: When specified, restricts the returned list of engine versions to those that are compatible with this parameter group family. :return: The list of database engine versions. """ try: kwargs = {"Engine": engine} if parameter_group_family is not None: kwargs["DBParameterGroupFamily"] = parameter_group_family response = self.rds_client.describe_db_engine_versions(**kwargs) versions = response["DBEngineVersions"] except ClientError as err: logger.error( "Couldn't get engine versions for %s. Here's why: %s: %s", engine, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return versions

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeDBEngineVersions.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_engine_versions(self, engine, parameter_group_family=None): """ Gets database engine versions that are available for the specified engine and parameter group family. :param engine: The database engine to look up. :param parameter_group_family: When specified, restricts the returned list of engine versions to those that are compatible with this parameter group family. :return: The list of database engine versions. """ try: kwargs = {"Engine": engine} if parameter_group_family is not None: kwargs["DBParameterGroupFamily"] = parameter_group_family response = self.rds_client.describe_db_engine_versions(**kwargs) versions = response["DBEngineVersions"] except ClientError as err: logger.error( "Couldn't get engine versions for %s. Here's why: %s: %s", engine, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return versions

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeDBInstances.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_db_instance(self, instance_id): """ Gets data about a DB instance. :param instance_id: The ID of the DB instance to retrieve. :return: The retrieved DB instance. """ try: response = self.rds_client.describe_db_instances( DBInstanceIdentifier=instance_id ) db_inst = response["DBInstances"][0] except ClientError as err: if err.response["Error"]["Code"] == "DBInstanceNotFound": logger.info("Instance %s does not exist.", instance_id) else: logger.error( "Couldn't get DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst
  • Para obtener más información sobre la API, consulta la descripción DBInstances en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeDBInstances.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_db_instance(self, instance_id): """ Gets data about a DB instance. :param instance_id: The ID of the DB instance to retrieve. :return: The retrieved DB instance. """ try: response = self.rds_client.describe_db_instances( DBInstanceIdentifier=instance_id ) db_inst = response["DBInstances"][0] except ClientError as err: if err.response["Error"]["Code"] == "DBInstanceNotFound": logger.info("Instance %s does not exist.", instance_id) else: logger.error( "Couldn't get DB instance %s. Here's why: %s: %s", instance_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return db_inst
  • Para obtener más información sobre la API, consulta la descripción DBInstances en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeDBParameterGroups.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_parameter_group(self, parameter_group_name): """ Gets a DB parameter group. :param parameter_group_name: The name of the parameter group to retrieve. :return: The parameter group. """ try: response = self.rds_client.describe_db_parameter_groups( DBParameterGroupName=parameter_group_name ) parameter_group = response["DBParameterGroups"][0] except ClientError as err: if err.response["Error"]["Code"] == "DBParameterGroupNotFound": logger.info("Parameter group %s does not exist.", parameter_group_name) else: logger.error( "Couldn't get parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return parameter_group

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeDBParameterGroups.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_parameter_group(self, parameter_group_name): """ Gets a DB parameter group. :param parameter_group_name: The name of the parameter group to retrieve. :return: The parameter group. """ try: response = self.rds_client.describe_db_parameter_groups( DBParameterGroupName=parameter_group_name ) parameter_group = response["DBParameterGroups"][0] except ClientError as err: if err.response["Error"]["Code"] == "DBParameterGroupNotFound": logger.info("Parameter group %s does not exist.", parameter_group_name) else: logger.error( "Couldn't get parameter group %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return parameter_group

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeDBParameters.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_parameters(self, parameter_group_name, name_prefix="", source=None): """ Gets the parameters that are contained in a DB parameter group. :param parameter_group_name: The name of the parameter group to query. :param name_prefix: When specified, the retrieved list of parameters is filtered to contain only parameters that start with this prefix. :param source: When specified, only parameters from this source are retrieved. For example, a source of 'user' retrieves only parameters that were set by a user. :return: The list of requested parameters. """ try: kwargs = {"DBParameterGroupName": parameter_group_name} if source is not None: kwargs["Source"] = source parameters = [] paginator = self.rds_client.get_paginator("describe_db_parameters") for page in paginator.paginate(**kwargs): parameters += [ p for p in page["Parameters"] if p["ParameterName"].startswith(name_prefix) ] except ClientError as err: logger.error( "Couldn't get parameters for %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return parameters
  • Para obtener más información sobre la API, consulta la descripción DBParameters en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeDBParameters.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_parameters(self, parameter_group_name, name_prefix="", source=None): """ Gets the parameters that are contained in a DB parameter group. :param parameter_group_name: The name of the parameter group to query. :param name_prefix: When specified, the retrieved list of parameters is filtered to contain only parameters that start with this prefix. :param source: When specified, only parameters from this source are retrieved. For example, a source of 'user' retrieves only parameters that were set by a user. :return: The list of requested parameters. """ try: kwargs = {"DBParameterGroupName": parameter_group_name} if source is not None: kwargs["Source"] = source parameters = [] paginator = self.rds_client.get_paginator("describe_db_parameters") for page in paginator.paginate(**kwargs): parameters += [ p for p in page["Parameters"] if p["ParameterName"].startswith(name_prefix) ] except ClientError as err: logger.error( "Couldn't get parameters for %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return parameters
  • Para obtener más información sobre la API, consulta la descripción DBParameters en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeDBSnapshots.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_snapshot(self, snapshot_id): """ Gets a DB instance snapshot. :param snapshot_id: The ID of the snapshot to retrieve. :return: The retrieved snapshot. """ try: response = self.rds_client.describe_db_snapshots( DBSnapshotIdentifier=snapshot_id ) snapshot = response["DBSnapshots"][0] except ClientError as err: logger.error( "Couldn't get snapshot %s. Here's why: %s: %s", snapshot_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot
  • Para obtener más información sobre la API, consulta la descripción DBSnapshots en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeDBSnapshots.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_snapshot(self, snapshot_id): """ Gets a DB instance snapshot. :param snapshot_id: The ID of the snapshot to retrieve. :return: The retrieved snapshot. """ try: response = self.rds_client.describe_db_snapshots( DBSnapshotIdentifier=snapshot_id ) snapshot = response["DBSnapshots"][0] except ClientError as err: logger.error( "Couldn't get snapshot %s. Here's why: %s: %s", snapshot_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return snapshot
  • Para obtener más información sobre la API, consulta la descripción DBSnapshots en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeOrderableDBInstanceOptions.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_orderable_instances(self, db_engine, db_engine_version): """ Gets DB instance options that can be used to create DB instances that are compatible with a set of specifications. :param db_engine: The database engine that must be supported by the DB instance. :param db_engine_version: The engine version that must be supported by the DB instance. :return: The list of DB instance options that can be used to create a compatible DB instance. """ try: inst_opts = [] paginator = self.rds_client.get_paginator( "describe_orderable_db_instance_options" ) for page in paginator.paginate( Engine=db_engine, EngineVersion=db_engine_version ): inst_opts += page["OrderableDBInstanceOptions"] except ClientError as err: logger.error( "Couldn't get orderable DB instances. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return inst_opts

En el siguiente ejemplo de código, se muestra cómo utilizar DescribeOrderableDBInstanceOptions.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def get_orderable_instances(self, db_engine, db_engine_version): """ Gets DB instance options that can be used to create DB instances that are compatible with a set of specifications. :param db_engine: The database engine that must be supported by the DB instance. :param db_engine_version: The engine version that must be supported by the DB instance. :return: The list of DB instance options that can be used to create a compatible DB instance. """ try: inst_opts = [] paginator = self.rds_client.get_paginator( "describe_orderable_db_instance_options" ) for page in paginator.paginate( Engine=db_engine, EngineVersion=db_engine_version ): inst_opts += page["OrderableDBInstanceOptions"] except ClientError as err: logger.error( "Couldn't get orderable DB instances. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return inst_opts

En el siguiente ejemplo de código, se muestra cómo utilizar ModifyDBParameterGroup.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def update_parameters(self, parameter_group_name, update_parameters): """ Updates parameters in a custom DB parameter group. :param parameter_group_name: The name of the parameter group to update. :param update_parameters: The parameters to update in the group. :return: Data about the modified parameter group. """ try: response = self.rds_client.modify_db_parameter_group( DBParameterGroupName=parameter_group_name, Parameters=update_parameters ) except ClientError as err: logger.error( "Couldn't update parameters in %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response
  • Para obtener más información sobre la API, consulta Modificar DBParameter grupo en la referencia de la API del AWS SDK for Python (Boto3).

En el siguiente ejemplo de código, se muestra cómo utilizar ModifyDBParameterGroup.

SDK para Python (Boto3)
nota

Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de AWS.

class InstanceWrapper: """Encapsulates Amazon RDS DB instance actions.""" def __init__(self, rds_client): """ :param rds_client: A Boto3 Amazon RDS client. """ self.rds_client = rds_client @classmethod def from_client(cls): """ Instantiates this class from a Boto3 client. """ rds_client = boto3.client("rds") return cls(rds_client) def update_parameters(self, parameter_group_name, update_parameters): """ Updates parameters in a custom DB parameter group. :param parameter_group_name: The name of the parameter group to update. :param update_parameters: The parameters to update in the group. :return: Data about the modified parameter group. """ try: response = self.rds_client.modify_db_parameter_group( DBParameterGroupName=parameter_group_name, Parameters=update_parameters ) except ClientError as err: logger.error( "Couldn't update parameters in %s. Here's why: %s: %s", parameter_group_name, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise else: return response
  • Para obtener más información sobre la API, consulta Modificar DBParameter grupo en la referencia de la API del AWS SDK for Python (Boto3).

Escenarios

El siguiente ejemplo de código muestra cómo crear una aplicación web que haga un seguimiento de los elementos de trabajo en una base de datos Amazon Aurora Serverless y utilice Amazon Simple Email Service (Amazon SES) para enviar informes.

SDK para Python (Boto3)

Muestra cómo usarlo AWS SDK for Python (Boto3) para crear un servicio REST que rastrea los elementos de trabajo en una base de datos Amazon Aurora Serverless y envía informes por correo electrónico mediante Amazon Simple Email Service (Amazon SES). En este ejemplo se utiliza el marco web de Flask para gestionar el enrutamiento HTTP y se integra con una página web de React para presentar una aplicación web completamente funcional.

  • Cree un servicio REST de Flask que se integre con. Servicios de AWS

  • Lea, escriba y actualice los elementos de trabajo almacenados en una base de datos de Aurora Serverless.

  • Cree un AWS Secrets Manager secreto que contenga las credenciales de la base de datos y utilícelo para autenticar las llamadas a la base de datos.

  • Utilice Amazon SES para enviar informes de elementos de trabajo por correo electrónico.

Para obtener el código fuente completo y las instrucciones sobre cómo configurarlo y ejecutarlo, consulte el ejemplo completo en GitHub.

Servicios utilizados en este ejemplo
  • Aurora

  • Amazon RDS

  • Servicio de datos de Amazon RDS

  • Amazon SES

El siguiente ejemplo de código muestra cómo crear una aplicación web que haga un seguimiento de los elementos de trabajo en una base de datos Amazon Aurora Serverless y utilice Amazon Simple Email Service (Amazon SES) para enviar informes.

SDK para Python (Boto3)

Muestra cómo usarlo AWS SDK for Python (Boto3) para crear un servicio REST que rastrea los elementos de trabajo en una base de datos Amazon Aurora Serverless y envía informes por correo electrónico mediante Amazon Simple Email Service (Amazon SES). En este ejemplo se utiliza el marco web de Flask para gestionar el enrutamiento HTTP y se integra con una página web de React para presentar una aplicación web completamente funcional.

  • Cree un servicio REST de Flask que se integre con. Servicios de AWS

  • Lea, escriba y actualice los elementos de trabajo almacenados en una base de datos de Aurora Serverless.

  • Cree un AWS Secrets Manager secreto que contenga las credenciales de la base de datos y utilícelo para autenticar las llamadas a la base de datos.

  • Utilice Amazon SES para enviar informes de elementos de trabajo por correo electrónico.

Para obtener el código fuente completo y las instrucciones sobre cómo configurarlo y ejecutarlo, consulte el ejemplo completo en GitHub.

Servicios utilizados en este ejemplo
  • Aurora

  • Amazon RDS

  • Servicio de datos de Amazon RDS

  • Amazon SES

Ejemplos de tecnología sin servidor

En el siguiente ejemplo de código, se muestra cómo implementar una función de Lambda que se conecta a una base de datos de RDS. La función realiza una solicitud sencilla a la base de datos y devuelve el resultado.

SDK para Python (Boto3)
nota

Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el repositorio de ejemplos de tecnología sin servidor.

Conexión a una base de datos de Amazon RDS en una función de Lambda mediante Python.

import json import os import boto3 import pymysql # RDS settings proxy_host_name = os.environ['PROXY_HOST_NAME'] port = int(os.environ['PORT']) db_name = os.environ['DB_NAME'] db_user_name = os.environ['DB_USER_NAME'] aws_region = os.environ['AWS_REGION'] # Fetch RDS Auth Token def get_auth_token(): client = boto3.client('rds') token = client.generate_db_auth_token( DBHostname=proxy_host_name, Port=port DBUsername=db_user_name Region=aws_region ) return token def lambda_handler(event, context): token = get_auth_token() try: connection = pymysql.connect( host=proxy_host_name, user=db_user_name, password=token, db=db_name, port=port, ssl={'ca': 'Amazon RDS'} # Ensure you have the CA bundle for SSL connection ) with connection.cursor() as cursor: cursor.execute('SELECT %s + %s AS sum', (3, 2)) result = cursor.fetchone() return result except Exception as e: return (f"Error: {str(e)}") # Return an error message if an exception occurs

En el siguiente ejemplo de código, se muestra cómo implementar una función de Lambda que se conecta a una base de datos de RDS. La función realiza una solicitud sencilla a la base de datos y devuelve el resultado.

SDK para Python (Boto3)
nota

Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el repositorio de ejemplos de tecnología sin servidor.

Conexión a una base de datos de Amazon RDS en una función de Lambda mediante Python.

import json import os import boto3 import pymysql # RDS settings proxy_host_name = os.environ['PROXY_HOST_NAME'] port = int(os.environ['PORT']) db_name = os.environ['DB_NAME'] db_user_name = os.environ['DB_USER_NAME'] aws_region = os.environ['AWS_REGION'] # Fetch RDS Auth Token def get_auth_token(): client = boto3.client('rds') token = client.generate_db_auth_token( DBHostname=proxy_host_name, Port=port DBUsername=db_user_name Region=aws_region ) return token def lambda_handler(event, context): token = get_auth_token() try: connection = pymysql.connect( host=proxy_host_name, user=db_user_name, password=token, db=db_name, port=port, ssl={'ca': 'Amazon RDS'} # Ensure you have the CA bundle for SSL connection ) with connection.cursor() as cursor: cursor.execute('SELECT %s + %s AS sum', (3, 2)) result = cursor.fetchone() return result except Exception as e: return (f"Error: {str(e)}") # Return an error message if an exception occurs
PrivacidadTérminos del sitioPreferencias de cookies
© 2025, Amazon Web Services, Inc o sus afiliados. Todos los derechos reservados.