Provision Virtual Machine in Azure using Pulumi
Usage of Infrastructure as Code (IaC) has been adopted lately using various techniques and tools like Azure ARM template, AWS Cloud Formation, Terraform, Pulumi, Shell scripts etc. In this episode, let’s look at how to provision a Linux Virtual Machine in Azure platform using Pulumi.
Technologies used for the demo:
- Windows 10
- Azure Subscription
- Pulumi for Windows
- Azure CLI
- Pulumi CLI
- Visual Studio 2019
- C#
Step 1:
Assuming you have already got windows 10 and Azure subscription, lets download and install Pulumi on Windows.
- Download Pulumi at https://get.pulumi.com/releases/sdk/pulumi-v3.22.1-windows-x64.zip
- Unzip the file and extract the contents to a folder such as C:\pulumi
- Add C:\pulumi\bin to your path via System Properties -> Advanced -> Environment Variables -> User Variables -> Path -> Edit.
- Run “pulumi version” on the command prompt to verify the installation and version.
Step 2:
Signup for Pulumi Account
- Go to https://app.pulumi.com/. You can sign up using your existing accounts in Github, Gitlab, Altassian or via email account.
- Once signed up, login to the https://app.pulumi.com/ and you should see something like the below
- Fill the values in the Get Started: Set Up Your Environment section and select Next step
- Now you can see the steps to create the Project
Step 3:
Let’s now create a Pulumi project
- Create a new directory in C drive with the directory name ‘azure-csharp” or any name your prefer
- Go to the above directory and open a command prompt within the directory
- Type the below command to initialize the project
“pulumi new azure-csharp”
- You will be prompted to Login to Pulumi or to enter your access token
- Click Enter to open the browser and login with your Pulumi account details.
- Now you should see something similar below in your command window
- Enter or Modify project name, project description, stack name, location etc.
- Once completed, you should see something similar below
Step 4:
Assuming Azure CLI and Visual Studio 2019 is installed, let us open the project in Visual Studio
- Let us first check the project directory
- Open the project in Visual Studio 2019
- Open the MyStack.cs file.
- Replace the default code with the below code:
- You can also download or fork the code from the below GitHub repository:
https://github.com/azeemqf/pulumi-azure
- Now build the project and make sure it is succeeded
Step 5:
Now you are all set to run the Pulumi up command to execute the project
- Open the PowerShell terminal on Visual Studio
- Login to Azure account and set to your preferred subscription if you have multiple subscriptions
Type “az login” at the PS prompt
Type “az account set –subscription [Subscription Name]” at the PS prompt. Substitute your actual azure subscription name in the above highlighted text
Type “pulumi Up” at the PS prompt.
- Move the pointer to “yes” and press enter
- Pulumi will now provision the infrastructure as specified in our code. Once the process is finished, you should see something like the below
- Login to Azure Portal and double check whether the resources have been created.
Step 6:
Cleaning up the resources
- If you want to destroy the resources, just type the below on the PS prompt
- pulumi destroy
- Once the process is complete, you should see something like the below confirming that the resources have been deleted
Notes:
- The ssh key which has been used in the code need to be replaced by your own private ssh key