Pulumi Python projects

This topic describes how to set up Pulumi projects with Python.

Prerequisites

The following are the prerequisites for managing Pulumi projects with Python using the Connect Pulumi provider.

  • Python version 3.9
  • pip (Python package installer)

Log in

Before creating a project, log in to a Pulumi back end to store your project's state. For a self-managed local back end, run pulumi login --local

This stores your project state locally in your file system. It is the simplest option for getting started. For Pulumi Cloud or other back end options, see the Pulumi documentation for pulumi login.

Create a new directory

Create a new directory. Run:

mkdir my-pulumi-python-project

cd my-pulumi-python-project

Caution: Create your Pulumi project directory outside of the Connect installation directory. Keeping your project separate, helps avoid accidental overwrites or permission issues during upgrades or maintenance.

Initialize a new project

Initialize the Pulumi Python project. Run: pulumi new python

Follow the prompts to set your project name, description, and stack.

Create a virtual environment

We recommend using a Python virtual environment to isolate your project dependencies.

To set up the virtual environment:

  1. To create the virtual environment, run: python -m venv venv

  2. To activate the virtual environment, run: source venv/bin/activate. Once activated, your command prompt will show the virtual environment name, for example, (venv).

  3. Update your Pulumi.yaml file to specify the virtual environment configuration:

Copy code
name: pythonConnect 
description: A minimal Python Pulumi program 
runtime:
    name: python   
    options:     
        toolchain: pip     
        virtualenv: venv 
config:   
    pulumi:tags:     
        value:       
            pulumi:template: python 

Install required packages

Install any additional Python packages required by your project. Run: pip install pulumi

Generate the Python SDK

After each upgrade or installation of a new version of the Connect provider, you must replace the old Python SDK with the new one generated from the provider binary.

Note: Before generating or regenerating the Python SDK, make sure the virtual environment is enabled. For details, see Create a virtual environment.

To set up the SDK:

  1. If a python-sdk directory already exists, delete it first to avoid conflicts. Run: rm -rf python-sdk

  2. Generate the new SDK. Run:
    pulumi package gen-sdk <absolutePathToPluginDir>/pulumi-resource-mfcpulumiprovider --language python --out <pathToPythonProject>/python-sdk

  3. Install or upgrade the generated SDK in your Python environment. Run:
    pip install --force-reinstall ./python-sdk/python

Add the Connect provider to __main__.py

Edit the __main__.py file in your project directory to define your resources using the Connect provider. For example:

Copy code
import pulumi 
from pulumi_mfcpulumiprovider import DummyResource

resource = DummyResource("dummyResource", name="example-dummy", description="This is a dummy resource for demonstration purposes.")

See the resource-specific pages for the actual resource definitions.

Configure the stack

To configure the stack, set the required configuration values for the Connect provider. Run:

pulumi config set mfcpulumiprovider:accessToken <your-access-token> --secret

pulumi config set mfcpulumiprovider:connectUrl <your-connect-url>

Setting Required Description
accessToken Yes (secret) Your Connect access token. For details, see Manage access tokens.
connectUrl Yes The URL of your Connect server (e.g.,http://localhost:8081).

Deploy

To complete the deployment:

  1. Deploy your infrastructure. Run: pulumi up

  2. Refresh your stack state to match the actual state of your resources. Run: pulumi refresh