Explore Azure SDKs for Python , .NET and JavaScript
Intro
Today we will explore the power of Azure SDKs, Software Development Kits, from the most used and widespread programming languages like Python, .NET and JavaScript. The aim is to provide you with practical insights and code snippets that bring Azure’s capabilities to your fingertips. Whether you’re a mature developer or just starting out, this guide will enhance your understanding and use of Azure SDKs.
Overview of Azure SDKs
But what are exactly Azure SDKs ? The Azure SDKs are collections of libraries built to make it easier to use Azure services from your language of choice. These libraries are designed to be consistent, approachable, diagnosable, dependable, and idiomatic. Azure SDKs are designed to streamline the process of integrating Azure services into our applications. These SDKs provide developers with pre-written code, tools, and libraries that make it easier to interact with Azure’s vast array of services. Whether it’s managing storage, securing applications with KeyVault, orchestrating compute resources, or handling complex networking tasks, SDKs encapsulate much of the necessary heavy lifting.
One of Azure SDKs’ greatest strengths is their support for a wide range of programming languages and platforms. This inclusive approach allows developers from different backgrounds and with varying expertise to take advantage of Azure’s cloud capabilities. As you may understand the field is vast ! We have SDK for iOS, for Python for Go and so on! So let’s focus on three key languages: Python, .NET, and JavaScript. Each of these languages has a dedicated set of Azure SDKs, tailored to fit their distinctive styles and best practices.
Key Azure SDKs and examples
Let’s start with Python! Python’s Azure SDKs bring simplicity and efficiency to cloud operations. The DefaultAzureCredential
class from the azure-identity
package is a cornerstone for authentication, automatically selecting the best available credential type based on the environment. For example let’s have a look at Storage. It is a common task to authenticate to Azure Storage and we can do it with a few lines :
from azure.storage.blob import BlobServiceClient
from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
blob_service_client = BlobServiceClient(account_url="https://<your_account>.blob.core.windows.net", credential=credential)
If we want to break it down :
- Importing Necessary Modules:
from azure.storage.blob import BlobServiceClient
: This imports theBlobServiceClient
class, which is used to interact with the Blob Storage service.from azure.identity import DefaultAzureCredential
: This imports theDefaultAzureCredential
class, which provides a seamless way to authenticate with Azure services, especially when your code is running on Azure.
- Setting Up Authentication:
credential = DefaultAzureCredential()
: This line creates an instance ofDefaultAzureCredential
. This class automatically selects the best available authentication method based on the environment your code is running in. For example, it might use managed identity in an Azure-hosted environment or a developer’s credentials when running locally.
- Creating the Blob Service Client:
blob_service_client = BlobServiceClient(account_url="https://<your_account>.blob.core.windows.net", credential=credential)
: This line creates an instance ofBlobServiceClient
, which is used to perform operations on Blob Storage. You need to replace<your_account>
with your Azure Storage account name. Thecredential
argument is passed theDefaultAzureCredential
instance for authentication.
Example of Azure SDKs with Key Vault
Another well known core service is Azure Key Vault. The azure-keyvault-secrets
package manages secrets. Authenticate and create a KeyVault client as follows:
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
credential = DefaultAzureCredential()
secret_client = SecretClient(vault_url="https://<your-vault-name>.vault.azure.net/", credential=credential)
In a similar manner, for example, managing virtual machines and networking, use azure-mgmt-compute
and azure-mgmt-network
. The client setup is similar, utilizing DefaultAzureCredential
for authentication.
Azure SDKs for .NET
Moving on to .NET SDK. Azure SDKs for .NET integrate seamlessly with the .NET ecosystem, offering a familiar and powerful environment for managing Azure resources.
The Azure SDK for .NET is designed to make it easy to use Azure services from your .NET applications. Whether it is uploading and downloading files to Blob Storage, retrieving application secrets from Azure Key Vault, or processing notifications from Azure Event Hubs, the Azure SDK for .NET provides a consistent and familiar interface to access Azure services. It is available as series of NuGet packages that can be used in both .NET Core (2.1 and higher) and .NET Framework (4.7.2 and higher) applications.(1)
If we wanted to create a client for Azure Storage:
using Azure.Identity;
using Azure.Storage.Blobs;
var credential = new DefaultAzureCredential();
var blobServiceClient = new BlobServiceClient(new Uri("https://<your_account>.blob.core.windows.net"), credential);
If we want to implement Logging to the console:
using AzureEventSourceListener listener =
AzureEventSourceListener.CreateConsoleLogger();
To manage secrets in KeyVault, use the Azure.Security.KeyVault.Secrets
namespace. Client initialization is straightforward:
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
var credential = new DefaultAzureCredential();
var secretClient = new SecretClient(new Uri("https://<your-vault-name>.vault.azure.net/"), credential);
Finally JavaScript! Azure’s JavaScript SDKs are tailored for modern web development, offering easy integration with Azure services in Node.js applications.
So, in our example with Storage, the @azure/storage-blob
package is used for interacting with blob storage:
const { BlobServiceClient } = require("@azure/storage-blob");
const { DefaultAzureCredential } = require("@azure/identity");
const credential = new DefaultAzureCredential();
const blobServiceClient = new BlobServiceClient(`https://${yourAccount}.blob.core.windows.net`, credential);
It is a common usage for Node.js to take the role of the Frontend or Backend Application due to the flexibility and range of use cases. In the KeyVault example let’s see an extended version where we create and get our secrets:
const { SecretClient } = require("@azure/keyvault-secrets");
const { DefaultAzureCredential } = require("@azure/identity");
// Replace 'yourVaultName' with your Key Vault name
const vaultName = "yourVaultName";
const url = `https://${vaultName}.vault.azure.net/`;
const credential = new DefaultAzureCredential();
const secretClient = new SecretClient(url, credential);
async function main() {
// Secret to store in the Key Vault
const secretName = "mySecretName";
const secretValue = "mySecretValue";
// Storing a secret
console.log(`Storing secret: ${secretName}`);
await secretClient.setSecret(secretName, secretValue);
console.log(`Secret stored: ${secretName}`);
// Retrieving the stored secret
console.log(`Retrieving stored secret: ${secretName}`);
const retrievedSecret = await secretClient.getSecret(secretName);
console.log(`Retrieved secret: ${retrievedSecret.name} with value: ${retrievedSecret.value}`);
// Deleting the secret (optional)
console.log(`Deleting secret: ${secretName}`);
await secretClient.beginDeleteSecret(secretName);
console.log(`Secret deleted: ${secretName}`);
}
main().catch((error) => {
console.error("An error occurred:", error);
process.exit(1);
});
In this expanded example:
- A secret named
mySecretName
with the valuemySecretValue
is created and stored in Azure Key Vault. - The
SecretClient
is used to interact with the Key Vault. It is initialized with the vault URL and a credential object, which in this case is obtained fromDefaultAzureCredential
. - The
setSecret
method stores the secret in the Key Vault. - The
getSecret
method retrieves the secret from the Key Vault. - Optionally, the
beginDeleteSecret
method is used to delete the secret. Note that this deletion process may be delayed as it involves a recovery period; the secret isn’t immediately removed from the Key Vault.
Putting it all together
In Azure, we benefit from exceptional flexibility through a variety of resources designed to host and manage our applications effectively. Given the modern trend towards microservices and containerization, it’s common to see a combination of diverse SDKs, each contributing to a larger project framework. This architecture typically involves distinct components such as a Frontend, a Backend, and potentially a Middleware layer. Each component serves a specific role, seamlessly integrating as part of a comprehensive application solution. Azure’s robust infrastructure supports this modular approach, enabling scalable, efficient, and highly customizable application development.
Let’s see an example, shall we?
Frontend-Node.js
The frontend is built using Node.js and Express. It serves as the user interface and communicates with the Python middleware.
const express = require('express');
const axios = require('axios');
const app = express();
const port = 3000;
app.get('/data', async (req, res) => {
try {
// Communicate with Python middleware
const response = await axios.get('http://localhost:5000/process');
res.send(response.data);
} catch (error) {
res.status(500).send('Error communicating with middleware');
}
});
app.listen(port, () => {
console.log(`Frontend listening at http://localhost:${port}`);
});
Node.js is excellent for building lightweight frontend/backend services. Here, it’s used to handle HTTP requests and communicate with the middleware. The simplicity and non-blocking nature of Node.js make it ideal for such tasks.
Azure SDKs: Middleware-Python
The middleware is a Python Flask application. It acts as an intermediary, processing data from the frontend and communicating with the .NET backend.
from flask import Flask, jsonify
import requests
app = Flask(__name__)
@app.route('/process', methods=['GET'])
def process_data():
try:
# Communicate with .NET backend
response = requests.get('http://localhost:6000/data')
processed_data = response.json() # Example of data processing
return jsonify(processed_data)
except:
return jsonify({"error": "Failed to communicate with backend"}), 500
if __name__ == '__main__':
app.run(port=5000)
Python’s simplicity and powerful libraries make it a good choice for middleware. In this case, it’s used to perform intermediate processing and orchestrate communication between the frontend and the backend.
Azure SDKs: Backend-.NET
The backend is developed using .NET Core, providing data to the middleware. It could also be integrated with Azure services like Azure SQL Database or Azure Blob Storage for data persistence.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
app.Run("http://localhost:6000");
[ApiController]
[Route("[controller]")]
public class DataController : ControllerBase
{
[HttpGet]
public IActionResult GetData()
{
// Example data from the backend
var data = new { Message = "Data from .NET Backend" };
return Ok(data);
}
}
.NET Core is robust and scalable, suitable for building complex backend systems. It can efficiently handle database connections, business logic, and other backend processes.
Integration and Azure SDK Usage
- The Node.js frontend serves as the entry point for user requests, which it forwards to the Python middleware.
- The Python middleware processes the request and then communicates with the .NET backend, which could be integrated with Azure services for enhanced functionality.
- The .NET backend could utilize Azure SDKs, like Azure Storage SDK for storing data or Azure Cognitive Services for AI processing.
- Each component communicates over HTTP, demonstrating a microservices architecture. This approach allows each part to be scaled and maintained independently.
An example of the Architecture is showcased in the following example:
Best Practices
What is the number one thing we should do when building our Solutions and Projects ? Following Best Practices! Here are some best practices:
- Stay Updated with SDK Versions: Azure SDKs are frequently updated to introduce new features and fix bugs. Regularly updating your SDKs ensures you have the latest improvements and security patches. However, keep an eye on release notes for any breaking changes.
- Use
DefaultAzureCredential
for Simplified Authentication: This class simplifies the authentication process across various environments (local development, deployment in Azure, etc.), making your code more versatile and secure. - Error Handling: Proper error handling is crucial. Azure SDKs throw exceptions for service-side issues. Implement try-catch blocks to handle these exceptions gracefully, ensuring your application remains stable and provides useful feedback to the user.
- Asynchronous Programming: Many Azure SDKs offer asynchronous methods. Utilize these to improve the scalability and responsiveness of your applications, especially when dealing with I/O-bound operations.
- Resource Management: Be mindful of resource creation and management. Clean up resources that are no longer needed to avoid unnecessary costs and maintain an efficient cloud environment.
- Utilize SDK Core Features: Azure SDKs provide core functionalities like retries, logging, and telemetry. Familiarize yourself with these features to enhance your application’s reliability and maintainability.
- Leverage Community and Documentation: The Azure SDKs are well-documented, with a wealth of examples and guidance. Additionally, the community around Azure SDKs is a valuable resource for best practices, troubleshooting, and staying updated with the latest trends.
Closing
Azure SDKs are powerful tools in our development arsenal, simplifying the complexity of cloud services. Staying informed, following best practices, and leveraging these SDKs, we can unlock the full potential of Azure Cloud, making our cloud journey productive, secure, and efficient. Happy coding!