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. To provide users and applications with credentials for programmatic access to Amazon Keyspaces resources, you can do either of the following:

  • Create service-specific credentials that are associated with a specific AWS Identity and Access Management (IAM) user.

  • For enhanced security, we recommend to create IAM access keys for IAM users or roles that are used across all AWS services. The Amazon Keyspaces SigV4 authentication plugin for Cassandra client drivers enables you to authenticate calls to Amazon Keyspaces using IAM access keys instead of user name and password. For more information, see Create and configure AWS credentials for Amazon Keyspaces.

Before you begin

You need to complete the following task before you can start.

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.

Download the Starfield digital certificate using the following command and save sf-class2-root.crt locally or in your home directory.

curl https://certs.secureserver.net/repository/sf-class2-root.crt -O
Note

You can also use the Amazon digital certificate to connect to Amazon Keyspaces and can continue to do so if your client is connecting to Amazon Keyspaces successfully. The Starfield certificate provides additional backwards compatibility for clients using older certificate authorities.

curl https://certs.secureserver.net/repository/sf-class2-root.crt -O

Connect to Amazon Keyspaces using the Python driver for Apache Cassandra and service-specific credentials

The following code example shows you how to connect to Amazon Keyspaces with a Python client driver and service-specific credentials.

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/sf-class2-root.crt') 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/sf-class2-root.crt" 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 Create service-specific credentials for programmatic access to Amazon Keyspaces.

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

Connect to Amazon Keyspaces using the DataStax Python driver for Apache Cassandra and the SigV4 authentication plugin

The following section shows how to use the SigV4 authentication plugin for the open-source DataStax Python driver for Apache Cassandra to access Amazon Keyspaces (for Apache Cassandra).

If you haven't already done so, begin with creating credentials for your IAM role following the steps at Create and configure AWS credentials for Amazon Keyspaces. This tutorial uses temporary credentials, which requires an IAM role. For more information about temporary credentials, see Create temporary credentials to connect to Amazon Keyspaces using an IAM role and the SigV4 plugin.

Then, add the Python SigV4 authentication plugin to your environment from the GitHub repository.

pip install cassandra-sigv4

The following code example shows how to connect to Amazon Keyspaces by using the open-source DataStax Python driver for Cassandra and the SigV4 authentication plugin. The plugin depends on the AWS SDK for Python (Boto3). It uses boto3.session to obtain temporary credentials.

from cassandra.cluster import Cluster from ssl import SSLContext, PROTOCOL_TLSv1_2 , CERT_REQUIRED from cassandra.auth import PlainTextAuthProvider import boto3 from cassandra_sigv4.auth import SigV4AuthProvider ssl_context = SSLContext(PROTOCOL_TLSv1_2) ssl_context.load_verify_locations('path_to_file/sf-class2-root.crt') ssl_context.verify_mode = CERT_REQUIRED # use this if you want to use Boto to set the session parameters. boto_session = boto3.Session(aws_access_key_id="AKIAIOSFODNN7EXAMPLE", aws_secret_access_key="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", aws_session_token="AQoDYXdzEJr...<remainder of token>", region_name="us-east-2") auth_provider = SigV4AuthProvider(boto_session) # Use this instead of the above line if you want to use the Default Credentials and not bother with a session. # auth_provider = SigV4AuthProvider() 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/sf-class2-root.crt" with the path to the certificate saved in the first step.

  2. Ensure that the aws_access_key_id, aws_secret_access_key, and the aws_session_token match the Access Key, Secret Access Key, and Session Token you obtained using boto3.session. For more information, see Credentials in the AWS SDK for Python (Boto3).

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