From 8b5b876cfb71697b37e4f1e6886cf712d641c3ac Mon Sep 17 00:00:00 2001 From: Emil Christensen Date: Mon, 16 Dec 2024 16:03:11 -0500 Subject: [PATCH] Adds infra recipe for prefect-cloud workspace --- infra/prefect-cloud/main.tf | 52 ++++++++++++++++++++++++++++++++ infra/prefect-cloud/outputs.tf | 8 +++++ infra/prefect-cloud/variables.tf | 3 ++ 3 files changed, 63 insertions(+) create mode 100644 infra/prefect-cloud/main.tf create mode 100644 infra/prefect-cloud/outputs.tf create mode 100644 infra/prefect-cloud/variables.tf diff --git a/infra/prefect-cloud/main.tf b/infra/prefect-cloud/main.tf new file mode 100644 index 0000000..4a12a41 --- /dev/null +++ b/infra/prefect-cloud/main.tf @@ -0,0 +1,52 @@ +// Declare that we need the Prefect provider +// NOTE: Fails when both PREFECT_CLOUD_ACCOUNT_ID and PREFECT_API_URL are set +terraform { + required_providers { + prefect = { + source = "PrefectHQ/prefect" + } + } +} + +// Create a workspace +locals { + workspace_handle = replace(replace(lower(var.prefect_workspace_name), " ", "-"), "_", "-") +} + +resource "prefect_workspace" "workspace" { + name = var.prefect_workspace_name + handle = local.workspace_handle +} + +// Create a service account and add it to the workspace +resource "prefect_service_account" "service_account" { + name = "prefect-worker" + account_role_name = "Member" + depends_on = [prefect_workspace.workspace] +} + +data "prefect_workspace_role" "worker" { + name = "Worker" + depends_on = [prefect_workspace.workspace] +} + +resource "prefect_workspace_access" "workspace_access" { + accessor_type = "SERVICE_ACCOUNT" + accessor_id = prefect_service_account.service_account.id + workspace_id = prefect_workspace.workspace.id + workspace_role_id = data.prefect_workspace_role.worker.id +} + +// Create basic process and docker work pools +resource "prefect_work_pool" "process_work_pool" { + name = "process" + type = "process" + workspace_id = prefect_workspace.workspace.id +} + +resource "prefect_work_pool" "docker_work_pool" { + name = "docker" + type = "docker" + workspace_id = prefect_workspace.workspace.id +} + diff --git a/infra/prefect-cloud/outputs.tf b/infra/prefect-cloud/outputs.tf new file mode 100644 index 0000000..6dff579 --- /dev/null +++ b/infra/prefect-cloud/outputs.tf @@ -0,0 +1,8 @@ +output "prefect_workspace_id" { + value = prefect_workspace.workspace.id +} + +output "prefect_service_account_api_key" { + value = prefect_service_account.service_account.api_key + sensitive = true +} diff --git a/infra/prefect-cloud/variables.tf b/infra/prefect-cloud/variables.tf new file mode 100644 index 0000000..9d62dcf --- /dev/null +++ b/infra/prefect-cloud/variables.tf @@ -0,0 +1,3 @@ +variable "prefect_workspace_name" { + description = "The name of the workspace" +}