<@ULVA73B9P> im using prefect open source, can i c...
# ask-marvin
d
@Marvin im using prefect open source, can i create the automation using python code and deploy this using terraform, and what i can do
m
Error: Event loop is closed
Sorry, I encountered an error while processing your request. Please try again.
d
@Marvin im using prefect open source, can i create the automation using python code and deploy this using terraform, and what i can do
m
Error: Event loop is closed
Sorry, I encountered an error while processing your request. Please try again.
n
oof
can i create the automation using python code
yes!
deploy this using terraform
yes!
d
@Marvin i mean, im using python to create the code of automation, and using terraform to create this resource in the same time
m
thought for 164.8 seconds
Got it — you’re creating the same Prefect Automation from both Python and Terraform at the same time. Short answer: don’t do that. You’ll get drift and last‑write‑wins behavior. Pick one tool to be the source of truth for each resource. Recommended patterns: - If Terraform should own Automations (most common) - Stop creating/updating that Automation in Python. - Import the existing Automation into Terraform state, then manage it only via Terraform going forward. - Use Python only for runtime actions (triggering flow runs, querying state), not for changing the Automation spec. - If Python should own Automations - Don’t define that Automation as a Terraform resource. If you need to reference it in Terraform, use a data source instead of a resource. - Keep Terraform focused on infra (work pools, variables, service accounts, etc.). How to import an existing Automation into Terraform:
Copy code
# If the automation is in your current workspace
terraform import prefect_automation.my_auto "automation-id-uuid"

# If it lives in another workspace
terraform import prefect_automation.my_auto "automation-id-uuid,workspace-id-uuid"
Example Terraform Automation (runs a deployment when a custom event arrives):
Copy code
resource "prefect_automation" "webhook_trigger" {
  name        = "webhook-triggered-deployment"
  description = "Runs deployment on external webhook event"
  enabled     = true

  trigger = {
    event = {
      posture  = "Reactive"
      expect   = ["external.event.happened"]
      match    = jsonencode({ "prefect.resource.id" = "prefect.flow-run.*" })
      threshold = 1
      within    = 0
    }
  }

  actions = [
    {
      type          = "run-deployment"
      source        = "selected"
      deployment_id = prefect_deployment.example_deployment.id
      parameters    = jsonencode({})
    }
  ]
}
Tips to avoid conflicts: - One owner per resource: Terraform OR Python, not both. - If switching ownership to Terraform, import existing resources and remove Python code that mutates them. - Use Terraform for stable config (Automations, Work Pools, Variables, Service Accounts). Use Python for runtime behavior (trigger runs, read state). - If you absolutely must mix, consider Terraform
lifecycle { ignore_changes = [...] }
for specific fields — but this weakens IaC guarantees. Useful links: - Provider docs: Terraform Registry: Prefect provider - Automation resource docs: prefect_automation - Examples: provider examples - API parity tracker: API parity matrix - Prefect IaC guidance (Prefect 3.x): Infrastructure as Code with Prefect If you share how you’re defining the Automation in Python (trigger + actions), I can translate it into a Terraform resource for you and outline the exact import steps for your workspace.