In today's world as a developer, you need cloud infrastructure and services to test code. Whether you need a virtual machine or a container, one fact still holds true, you need the ability to create resources for running code. Several Infrastructure-as-code (IaS) and Configuration Management tools exist, but what if you don't want to learn one of those languages? What if you want to stick with a programming language to deploy cloud services? Well, you can! With a new platform called Pulumi.

Pulumi takes code written in Python, .NET Core, , Golang, and Node.JS and turns it into Infrastructure-as-code. Not only does it turn code into IaS, but Pulumi offers a very clean UI that you can manage deployments from for Azure, AWS, and Google Cloud. In this blog post you will create Azure services using Python3 code and Pulumi to run the Python3 to create said services.

Prerequisites

To follow along with this blog, you will need the following:

  • Python3 beginner to intermediate level experience
  • Python3 installed which you can find here
  • An Azure account. You can sign up for a free 30-day Azure trial here.
  • A free GitHub account which you can sign up for here.
  • On Windows, Chocolatey must be installed. On Mac, Brew must be installed.

Installing Pulumi

Before writing code to run in Pulumi, you have to install the tool first. Depending on the operating system, there are a few ways to install. In this section you will take a look at how to install Pulumi on both Mac and Windows.

Mac

Installing Pulumi on OS X can be done via brew, an open-source package manager for OS X.

To install Pulumi via brew, open up a terminal and run the following command in a terminal.

brew install pulumi

If the installation of Pulumi was successful, you will see an output similar to the screenshot below.

Windows

Installing Pulumi on Windows can be done via chocolatey, an open-source package manager for Windows.

To install Pulumi via Chocolatey, open up PowerShell as an administrator and run the following command in a terminal.

choco install pulumi

If the installation of Pulumi was successful, you will see an output similar to the screenshot below.

Signing up for Pulumi

One of the best things about Pulumi is that they make it easy to sign up. You can use an email to sign up, but the easiest option for authentication is to sign up with GitHub. Once Pulumi is authorized with GitHub, you can use GitHub credentials to sign in. In this section you will go through the sign-up and authentication process for Pulumi.

You don't have to use GitHub as it's not a Pulumi requirement, but this blog post will be using the GitHub authentication. If you are following along, some components of the steps will differ.

Open up a web browser and go to Pulumi's website to start the authentication process.

Click the red SIGN IN button.

Under the Sign in with one of the following: menu, choose GitHub to complete the authentication.

After clicking the GitHub button, you will see a screenshot similar to the below.

Deploying Infrastructure with Pulumi

In the previous section you signed up for Pulumi and used a GitHub account for authentication. Now that you are signed into Pulumi, it's time to start creating some Azure services! You will first see how to created a new project, authenticate to Azure, then use Python to create an Azure service.

Create a New Project in Pulumi

Click on the blue + NEW PROJECT button to start a new Pulumi project.

As shown in the screenshot below, you will see many options that Pulumi provides for choosing a cloud and what programming language to use.

For the cloud, choose Azure and for the programming language, choose Python. Doing so will allow you to deploy resources into Azure with Python. Once you have chosen Azure and Python, click the blue Next button.

Under the New Project window, you can keep every default. Although the default settings aren't a requirement, it's fine to use as this is a test/dev project. Click the blue CREATE PROJECT button once you have reviewed the options.

Creating the Pulumi Environment

Next you will create a local directory that will store the Python3 code that Pulumi will run to create an Azure resource.

The screenshot shown below is similar to a screen that you will see, except the GitHub repository that is shown on your screen will be different.

Open up a terminal and cd to the home directory for creating the azure-python directory. Although the home directory isn't a requirement, that's what will be used in this section.

Run the following command to create a new directory.

mkdir azure-python

cd to the new directory by running the following command.

cd azure-python

Next run the code below to create and pull down the new project by using the pulumi new command to initiate the azure-python directory for Pulumi.

pulumi new azure-python -s your_github_org/azure-python/dev

You will see a screenshot similar to the one below asking to enter an access token.

Generating a New Pulumi Access Token

Open up a web browser then copy/paste https://app.pulumi.com/account/tokens to create and retrieve an access token. You should see a screenshot similar to the one below.

Click on the NEW ACCESS TOKEN button to generate a new access token.

Choose a description then click on the blue CREATE button.

You will then see a new access token like in the screenshot below. Copy the access token and go back to the terminal.

Within the terminal, paste in the new access token and click enter. You will see a screenshot similar to the one below letting you know that Pulumi has authenticated.

Creating an Initial Deployment

In the terminal, it's time to create an initial deployment that will create a resource group using Python for the Azure services to reside in.

Create a Python Virtual Environment (venv), which is an environment that segregates itself from the local machine in a virtualized fashion. For example, let's say you install a Python library in venv, the library will only exist in venv and not on the localhost. Run the following command to create the venv.

python3 -m venv venv

To activate the virtual environment, run the following command.

source venv/bin/activate

After the venv is activated, you will see that the terminal changes a bit and shows (venv) in the beginning.

Finally, install the venv dependencies to ensure the virtual environment is running properly and the Pulumi Python library is installed. In the terminal run the following.

pip3 install -r requirements.txt

You will see an output similar to the screenshot below. Notice that the Python library for Pulumi as been installed.

Running the Pulumi Deployment

Now that the Pulumi deployment, virtual environment, and dependencies have been created, it's time to deploy!

Run the following command to create the initial deployment in Azure.

pulumi up

Using the keyboard arrows, hit the up arrow to create the new resources. The new resources that are created is a resource group and storage account for the Pulumi resources to reside in.

After the resources have been created, you will see a screenshot similar to the one below showing you any outputs/connection strings and what was created.

Open up a web browser and go to the Azure portal

Click on Resource Groups.

You will now see the resource group that was created by Pulumi!

Congratulations! You have officially created Pulumi resources and are now ready to start running Python3 code with Pulumi

Conclusion

In this blog post you took a hands-on approach to create a Pulumi account and hit the ground running by created a resource group and storage account. You learned about the authentication methods provided by Pulumi and used the GitHub method to authenticate. Following that you jumped into the Pulumi dashboard and created a new project to deploy Python3 code to Azure. Finally you then dove into the terminal to create a Python Virtual Environment and start preparing Pulumi to deploy resources.

Thanks for reading!