Using a Cassandra Python Client Driver to Access Amazon Keyspaces Programmatically - Amazon Keyspaces (for Apache Cassandra)

Using a Cassandra Python Client Driver to Access Amazon Keyspaces Programmatically

In this section, we show you how to connect to Amazon Keyspaces using a Python client driver. Amazon Keyspaces requires the use of Transport Layer Security (TLS) to help secure connections with clients. To connect to Amazon Keyspaces using TLS, you need to download an Amazon digital certificate and configure the Python driver to use TLS.

  1. Download the Amazon digital certificate using the following command and save it to the path_to_file/.cassandra directory.

    curl https://www.amazontrust.com/repository/AmazonRootCA1.pem -O
  2. Include the Amazon digital certificate in your Python application. For example:

    from cassandra.cluster import Cluster from ssl import SSLContext, PROTOCOL_TLSv1_2 , CERT_REQUIRED from cassandra.auth import PlainTextAuthProvider ssl_context = SSLContext(PROTOCOL_TLSv1_2 ) ssl_context.load_verify_locations('path_to_file/AmazonRootCA1.pem') ssl_context.verify_mode = CERT_REQUIRED auth_provider = PlainTextAuthProvider(username='ServiceUserName', password='ServicePassword') cluster = Cluster(['cassandra.us-east-2.amazonaws.com'], ssl_context=ssl_context, auth_provider=auth_provider, port=9142) session = cluster.connect() r = session.execute('select * from system_schema.keyspaces') print(r.current_rows)

    Usage notes:

    1. Replace "path_to_file/AmazonRootCA1.pem" with the path to the certificate saved in the first step.

    2. Ensure that the ServiceUserName and ServicePassword match the user name and password you obtained when you generated the service-specific credentials by following the steps to Generate Service-Specific Credentials.

    3. For a list of available endpoints, see Service Endpoints for Amazon Keyspaces.