Getting started with Pulumi
Managing Infrastructure as code (IaC) is dominated by well known Terraform.
However, you are confined to use the more Ops driven HCL.
Where Pulumi differs is that it lets you use languages most people are already comfortable of using - Python, Go, JavaScript, TypeScript, and C#.
Since Pulumi supports these languages it makes using loops, functions and classes easier to integrate, and enforce best practices.
In essence fusing the roles which Devs and Ops play.
Projects
A Pulumi project is any folder which contains a Pulumi.yaml
file.
This file consists of the following properties.
- name (name of the project)
- runtime (associated language)
- description (brief description of what purpose the project serves)
e.g.
name: gcp-instance-nginx
runtime: python
description: Deploy a Nginx Server in a GCP instance
Stacks
A stack is an isolated, independently configurable instance of a Pulumi program.Stacks are commonly used to denote different phases of development (such as development, staging and production) or feature branches (such as feature-x-dev, jane-feature-x-dev).
Each stack has a configuration and update history associated with it, stored in the workspace, in addition to a full checkpoint of the last known good update.
With that being said I shall note down the steps to get up and running with Pulumi.
Installation steps for Linux
Pre requisite - Installation of runtime languages (Python, Go, JavaScript, TypeScript, or C#)
curl -fsSL https://get.pulumi.com | sh
Close, then open a new terminal to verify pulumi installation.
pulumi version
v2.0.0
Access token
- Head over to Pulumi to create a new account
- Create a new access token (this is under your profile avatar > settings)
Make a note of the access token as it will not be visible again.
Basic steps
Using a stack
Out of the box Pulumi provides the following stack templates.
Available Templates:
alicloud-csharp A minimal AliCloud C# Pulumi program
alicloud-fsharp A minimal AliCloud F# Pulumi program
alicloud-go A minimal AliCloud Go Pulumi program
alicloud-javascript A minimal AliCloud JavaScript Pulumi program
alicloud-python A minimal AliCloud Python Pulumi program
alicloud-typescript A minimal AliCloud TypeScript Pulumi program
alicloud-visualbasic A minimal AliCloud VB.NET Pulumi program
aws-csharp A minimal AWS C# Pulumi program
aws-fsharp A minimal AWS F# Pulumi program
aws-go A minimal AWS Go Pulumi program
aws-javascript A minimal AWS JavaScript Pulumi program
aws-python A minimal AWS Python Pulumi program
aws-typescript A minimal AWS TypeScript Pulumi program
aws-visualbasic A minimal AWS VB.NET Pulumi program
azure-csharp A minimal Azure C# Pulumi program
azure-fsharp A minimal Azure F# Pulumi program
azure-go A minimal Azure Go Pulumi program
azure-javascript A minimal Azure JavaScript Pulumi program
azure-python A minimal Azure Python Pulumi program
azure-typescript A minimal Azure TypeScript Pulumi program
azure-visualbasic A minimal Azure VB.NET Pulumi program
csharp A minimal C# Pulumi program
digitalocean-go A minimal DigitalOcean Go Pulumi program
digitalocean-javascript A minimal DigitalOcean JavaScript Pulumi program
digitalocean-python A minimal DigitalOcean Python Pulumi program
digitalocean-typescript A minimal DigitalOcean TypeScript Pulumi program
fsharp A minimal F# Pulumi program
gcp-csharp A minimal GCP C# Pulumi program
gcp-fsharp A minimal GCP F# Pulumi program
gcp-go A minimal Google Cloud Go Pulumi program
gcp-javascript A minimal Google Cloud JavaScript Pulumi program
gcp-python A minimal Google Cloud Python Pulumi program
gcp-typescript A minimal Google Cloud TypeScript Pulumi program
gcp-visualbasic A minimal GCP VB.NET Pulumi program
go A minimal Go Pulumi program
hello-aws-javascript A simple AWS serverless JavaScript Pulumi program
javascript A minimal JavaScript Pulumi program
kubernetes-csharp A minimal Kubernetes C# Pulumi program
kubernetes-fsharp A minimal Kubernetes F# Pulumi program
kubernetes-go A minimal Kubernetes Go Pulumi program
kubernetes-javascript A minimal Kubernetes JavaScript Pulumi program
kubernetes-python A minimal Kubernetes Python Pulumi program
kubernetes-typescript A minimal Kubernetes TypeScript Pulumi program
linode-go A minimal Linode Go Pulumi program
linode-javascript A minimal Linode JavaScript Pulumi program
linode-python A minimal Linode Python Pulumi program
linode-typescript A minimal Linode TypeScript Pulumi program
openstack-go A minimal OpenStack Go Pulumi program
openstack-javascript A minimal OpenStack JavaScript Pulumi program
openstack-python A minimal OpenStack Python Pulumi program
openstack-typescript A minimal OpenStack TypeScript Pulumi program
packet-go A minimal Packet.net Go Pulumi program
packet-javascript A minimal Packet.net JavaScript Pulumi program
packet-python A minimal Packet.net Python Pulumi program
packet-typescript A minimal Packet.net TypeScript Pulumi program
python A minimal Python Pulumi program
typescript A minimal TypeScript Pulumi program
visualbasic A minimal VB.NET Pulumi program
Creating a new project by initializing an empty directory
mkdir pulumi-dev; cd pulumi-dev
# where python is the name of the project template taken from the long list above
pulumi new python
Since this is the first time running Pulumi you will prompted to enter an access token.
Paste in the access token it’s the one you created earlier.
This will prompt for further Project information.
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-dev)
project description: (A minimal Python Pulumi program)
Created project 'pulumi-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)
Created stack 'dev'
Your new project is ready to go!
To perform an initial deployment, run the following commands:
1. python3 -m venv venv
2. source venv/bin/activate
3. pip3 install -r requirements.txt
Then, run 'pulumi up'
Head over to the console to see the new stack called ‘dev’.
Lets take a look at the files generated.
requirements.txt
pulumi>=2.0.0,<3.0.0
Pulumi.yaml
name: pulumi-dev
runtime: python
description: A minimal Python Pulumi program
main.py
import pulumi
.gitignore
*.pyc
venv/
I’ve selected the runtime as Python here, so Pulumi encourages you to use a virtual environment for isolating the install of the additional Python modules away from the main OS Python modules installation area.
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Next, run pulumi up to create/update resources in a stack
Check the stack
pulumi stack
Current stack is dev:
Owner: ###
Last updated: 4 minutes ago (2020-04-22 18:30:51.201134571 +0100 BST)
Pulumi version: v2.0.0
Current stack resources (1):
TYPE NAME
pulumi:pulumi:Stack pulumi-dev-dev
More information at: https://app.pulumi.com/###/pulumi-dev/dev
Running updates
Next, I shall make a slight change to the main.py file and add in the following lines.
These 2 lines applies 2 properties to the stack
a=hello
b=world
pulumi.export("a", "hello")
pulumi.export("b", "world")
To deploy the changes, run
pulumi up
Next, head back to the Pulumi console. You’ll find that the Outputs of the stack updated.
Viewing stack outputs
Make sure you are in the Pulumi project directory, i.e. the one with the Pulumi.yaml file before running the command.
pulumi stack output
Current stack outputs (2):
OUTPUT VALUE
a hello
b world
pulumi stack output a
hello
pulumi stack output b
world
Adding tags to a stack
# view all tags
pulumi stack ls
NAME VALUE
pulumi:description A minimal Python Pulumi program
pulumi:project pulumi-dev
pulumi:runtime python
# add a tag
pulumi stack tag set environment dev
Removing a stack
pulumi stack rm
This will permanently remove the 'dev' stack!
Please confirm that this is what you'd like to do by typing ("dev"): dev
Stack 'dev' has been removed!