Jump toUpdate content

Scaleway Python software developement kit (SDK)

Reviewed on 14 November 2023Published on 14 November 2023

The Scaleway Python SDK offers an efficient way for developers to interact with Scaleway’s cloud services programmatically. This powerful tool simplifies the automation of various tasks, including server management, storage operations, and networking setup. It encapsulates these functions into user-friendly Python method invocations by taking care of the complexity of direct API calls and authentication. This makes it easier to integrate Scaleway’s cloud resources into applications or manage these resources directly from code.

Installation of the Python SDK

The SDK caters to different programming needs by offering both synchronous and asynchronous versions. You can easily install the version that suits your project’s requirements:

For the synchronous version:

pip install scaleway

For the asynchronous version:

pip install scaleway-asynchronous
Note:

While this library is optimized for Python 3.8, it maintains compatibility with newer Python versions, ensuring a broad scope of applicability.

Initialization

You need a pair of access and secret keys to connect to Scaleway API. Check out the documentation to find out how to retrieve them.

scaleway APIs must be initialized with a scaleway.Client.

A minimal setup would look like this:

from scaleway import Client
from scaleway.registry.v1 import RegistryV1API
client = Client(
access_key="SCWXXXXXXXXXXXXXXXXX",
secret_key="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
default_project_id="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
default_region="fr-par",
default_zone="fr-par-1",
)
registry_api = RegistryV1API(client)

Example

To create a Kubernetes cluster on Scaleway using the Python SDK, you would typically interact with the scaleway SDK package. The specific details can change, so you should always refer to the latest documentation. Below is a conceptual example of how you might use the Scaleway Python SDK to create a Kubernetes (Kapsule) cluster with 3 nodes:

import time
from scaleway import Client
from scaleway.k8s.v1.api import K8SV1API
from scaleway.vpc.v2.api import VpcV2API
ACCESS_KEY = "<SCW_ACCESS_KEY>""
SECRET_KEY = "<SCW_SECRET_KEY>"
PROJECT_ID = "<SCW_PROJECT_ID>"
REGION = "fr-par"
ZONE = "fr-par-1"
CLUSTER_NAME = "my-k8s-cluster"
NODE_TYPE = "PLAY2-NANO"
NUMBER_OF_NODES = 3
KUBERNETES_VERSION = "1.28.2"
MAX_SIZE = 5
# Initialize the Scaleway client with credentials and configuration
client = Client(
access_key=ACCESS_KEY,
secret_key=SECRET_KEY,
default_project_id=PROJECT_ID,
default_region=REGION,
default_zone=ZONE
)
# Initialize APIs
k8s_api = K8SV1API(client)
vpc_api = VpcV2API(client)
# Create a new Private Network for the Kubernetes cluster
try:
pn = vpc_api.create_private_network(
region=REGION,
project_id=PROJECT_ID,
tags=["kube-cluster-pn"]
)
pn_id = pn.id
print(f"Private Network created with ID: {pn_id}")
except Exception as e:
raise Exception(f"Failed to create Private Network: {e}")
time.sleep(5)
# Note the time.sleep(5) command adds a five seconds of wait for the network and later the cluster to be ready.
# For demonstrations or simple scripts where efficiency and responsiveness are not as critical, time.sleep() might be sufficient, but for production-quality code, it's essential to use more robust and responsive techniques.
# Create a new Kubernetes cluster
try:
cluster = k8s_api.create_cluster(
type_="kapsule",
name=CLUSTER_NAME,
version=KUBERNETES_VERSION,
region=REGION,
description=CLUSTER_NAME,
cni="cilium",
tags=["my-kube-cluster"],
private_network_id=pn_id
)
cluster_id = cluster.id
except Exception as e:
raise Exception(f"Failed to create Kubernetes cluster: {e}")
time.sleep(5)
# Create the node pool for the Kubernetes cluster
try:
node_pool = k8s_api.create_pool(
cluster_id=cluster_id,
name=f"{CLUSTER_NAME}-pool",
node_type=NODE_TYPE,
size=NUMBER_OF_NODES,
min_size=1,
max_size=MAX_SIZE,
autoscaling=True,
tags=["my-node-pool"],
region=REGION,
container_runtime='containerd',
autohealing=True,
root_volume_size=20000000000,
root_volume_type='b_ssd'
)
node_pool_id = node_pool.id
except Exception as e:
raise Exception(f"Failed to create node pool: {e}")
print(f"Cluster created with ID: {cluster_id}")
print(f"Node pool created with ID: {node_pool_id}")

Further resources

For further information about the Scaleway Python SDK, refer to the offical SDK repository on GitHub.