Provisions Azure Service Bus Resources, Using Pulumi
Nowadays it is very common that developers use one of the cloud service providers, such as Azure or AWS. One of the challenges working with those services in an enterprise application is how to manage the resources one may require in the cloud; for instance, Azure has a messaging service, Azure Service Bus, and you could bind Azure Functions to Topics and Queues within the bus. Azure functions will not start though, unless those topics and queues exist. By using pulumi in a CI/CD pipeline you could make sure that you always have the required resources before the deployment happens. Let’s see more of pulumi in action.
Pulumi as they mentioned in their documentation is a modern infrastructure as code platform that allows you to use familiar programming languages and tools to build, deploy, and manage cloud infrastructure. That means, a developer can use the programming language of her or his interest to manage cloud resources, and pulumi will also track changes of those resources for you based on your code base and the stack it creates.
In this article I want to show how to manage topics of Azure Service Bus leveraging pulumi and using C#, you could choose other languages. Let’s begin. :)
To start, create an empty folder, and navigate from your preferred command line tool in that directory and run the following command, and follow the instructions, if you want to use default values pass
-y to the command.
pulumi new azure-csharp
This will create a project that could be used to create azure resources. There are 3 important files,
MyStack.cs. the first one includes general and common configurations for your pulumi stack, the second one, includes configurations specific to a special stack, in this case,
dev. This could differ if you have chosen another stack name during creation.
MyStack.cs contains the C# code to create pulumi stack and consequently azure resources. Pulumi uses changes in this file to track which cloud resources should be created, deleted, or updated on your cloud platform.
To create any resources on Azure, we need to have a resource group. Currently there should be one in
var resourceGroup = new ResourceGroup("resourceGroup");
Keep this line in the file and remove the rest; of course, remove those that will not break the program from compiling :wink:.
Next step is to create an azure service bus namespace, this is a place where later on we could create topics and queues there.
var serviceBusNamespace = new Pulumi.AzureNative.ServiceBus.Namespace("sbns-pulumi-sample",
PS: For naming conventions check this link.
Now let’s get back to the terminal and run
pulumi up, you should see an output similar to this:
Previewing update (dev)
You will see that pulumi wants to
create two resources, a
ResourceGroup and a
Namespace for service bus, choose details if you want to see more details about what will happen on this command, otherwise select
yes and wait for the command to finish. The following should be the output:
Do you want to perform this update? yes
Now we can add our topics and subscriptions to the service bus namespace, use the code below to tell pulumi to create those resources and run
var topic = new Pulumi.AzureNative.ServiceBus.Topic("sbt-pulumi-sample", new TopicArgs
This time, you will see that pulumi detects only the changes you made and will only create the new resources; you will see an output similar to the one below:
Previewing update (dev)
yes to create all the new resources. then you could navigate to portal, there you should be able to see all the resources create by pulumi. If you are interested to see the stack that pulumi has created for you navigate to pulumi app, log into your account and then on the home page you will see the list of stacks.
To cleanup all the creates resources in this sample run
pulumi destroy, to delete all the resources create on the cloud, and then, if you want to totally remove the pulumi stack as well run
pulumi stack rm dev.
Pulumi facilitates the way developers can manage the resources on their cloud infrastructure, one of the great benefits of that is tracking changes made to the infrastructure and integrate these operations within the CI/CD pipeline, no matter which framework or language you are using, C# or python, Azure or AWS.
If you found this article useful please share it, and I hope you have a bug free coding week ahead :). You could find the code for this blog in here on GitHub.