Azure Python Starter
A minimal Azure Native Python Pulumi program
Before you begin#
For this walkthrough, you'll need:
- The Pulumi CLI
- The Azure CLI (optional, but recommended and used in this guide)
- Permission to manage Azure Storage
Create a new project#
This starter creates an Azure resource group with a storage account in it and exports the storage account’s name.
mkdir my-project; cd my-projectpulumi new azure-pythonmkdir my-project; cd my-projectpulumi new azure-pythonThe pulumi new command walks you through creating and configuring the new project and stack. A stack is an instance of a project, and you can have many of them (dev, staging, prod), each with its own configuration settings. Depending on the language, you may also be asked for your preferred package manager or toolchain.
The template also asks for the Azure location you’d like to deploy into:
| Key | Description | Default |
|---|---|---|
| azure-native:location | The Azure location to use | WestUS2 |
pulumi config set
:
pulumi config set azure-native:location <value>pulumi config set azure-native:location "<value>"Review the code#
When setup completes, you’ll see a few files:
Pulumi.yamlcontains the project name and the language it’s written in.Pulumi.dev.yamlcontains thedevstack’s settings, including the values you just entered.__main__.pycontains the program code that declares your cloud resources.
Take a moment to open them up and have a look. Here’s what’s in __main__.py:
"""An Azure RM Python Pulumi program"""
import pulumifrom pulumi_azure_native import storagefrom pulumi_azure_native import resources
# Create an Azure Resource Groupresource_group = resources.ResourceGroup("resource_group")
# Create an Azure Storage Accountaccount = storage.StorageAccount( "sa", resource_group_name=resource_group.name, sku={ "name": storage.SkuName.STANDARD_LRS, }, kind=storage.Kind.STORAGE_V2,)
# Export the storage account namepulumi.export("storage_account_name", account.name)Deploy the project#
Deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy.
Pulumi adds a random suffix to most resource names, so the storage account sa gets a name like sa8deefa78. This is autonaming. It lets you deploy the same program to more than one stack, and lets Pulumi create a replacement before it deletes the old resource, without names colliding.
pulumi uppulumi upWhen it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist. This program exports the storage account’s name. Read it any time with pulumi stack output <name>, or pass it to another command. This one prints the account’s blob endpoint:
az storage account show --name $(pulumi stack output storage_account_name) --query primaryEndpoints.blob --output tsvaz storage account show --name $(pulumi stack output storage_account_name) --query primaryEndpoints.blob --output tsvNeed help?
If pulumi up asks you to run az login, or says your sign-in has expired, sign in again:
az loginaz loginStill stuck? Let us know.
Customize the project#
Put a file in the storage account. In __main__.py, add this below the storage account:
container = storage.BlobContainer( "container", resource_group_name=resource_group.name, account_name=account.name,)
storage.Blob( "hello", resource_group_name=resource_group.name, account_name=account.name, container_name=container.name, blob_name="hello.txt", source=pulumi.StringAsset("Hello, Pulumi!"),)
pulumi.export("container_name", container.name)Deploy again. The preview shows a container and a file to create:
pulumi uppulumi upThen read the file back from the storage account. It prints Hello, Pulumi!:
az storage blob download --account-name $(pulumi stack output storage_account_name) --container-name $(pulumi stack output container_name) --name hello.txt --auth-mode key --only-show-errors --no-progressaz storage blob download --account-name $(pulumi stack output storage_account_name) --container-name $(pulumi stack output container_name) --name hello.txt --auth-mode key --only-show-errors --no-progressTo see what else you can add, browse the Azure Native provider in the Pulumi Registry.
Cleaning up#
You can destroy the stack and all of its infrastructure with pulumi destroy, then remove the stack with pulumi stack rm:
pulumi destroypulumi stack rmpulumi destroypulumi stack rm