Now you have set up your environment by installing Pulumi, installing your preferred language runtime, and configuring your Azure credentials.
Pulumi supports several programming languages. The first step is to ask the CLI to bootstrap a basic TypeScript project.
Make a new folder called pulumi-aks-workshop anywhere on your local disk. By default, this folder gives a name to your Pulumi project.
Initialize the project
The pulumi new command creates a new Pulumi project with some basic scaffolding based on the cloud and language specified.
mkdir pulumi-ask-workshop && cd pulumi-ask-workshop && pulumi new azure-typescript
First, you will be asked for a project name and project description. Hit ENTER to accept the default values or specify new values.
This command will walk you through creating a new Pulumi project.
Enter a value or leave blank to accept the (default), and press <ENTER>.
Press ^C at any time to quit.
Project name (pulumi-ask-workshop):
Project description (A minimal Azure Native TypeScript Pulumi program):
Created project 'pulumi-ask-workshop'
Next, you will be asked for a stack name. Hit ENTER to accept the default value of dev.
Please enter your desired stack name.
To create a stack in an organization, use the format <org-name>/<stack-name> (e.g. `acmecorp/dev`).
Stack name (dev): dev
Created stack 'dev'
The package manager to use for installing dependencies [Use arrows to move, type to filter]
> npm
yarn
pnpm [not found]
You can choose the package manager likes npm or yarn. The default is npm. Hit ENTER to accept.
For Azure projects, you will be prompted for the Azure location. You can accept the default value of WestUS or choose another location. The Azure location will be stored in Pulumi.dev.yaml file. I will choose southeastasia for now.
You also can then change the region for your stack by using the pulumi config set command as shown below:
After some dependency installations, the project and stack will be ready.
Install Dependencies
Ensure you have the required dependencies installed:
Introduction to Pulumi Configuration
Pulumi uses configurations to manage environment-specific settings like credentials, resource options, or feature flags. This makes your infrastructure code adaptable and reusable across different environments (such as development, staging, and production) by applying different configurations for each.
Key Concepts in Pulumi Configuration
Stack: A Pulumi stack is an isolated deployment environment for your infrastructure. You can have different stacks for different environments (e.g., dev, staging, prod).
Configuration: Each stack can have its own configuration settings, such as region, instance types, or API keys. These configurations are stored securely and can be referenced within your Pulumi code.
Secrets: Pulumi can securely manage sensitive information, like passwords or API keys, using encrypted secrets.
Setting Configuration Values
Pulumi allows you to define configuration values through the command line, which are stored in the stack’s configuration file (e.g., Pulumi.dev.yaml, Pulumi.prod.yaml).
You can set configuration values using the Pulumi CLI:
For example, to set the region for an Azure deployment, you can run:
This will save the location to the stack's configuration file.
You can view the current configuration for a stack:
Accessing Configuration in Code
Once a configuration value is set, you can access it in your Pulumi code using the pulumi.Config object.
Example in TypeScript:
In this case, config.require("azure:location") fetches the value set by pulumi config set azure:location westus2 and uses it to define the location of the resource group.
config.require("<key>"): Retrieves a configuration value and throws an error if the key is not found.
config.get("<key>"): Retrieves a configuration value and returns undefined if the key is not found.
import * as pulumi from "@pulumi/pulumi";
// Create a configuration object
const config = new pulumi.Config();
// Access a simple configuration value
const location = config.require("azure:location");
// Use the configuration value in your resource definitions
const resourceGroup = new azure.core.ResourceGroup("myResourceGroup", {
location: location,
});