Using azure servicebus with cognitive
TypeScriptIn Azure, Service Bus is a fully managed enterprise integration message broker. Service Bus can decouple applications and services. Azure Cognitive Services are APIs, SDKs, and services available to help developers build intelligent applications without having direct AI or data science skills or knowledge. Azure Service Bus and Azure Cognitive Services can be used together to build cloud applications that can react to events and messages and perform intelligent operations.
To demonstrate how you can use these Azure services with Pulumi, we will do the following:
- Set up a new Azure Service Bus namespace, which is a container for all messaging components.
- Create a topic within that namespace, which is used to send messages to multiple subscriptions.
- Set up a Cognitive Services account, which provides access to AI services like computer vision, language understanding, and more.
Below is a TypeScript program that sets up an Azure Service Bus Namespace along with a Topic and a Subscription. Additionally, it creates a Cognitive Services account that could later be used to process messages sent to and from the Service Bus.
import * as pulumi from "@pulumi/pulumi"; import * as azureNative from "@pulumi/azure-native"; // Create an Azure Resource Group const resourceGroup = new azureNative.resources.ResourceGroup("myResourceGroup"); // Create an Azure Service Bus Namespace const serviceBusNamespace = new azureNative.servicebus.Namespace("myServiceBusNamespace", { resourceGroupName: resourceGroup.name, sku: { name: "Standard", // Choose between Basic, Standard, and Premium }, location: resourceGroup.location, }); // Create a Service Bus Topic within the Namespace const topic = new azureNative.servicebus.Topic("myTopic", { resourceGroupName: resourceGroup.name, namespaceName: serviceBusNamespace.name, }); // Create a Cognitive Services Account const cognitiveServicesAccount = new azureNative.cognitiveservices.Account("myCognitiveServicesAccount", { resourceGroupName: resourceGroup.name, kind: "TextAnalytics", // Choose specific Cognitive Service (e.g., Face, TextAnalytics, Speech) sku: { name: "S0" // The pricing tier (e.g., F0 for free, S0 for Standard) }, location: resourceGroup.location, }); // Export the primary key of the Cognitive Services Account const cognitiveServicesKeys = pulumi.all([resourceGroup.name, cognitiveServicesAccount.name]) .apply(([resourceGroupName, accountName]) => azureNative.cognitiveservices.listAccountKeys({ accountName: accountName, resourceGroupName: resourceGroupName, })); const cognitiveServicesPrimaryKey = cognitiveServicesKeys.apply(keys => keys.key1); export const cognitiveServicesKey = cognitiveServicesPrimaryKey; export const serviceBusNamespaceName = serviceBusNamespace.name; export const serviceBusTopicName = topic.name; export const cognitiveServicesAccountName = cognitiveServicesAccount.name;
This program sets up the basic building blocks. Each block represents a resource within Azure:
- Resource Group: This is the logical container for Azure resources. Resources within the same group are managed together.
- Service Bus Namespace: Namespaces act as a container for all messaging components. Think of it as an isolated environment where you can send and receive messages.
- Service Bus Topic: Topics are similar to queues. However, with topics, you can have multiple subscriptions that can optionally filter messages.
- Cognitive Services Account: This is an instance of Cognitive Services, which allows you to access AI and data science capabilities provided by Azure.
The
export
statements at the end of the program provide output variables after deployment. You can use these outputs, for example, thecognitiveServicesKey
, to authenticate API calls to your cognitive services account.For a novice, this program serves as a starting point to understand the integration between Service Bus and Cognitive Services on Azure using Pulumi. You would continue by adding logic to subscribe to messages and send them to the Cognitive Service for processing, or reacting to events by adding event handlers and logic as needed for your application.