Azure Cosmos DB Serverless Preview


In today’s episode, we explore the next generation Microsoft Azure provider for Pulumi. We take it for a spin on Azure’s Cosmos DB serverless preview and show how easy it is to create a database and wire up the connection string information to a client. Code for the episode can be found here.


  • Lee Zen
    VP Engineering, Pulumi

What you'll learn

  • Create a Cosmos DB account.
  • Provision a database and container.
  • Setup a client to interact with that container.
Show video transcript

Hello, and welcome to another episode of Modern Infrastructure Wednesday. I’m your host, Lee Zen, and today we’re going to be covering the Azure Cosmos DB Serverless Preview along with Pulumi’s new Azure Next-Gen Provider. So, what are we going to do today? We’re going to be covering creation of a Cosmos DB account, provisioning a database container, and then also setting up a client to interact with that container. So, very basic stuff, but really just excited to show off the new next-gen provider that we’ve built that really covers the full surface area of all those Azure resources that you want to use. And really, the ability to use any feature on day one, once it’s announced because we’re compiling our provider based on the actual Azure REST API specs. So, let’s get started.

I have a TypeScript program here, so you can see we’re importing from our new Azure next-gen provider here. Then, I’m also importing from the Azure Cosmos Client. I already have everything prebuilt because I don’t want to spend any time on the provisioning steps. So, you can see this looks and behaves very similar to the previous Azure provider. You create a resource group, you create a document DB database account. We give it the parameters we want. In particular of note here is that you can actually, again, because we’re reflecting the full surface area of that resource model in the Azure REST API, we can give this a capabilities property with the name EnableServerless, and this will enable the serverless preview for Cosmos DB.

Everything else after that is fairly standard. We create a SQL database in that Cosmos DB account, and then we create a container to interact with it as always. Then, finally, you could do a number of things with these outputs now, so at the end of this, we’ll have created that account, the database, and then the container, and then you can then get back to the connection string. So, you can see here, again, using the new provider, we can actually make a call to list database account connection strings, which is again, part of that REST API surface area that we’ve modeled. So, we can actually get back those connection strings. You could totally imagine taking that and passing that on to something else. So, passing those connection strings onto, for example, a function or something like that, or storing it as a sequence so you can use it later on in your web app.

In our particular case here, I’m just doing something super simple. I’m using the Cosmos Client that I’ve imported from above, and then I’m just going to insert a single item here with just some key and some value. So, if we run this program, we’ve already gone ahead and all these resources were already created, so there’s really not too much to show there. As you can see, there’s no changes. But if I say yes, this particular apply will actually run and we should see an additional item in our data store. So, if we go here and this is the items, this is the serverless DB that I created, and if I refresh my items here, you can see that I have a third item now, and that has that timestamp inserted in.

So, really just wanted to go through the basics of using the new next-gen provider with Pulumi and how easy it is to use it and how easy it is to actually wire things in and get your applications up and running on the new preview for Azure Cosmos DB. I hope you enjoyed today’s episode of Modern Infrastructure Wednesday. Please make sure to subscribe to PulumiTV for future updates and leave your comments below and like the video, if you enjoyed today’s episode and I hope to see you next week on PulumiTV. Thanks very much.

Learn more

Discover the getting started guides and learn about Pulumi concepts.

Explore the docs →

Pulumi AI

Generate Pulumi infrastructure-as-code programs in any language.

Try Pulumi AI →