Skip to main content

Creating Resources on Azure

10 min

In Pulumi, resources represent the fundamental units that make up your infrastructure, such as virtual machines, networks, storage, and databases. A resource is used to define and manage an infrastructure object in your Pulumi configuration.

In this tutorial, you will create a simple static website hosted on an Azure Blob Storage account. You will then refer to documentation in the Pulumi Registry to configure the storage account as a website.

What you'll learn
  • How to create a new resource
  • How to reference resource definitions in the Pulumi documentation
Prerequisites

Create a new project#

To start, login to the Pulumi CLI and ensure it is configured to use your Azure account. Next, create a new project and stack.

Terminal window
# Example using Python
mkdir pulumi-tutorial-azure
cd pulumi-tutorial-azure
pulumi new azure-python

Then use the following code snippet to scaffold your project with the required imports and overall program structure that you will fill in as you go along:

import * as pulumi from "@pulumi/pulumi";
import * as resources from "@pulumi/azure-native/resources";
import * as storage from "@pulumi/azure-native/storage";
const path = "./wwwroot";
const indexDocument = "index.html";
const errorDocument = "error.html";
// Steps:
// [1] Create a resource group.
// [2] Create a blob storage account.
// [3] Configure the storage account as a website.

Create a resource group#

The first resource you will create will be an Azure Resource Group. Azure Resource Groups provide a logical container to organize and manage resources in your Azure account. In Azure, resources like virtual machines, storage accounts, and databases are grouped together within a resource group.

The Pulumi Registry provides the documentation for all of the Pulumi providers and their associated resources. Open the azure-native.resources.ResourceGroup documentation page to view a description of this resource, example usage, the resource definition, and supported properties. You will now define your resource group resource as shown below:

import * as pulumi from "@pulumi/pulumi";
import * as resources from "@pulumi/azure-native/resources";
import * as storage from "@pulumi/azure-native/storage";
const path = "./wwwroot";
const indexDocument = "index.html";
const errorDocument = "error.html";
// [1] Create a resource group.
const resourceGroup = new resources.ResourceGroup("website-resource-group", {
location: "eastus",
});

All resources have a required name argument. Each resource has both a logical name and a physical name. The logical name is how the resource is known inside Pulumi. This is the value provided to the required name argument. The physical name is the name used for the resource in the cloud provider that a Pulumi program is deploying to. It is a combination of the logical name plus a random suffix which helps to prevent resource naming collisions.

In the above example, the logical name for our ResourceGroup resource is “website-resource-group”, and the physical name might typically look something like “website-resource-group-d7c2fa0”.

Create a storage account#

The next step will be to create an Azure Blob Storage account that will be used to host the website. Once again, you can refer to the Pulumi registry, specifically the azure-native.storage.StorageAccount resource page, to define your storage account as shown below:

import * as pulumi from "@pulumi/pulumi";
import * as resources from "@pulumi/azure-native/resources";
import * as storage from "@pulumi/azure-native/storage";
const path = "./wwwroot";
const indexDocument = "index.html";
const errorDocument = "error.html";
// [1] Create a resource group.
const resourceGroup = new resources.ResourceGroup("website-resource-group", {
location: "eastus",
});
// [2] Create a blob storage account.
const storageAccount = new storage.StorageAccount("websiteblob", {
resourceGroupName: resourceGroup.name,
kind: "StorageV2",
sku: {
name: "Standard_LRS",
},
});

In addition to names, resources have properties and options. Properties are used to specify what type of resource to create. Properties are often resource-specific, and they can be required or optional depending on the specifications of the provider.

The properties inside your StorageAccount resource are:

PropertyDescription
resource group nametells the Azure Native what resource group to associate this resource with
kindtells the provider the type of storage account to create, e.g. StorageV2
skutells the provider the SKU to use when creating the resource, e.g. Standard_LRS

Options let you control certain aspects of a resource (such as showing explicit dependencies or importing existing infrastructure). We do not have any options defined for this resource, but you can learn more about options in the Pulumi documentation.

Deploy your storage account#

Now run the pulumi up command to preview and deploy the resources you’ve just defined in your project.

Terminal window
pulumi up -y
Previewing update (static-website)
Type Name Plan
+ pulumi:pulumi:Stack azure-static-website create
+ ├─ azure-native:resources:ResourceGroup website-resource-group create
+ └─ azure-native:storage:StorageAccount websiteblob create
Resources:
+ 3 to create
Updating (static-website)
Type Name Status
+ pulumi:pulumi:Stack azure-static-website created (39s)
+ ├─ azure-native:resources:ResourceGroup website-resource-group created (4s)
+ └─ azure-native:storage:StorageAccount websiteblob created (30s)
Resources:
+ 3 created
Duration: 41s

Configure the static website#

In this section, you will use the Pulumi documentation to configure the storage account as a static website on your own. To prepare for the configuration of the storage account, you will need to create a folder labeled www in your project. Inside this project, you will need to create an index.html file and an error.html file with the content as shown below.

Contents of the index.html file:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world! 👋</h1>
<p>Deployed with 💜 by <a href="https://pulumi.com/">Pulumi</a>.</p>
</body>
</html>

Content of the error.html file:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Page not found</title>
</head>
<body>
Oops! That page wasn't found. Try our <a href="/">home page</a> instead.
</body>
</html>

An updated version of the project code has been provided below as a starting point:

import * as pulumi from "@pulumi/pulumi";
import * as resources from "@pulumi/azure-native/resources";
import * as storage from "@pulumi/azure-native/storage";
const path = "./wwwroot";
const indexDocument = "index.html";
const errorDocument = "error.html";
// [1] Create a resource group.
const resourceGroup = new resources.ResourceGroup("website-resource-group", {
location: "eastus",
});
// [2] Create a blob storage account.
const storageAccount = new storage.StorageAccount("websiteblob", {
resourceGroupName: resourceGroup.name,
kind: "StorageV2",
sku: {
name: "Standard_LRS",
},
});
// [3] Configure the storage account as a website.
// TO-DO
// Upload the website files
[indexDocument, errorDocument].map(
name =>
new storage.Blob(name, {
resourceGroupName: resourceGroup.name,
accountName: storageAccount.name,
containerName: website.containerName,
source: new pulumi.asset.FileAsset(`./${path}/${name}`),
contentType: "text/html",
}),
);
// Export the URL of the website.
export const staticEndpoint = storageAccount.primaryEndpoints.web;

Use the following steps as a guide for adding the storage :

  • Navigate to the Azure Native Registry
  • Search for the StorageAccountStaticWebsite resource
  • Define the StorageAccountStaticWebsite resource in your project code
  • Preview and deploy your updated project code

The website URL has been provided for you as an output, and you can use this to access your static website once the deployment has completed. You should be greeted with a “Hello, world!” homepage message that indicates your static website has been successfully created.

View complete solution#

You can view the complete project code below:

import * as pulumi from "@pulumi/pulumi";
import * as resources from "@pulumi/azure-native/resources";
import * as storage from "@pulumi/azure-native/storage";
const path = "./wwwroot";
const indexDocument = "index.html";
const errorDocument = "error.html";
// [1] Create a resource group.
const resourceGroup = new resources.ResourceGroup("website-resource-group", {
location: "eastus",
});
// [2] Create a blob storage account.
const storageAccount = new storage.StorageAccount("websiteblob", {
resourceGroupName: resourceGroup.name,
kind: "StorageV2",
sku: {
name: "Standard_LRS",
},
});
// [3] Configure the storage account as a website.
const website = new storage.StorageAccountStaticWebsite("website", {
accountName: storageAccount.name,
resourceGroupName: resourceGroup.name,
indexDocument: indexDocument,
error404Document: errorDocument,
});
// Upload the website files
[indexDocument, errorDocument].map(
name =>
new storage.Blob(name, {
resourceGroupName: resourceGroup.name,
accountName: storageAccount.name,
containerName: website.containerName,
source: new pulumi.asset.FileAsset(`./${path}/${name}`),
contentType: "text/html",
}),
);
// Export the URL of the website.
export const staticEndpoint = storageAccount.primaryEndpoints.web;

Clean up#

Before moving on, tear down the resources that are part of your stack to avoid incurring any charges.

  1. Run pulumi destroy to tear down all resources. You’ll be prompted to make sure you really want to delete these resources. A destroy operation may take some time, since Pulumi waits for the resources to finish shutting down before it considers the destroy operation to be complete.
  2. To delete the stack itself, run pulumi stack rm. Note that this command deletes all deployment history from the Pulumi Service.

Next steps#

In this tutorial, you made a resource group and a storage account, and you configured your storage account as a static website by referencing the Pulumi Registry. You also reviewed resource properties and example usage of various resources.

To learn more about creating resources in Pulumi, take a look at the following resources:

Related

The infrastructure as code platform for any cloud.