Hey, Is anyone able to get `pulumi` to work with ...
# general
b
Hey, Is anyone able to get
pulumi
to work with a
bazel
-managed python environment? I think
pulumi up
is trying to install its own dependencies, which does not play well with bazel philosophy. Is there any way to combine the two? Or in general, how does pulumi deal with monorepo setups, where you tend to share all the libraries instead of creating many small python virtual environments?
h
you can disable pulumi's management of the virtual environment: https://www.pulumi.com/docs/iac/languages-sdks/python/#self-managed-virtual-environments
I'd probably approach this by creating a py_library of the pulumi program, and then creating a small wrapper script that invokes pulumi with it
essentially something like this:
Copy code
# BUILD.bazel

load("@rules_python//python:defs.bzl", "py_binary", "py_library")
load("@pulumi_pip//:requirements.bzl", "requirement")

# Your Pulumi program - customize this with your infrastructure code
py_library(
    name = "pulumi_program",
    srcs = ["__main__.py"],
    deps = [
        # Add your Pulumi provider dependencies here
        requirement("pulumi"),
        requirement("pulumi-aws"),
    ],
)

# Bazel wrapper that execs Pulumi CLI with the right Python environment
py_binary(
    name = "pulumi",
    srcs = ["pulumi_wrapper.py"],
    main = "pulumi_wrapper.py",
    data = glob(["Pulumi*.yaml"]),
    deps = [
        ":pulumi_program",  # Include the program so deps are in PYTHONPATH
    ],
)
Copy code
# pulumi_wrapper.py

#!/usr/bin/env python3
"""
Wrapper that execs Pulumi CLI with Bazel's Python environment.
Bazel's py_binary automatically sets up PYTHONPATH with all dependencies.
"""
import os
import sys

# Exec pulumi - PYTHONPATH is set by Bazel, Pulumi.yaml is in runfiles
os.execvp('pulumi', ['pulumi'] + sys.argv[1:])
Copy code
# Pulumi.yaml
name: pulumi-bazel-example
runtime:
  name: python
  options:
    virtualenv: none  # We're using Bazel's Python environment, so tell pulumi not to try to manage the venv itself.
description: Example Pulumi program running in a Bazel environment
๐Ÿ™Œ 1
you'll run
bazel run :pulumi -- preview
this will invoke the wrapper program with correct deps for your program. it'll immediately exec the pulumi cli, which will be run in an environment where it's able to directly run your program without needing to manage the virtual env itself, (since the environment is already set up by bazel)
I think there is an argument for separating the deps for a pulumi program from the rest of your monorepo, because I think the value of trying to keep them consistent with the rest of your codebase is somewhat low.
b
Thatโ€™s so cool, thank you so much for taking the time to write this down! ๐Ÿ™‚
This makes a ton of sense, let me try setting it up. Thank you very much!!
๐Ÿ’œ 1
114 Views