able-barista-69818
07/28/2025, 10:17 AMdev
environment, not create new ones. We control this behavior with a Pulumi config flag.
The Problem
We use a simple if/else
block based on an is_preview
boolean flag to decide whether to get an existing database or create a new one.
Here is a simplified version of our code:
Python
import pulumi
import pulumi_gcp as gcp
config = pulumi.Config()
is_preview = config.get_bool("is_preview") or False
# We print this value and confirm it's True in our CI logs
print(f"Executing with is_preview = {is_preview}")
if not is_preview:
# This block should only run for 'dev', 'staging', etc.
print("Executing 'create' block...")
db = gcp.sql.DatabaseInstance("my-db-instance",
name="my-actual-db-in-gcp",
# ... other settings
)
else:
# This block should run for preview stacks
print("Executing 'get' block...")
db = gcp.sql.get_database_instance(
name="my-actual-db-in-gcp",
project="my-gcp-project-name"
)
# The 'db' variable is then used later in a Cloud Run service
# ...
The is_preview
flag is confirmed to be True
at runtime via the print statement. We expect the else
block to run and for Pulumi to simply get
the existing instance.
However, the pulumi preview
plan incorrectly shows that it will create the gcp.sql.DatabaseInstance
, which is the logic from the if not is_preview
block. This leads to a 409: Instance Already Exists
error during the update.
What We've Ruled Out
• Stuck State: The error occurs even on a brand-new stack that is created and destroyed in the same CI job, so it's not a corrupted state file.
• Refactoring: We have tried refactoring this logic to use separate variable names and even encapsulated it into a pulumi.ComponentResource
, but the exact same issue persists.
• Configuration: We've confirmed via print()
statements that the is_preview
boolean is correctly evaluated as True
during the program's execution.
Our Question
Has anyone encountered a scenario where the Pulumi engine's preview plan seems to ignore a Python if/else
condition for resource declaration? Is there a known pattern or caveat for this type of conditional logic with the GCP provider that we might be missing?
Any help or pointers would be greatly appreciated.
Thank you!echoing-dinner-19531
07/28/2025, 10:41 AMHas anyone encountered a scenario where the Pulumi engine's preview plan seems to ignore a PythonThere is no possible way for the engine to do this. The engine has nothing to do with how your python code is executed. If your getting a create called either there's a bug in your code causing a new resource to be registered, or there's a bug in the get logic causing get_database_instance to somehow turn into a create. Can you confirm that only your "Executing 'get' block..." print statement executes? If you remove thecondition for resource declaration?if/else
db = gcp.sql.DatabaseInstance
line entirely do you still see a 'create'?able-barista-69818
07/28/2025, 1:10 PMdb = gcp.sql.DatabaseInstance
. to be precise we have this in our logs- gcp:sql:DatabaseInstance *******-db creating
echoing-dinner-19531
07/28/2025, 1:54 PMgcp.sql.get_database_instance
could be causing the engine to trigger a create stepable-barista-69818
07/28/2025, 3:42 PMgcp.sql.DatabaseInstance
, but we have the same log with creation of db instead of the read operationechoing-dinner-19531
07/28/2025, 4:16 PMable-barista-69818
07/29/2025, 7:17 AMpulumi up
command.echoing-dinner-19531
07/29/2025, 11:20 AMable-barista-69818
07/29/2025, 12:48 PM