Introduction
Pulumi is an open-source Infrastructure as Code (IaC) tool that enables developers to define cloud resources using familiar programming languages like TypeScript, Python, Go, and C#. By leveraging the full power of these languages—including loops, conditionals, and functions—you can efficiently manage your infrastructure.
In this guide, we’ll walk you through:
- Registering for a Pulumi account
- Generating a Personal Access Token (PAT)
- Setting up the Pulumi and Azure CLI tools
- Creating your first Pulumi project using the TypeScript template for Azure
- Deploying your first Azure Resource Group and Storage Account
Table of Contents
Open Table of Contents
Prerequisites
- Node.js installed on your machine
- Azure CLI installed and configured
- An Azure account (you can create a free account here)
1: Pulumi Setup
1.1. Create a Pulumi Account
-
Visit the Pulumi Website
Navigate to the Pulumi website and click on the “Sign Up” button.
-
Choose a Sign-Up Method
Sign up using one of the following methods:
- GitHub
- GitLab
- Bitbucket
Follow the on-screen instructions to complete the registration.
-
Confirm Your Email
If you signed up using an email address, check your inbox for a confirmation email and verify your account.
1.2. Generate a Personal Access Token (PAT)
A Personal Access Token (PAT) is required to authenticate the Pulumi CLI with your Pulumi account.
-
Log In to the Pulumi Console
Visit the Pulumi Console and log in with your credentials.
-
Access the Tokens Page
- Click on your avatar or username in the top-right corner.
- Select “Access Tokens” from the dropdown menu.
-
Create a New Token
- Click on “Create Token”.
- Provide a description (e.g., “Pulumi CLI Token”).
- Click “Create” and copy the generated token for later use.
1.3. (Optional) Create a New Organization
If you’d like to manage your projects under a separate organization:
-
Navigate to Organizations
- In the Pulumi Console, click on your avatar or username.
- Select “Organizations”.
-
Create a New Organization
- Click on “Create Organization”.
- Follow the prompts to set up your organization.
2: Install CLI Tools
2.1. Install the Pulumi CLI
Follow the instructions here to install the Pulumi CLI for your operating system.
2.2. Install the Azure CLI
Install the Azure CLI by following the instructions here.
Here are the current versions
> pulumi version
v3.133.0
> az -v
azure-cli 2.64.0
3: Configure Pulumi for Azure
Before diving into coding, let’s configure Pulumi to work with your Azure account. Run the following commands to set up your Pulumi stack with the correct Azure subscription details:
# Set the default Pulumi organization (replace with your organization name)
pulumi org set-default YOUR_PULUMI_ORGANIZATION
# Configure Azure settings
pulumi config set azure-native:tenantId YOUR_AZURE_TENANT_ID
pulumi config set azure-native:subscriptionId YOUR_AZURE_SUBSCRIPTION_ID
pulumi config set azure-native:location YOUR_AZURE_LOCATION # e.g., EastUS
# Optional: If you're using a service principal for authentication
pulumi config set azure-native:clientId YOUR_AZURE_CLIENT_ID
pulumi config set azure-native:clientSecret YOUR_AZURE_CLIENT_SECRET --secret
Note: Replace placeholders with your actual Azure details. The --secret
flag ensures sensitive information is encrypted.
4: Create Your First Pulumi Project
4.1. Set Up a Git Repository
Create a new directory for your Pulumi project and initialize a Git repository:
mkdir day00_pulumi-azure-start
cd day00_pulumi-azure-start
git init
4.2. Initialize a New Pulumi Project
Run the following command to create a new Pulumi project using the Azure TypeScript template:
pulumi new azure-typescript
You’ll be prompted to provide:
- Login: Authenticate using the PAT token generated earlier.
- Project name: Accept the default or enter a custom name.
- Project description: (Optional)
- Stack name: e.g.,
dev
- Package manager: Choose your preferred package manager (e.g.,
npm
,yarn
,pnpm
). - Azure location: e.g.,
SoutheastAsia
After the project is created, if you are using your own account for development then ensure you’re logged into Azure:
az login
# Sample Code
Retrieving tenants and subscriptions for the selection...
[Tenant and subscription selection]
No Subscription name Subscription ID Tenant
----- ------------------- ------------------------------------ -----------
[1] * DrunkCoding 54dbd16b-81cd-yyyy-xxxx-xxxyyyzzz000 DrunkCoding
5: Understand the Project Structure
5.1. Project files
The template generates several files:
- Pulumi.yaml: Contains project metadata.
- Pulumi.dev.yaml: Contains stack-specific configuration.
- index.ts: The main program file where you’ll define your infrastructure.
- package.json: Node.js project metadata.
- tsconfig.json: TypeScript compiler configuration.
5.2: Review and Modify the Sample Code
Open index.ts
in your preferred code editor and review the sample code. It typically includes the creation of a Resource Group and a Storage Account.
Sample Code:
import * as pulumi from "@pulumi/pulumi";
import * as resources from "@pulumi/azure-native/resources";
import * as storage from "@pulumi/azure-native/storage";
// Create an Azure Resource Group
const resourceGroup = new resources.ResourceGroup("resourceGroup");
// Create an Azure Storage Account
const storageAccount = new storage.StorageAccount("storageaccount", {
resourceGroupName: resourceGroup.name,
sku: {
name: storage.SkuName.Standard_LRS,
},
kind: storage.Kind.StorageV2,
});
// Export the primary key of the Storage Account
const storageAccountKeys = storage.listStorageAccountKeysOutput({
resourceGroupName: resourceGroup.name,
accountName: storageAccount.name,
});
export const primaryStorageKey = storageAccountKeys.keys[0].value;
6: Preview and Deploy Your Stack
6.1. Preview the Changes
Before deploying, preview the changes to ensure everything is set up correctly:
pulumi up
Sample Output:
> pulumi up
Previewing update (dev)
View in Browser (Ctrl+O): https://app.pulumi.com/drunkcoding/day00_pulumi-azure-start/dev/previews/xxxxxxxx-1f60-4ed9-bb35-xxxxxxxxxxxx
Type Name Plan
+ pulumi:pulumi:Stack day00_pulumi-azure-start-dev create
+ ├─ azure-native:resources:ResourceGroup resourceGroup create
+ └─ azure-native:storage:StorageAccount sa create
Outputs:
primaryStorageKey: output<string>
Resources:
+ 3 to create
Do you want to perform this update? [Use arrows to move, type to filter]
yes
> no
details
You should see a plan indicating that new resources will be created.
6.2. Deploy the Stack
Deploy your resources to Azure:
pulumi up -y
Sample Output:
Updating (dev)
View Live: https://app.pulumi.com/YOUR_ORGANIZATION/day00_pulumi-azure-start/dev/updates/1
Type Name Status
+ pulumi:pulumi:Stack day00_pulumi-azure-start-dev created
+ ├─ azure-native:resources:ResourceGroup resourceGroup created
+ └─ azure-native:storage:StorageAccount storageaccount created
Outputs:
primaryStorageKey: "<secure>"
Resources:
+ 3 created
Duration: 35s
6.3: Verify the Deployment
After deployment, you can verify the resources in the Azure Portal:
- Log in to the Azure Portal.
- Navigate to Resource Groups and locate your newly created resource group.
- Verify that the Storage Account is present within the resource group.
7: Clean Up Resources
To avoid incurring unnecessary costs, destroy the resources when they’re no longer needed:
pulumi destroy
- Review the resources scheduled for deletion.
- Type
yes
when prompted to confirm.
Sample Output:
Destroying (dev)
View Live: https://app.pulumi.com/YOUR_ORGANIZATION/day00_pulumi-azure-start/dev/updates/2
Type Name Status
- pulumi:pulumi:Stack day00_pulumi-azure-start-dev deleted
- ├─ azure-native:storage:StorageAccount storageaccount deleted
- └─ azure-native:resources:ResourceGroup resourceGroup deleted
Outputs:
- primaryStorageKey: "<secure>"
Resources:
- 3 deleted
Duration: 25s
The resources in the stack have been deleted, but the history and configuration are still maintained.
If you want to remove the stack completely, run `pulumi stack rm dev`.
Conclusion
Congratulations! You’ve successfully:
- Registered for a Pulumi account
- Generated a Personal Access Token
- Set up the Pulumi and Azure CLI tools
- Created your first Pulumi project using TypeScript for Azure
- Deployed an Azure Resource Group and Storage Account
Pulumi simplifies cloud resource management by allowing you to use familiar programming languages and tools. You can now explore adding more complex resources and configurations to your project.
Next Steps
-
Day 1: Create a Virtual Network Hub on Azure
Continue your Pulumi journey by setting up a virtual network hub, adding virtual machines, or deploying Azure Functions.
Thank You
Thank you for taking the time to read this guide! I hope it has been helpful, feel free to explore further, and happy coding! 🌟✨
Steven GitHub