Introduction

Pulumi is an infrastructure as code tool that helps you build and manage environments such as AWS, Azure and GCP with the programming languages you already know.

In this post, I will show you how to get up and running with Pulumi.

Software

The following software was used in this post.

  • Pulumi - v3.13.2
  • NodeJS - v16.0.0
  • Ubuntu - 20.04

Pre-Requisites

Language Runtime

Pulumi allows you to build infrastructure with the programming languages you know. Unlike other tools that have a DSL, Pulumi instructions are written in a choice of supported programming languages. Which, at the time of writing are as follows:

  • Node.js
  • Python
  • Go
  • .Net

Ensure that you have a supported runtime installed. For this post, I will be using Node.js with Typescript. Instructions to install Node.js on Ubuntu 2004 can be found here.

Environment Credentials

Pulumi requires access to credentials with sufficient permissions to make changes to your infrastructure. I will be using AWS for this post. For instructions on installing the AWS CLI and setting up your credentials see this post.

Pulumi Account

Create Account

Head here to create a Pulumi account. The account is free to use for indiviuals and is used as a storage backend for your Pulumi projects state data.

Access Token

An access token is required to login and use your Pulumi account. Create a token here.

Note

Save the token in your password manager or similar. Once you navigate away from the page, you will no longer be able to view the token.

Install Pulumi

The Pulumi application is installed in your $PATH via an installer script.

cmd

curl -fsSL https://get.pulumi.com | sh
  
# Output
=== Installing Pulumi v3.13.2 ===
+ Downloading https://get.pulumi.com/releases/sdk/pulumi-v3.13.2-linux-x64.tar.gz...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 61.0M  100 61.0M    0     0  5308k      0  0:00:11  0:00:11 --:--:-- 5597k
+ Extracting to /home/bradmin/.pulumi/bin
+ Adding $HOME/.pulumi/bin to $PATH in /home/bradmin/.zshrc

=== Pulumi is now installed! 🍹 ===
+ Please restart your shell or add /home/bradmin/.pulumi/bin to your $PATH
+ Get started with Pulumi: https://www.pulumi.com/docs/quickstart
    

Once Pulumi is installed, restart your shell environment and confirm you have access to the pulumi command.

cmd

# In a new shell
pulumi version

# Output
v3.13.2
    

Pulumi Project

Account Login

Create a new directory for your project and use the pulumi login command to login to your Pulumi account. You will need to enter the access token created in the previous step.

cmd

mkdir testing123 && cd testing123
pulumi login

# Output
Manage your Pulumi stacks by logging in.
Run `pulumi login --help` for alternative login options.
Enter your access token from https://app.pulumi.com/account/tokens
    or hit  to log in using your browser                   :
    

Create Project

Initialise a new AWS project that uses the Typescript language with the pulumi new aws-typescript command and follow the prompts.

cmd

pulumi new aws-typescript

# Output
This command will walk you through creating a new Pulumi project.

Enter a value or leave blank to accept the (default), and press .
Press ^C at any time to quit.

project name: (testing123)
project description: (A minimal AWS TypeScript Pulumi program)
Created project 'testing123'

Please enter your desired stack name.
To create a stack in an organization, use the format / (e.g. `acmecorp/dev`).
stack name: (dev)
Created stack 'dev'

aws:region: The AWS region to deploy into: (us-east-1)
Saved config

Installing dependencies...


added 125 packages, and audited 126 packages in 26s

31 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
Finished installing dependencies

Your new project is ready to go!

To perform an initial deployment, run 'pulumi up'
    

This will create a Stack which is an isolated, independently configurable instance of a Pulumi program. This stack belongs to the dev environment.

The following files and folders were created which gets a minimal config setup with the required dependencies.

  • index.ts
  • node_modules
  • package.json
  • package-lock.json
  • Pulumi.dev.yaml
  • Pulumi.yaml
  • tsconfig.json

Looking at the index.ts file you can see that it is setup to create an S3 bucket named my-bucket.

file

// index.ts
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as awsx from "@pulumi/awsx";

// Create an AWS resource (S3 Bucket)
const bucket = new aws.s3.Bucket("my-bucket");

// Export the name of the bucket
export const bucketName = bucket.id;
    

Deploy Project

With this minimal config we can deploy the project with the pulumi up command and create an S3 bucket.

cmd

pulumi up
  
# Output
Previewing update (dev)

View Live: https://app.pulumi.com/bwks/testing123/dev/previews/8c12d94d-1c88-43c6-b989-a0ebd2b4c9db

     Type                 Name            Plan
 +   pulumi:pulumi:Stack  testing123-dev  create
 +   └─ aws:s3:Bucket     my-bucket       create

Resources:
    + 2 to create

Do you want to perform this update? yes
Updating (dev)

View Live: https://app.pulumi.com/bwks/testing123/dev/updates/1

     Type                 Name            Status
 +   pulumi:pulumi:Stack  testing123-dev  created
 +   └─ aws:s3:Bucket     my-bucket       created

Outputs:
    bucketName: "my-bucket-c9ab4c7"

Resources:
    + 2 created

Duration: 20s
    

Confirm the bucket was created with the aws s3 ls command.

cmd

aws s3 ls

# Output
2021-10-02 20:46:30 my-bucket-c9ab4c7
    

Destroy Project

To remove all the resources in this project that are managed by pulumi, use the pulumi destroy command.

cmd

pulumi destroy

# Output
Previewing destroy (dev)

View Live: https://app.pulumi.com/bwks/testing123/dev/previews/1ab42c39-a78f-4d6a-a68d-6ed6d66e1d5e

     Type                 Name            Plan
 -   pulumi:pulumi:Stack  testing123-dev  delete
 -   └─ aws:s3:Bucket     my-bucket       delete

Outputs:
  - bucketName: "my-bucket-c9ab4c7"

Resources:
    - 2 to delete

Do you want to perform this destroy? yes
Destroying (dev)

View Live: https://app.pulumi.com/bwks/testing123/dev/updates/2

     Type                 Name            Status
 -   pulumi:pulumi:Stack  testing123-dev  deleted
 -   └─ aws:s3:Bucket     my-bucket       deleted

Outputs:
  - bucketName: "my-bucket-c9ab4c7"

Resources:
    - 2 deleted

Duration: 6s

The resources in the stack have been deleted, but the history and configuration associated with the stack are still maintained.

If you want to remove the stack completely, run 'pulumi stack rm dev'.
    

This deletes the AWS resouces, but it does not remove the Stack from being managed by pulumi. To delete the Stack use the pulumi stack rm command.

cmd

pulumi stack rm

# Output
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!
    

You can confirm that the stack is removed with the pulumi stack ls command.

cmd

pulumi stack ls

# Output
NAME  LAST UPDATE  RESOURCE COUNT  URL
    

Summary

In this post, we installed and configured Pulumi to manage our AWS environemnt using the Typescript provider. We created a stack which was used to deploy an S3 bucket to our AWS account. Finally, we destroyed the AWS resources and removed the stack from Pulumi management.

Tags

pulumi devops