跳到主要内容
Terraform

使用 Terraform 管理您的 Deno Deploy 和 Deno Subhosting 项目

我们最近发布了 Deno Subhosting (构建于 Deno Deploy 基础设施之上),这是运行和托管用户代码的最简单且最安全的方式。公司可以使用 Subhosting 通过开发者平台或第三方应用市场来扩展其产品的功能。Deno Subhosting 简单的 API 使其易于集成到您的平台中,因此您可以编程方式管理部署、项目等。

然而,许多开发者更喜欢使用基础设施即代码的模式(如 Terraform)在云端管理关键业务基础设施,因为它是一种安全、可靠且可预测的方式来配置和管理云服务。现在,我们激动地宣布,您可以通过我们的官方 Deno Terraform Provider,使用 Terraform 来管理 Deno Subhosting 和 Deno Deploy。

此 Provider 将允许您使用 Terraform 管理大量的项目、自定义域名、部署等等。我们将维护此 Provider,并使其与新的 Subhosting API 保持同步。

虽然我们仍在努力完成文档的编写,但您可以在此处找到自动生成的 Provider 文档,以及 GitHub 上找到许多可用的示例。

继续阅读以了解更多关于

如何将本地代码目录发布到 Deno Deploy

为了给您展示 Deno Terraform Provider 的工作原理示例,让我们一起完成一个将本地代码目录发布到 Deno Deploy 的示例。

假设有以下项目结构(为本示例简化)

.
├── deno.json
├── src
│   └── main.ts
├── static
│   └── cat.png
└── terraform
    └── main.tf

我们想要部署 src/main.ts,它访问 static/cat.png 和一个环境变量 FOO

Deno.serve(async (req) => {
  const url = new URL(req.url);

  // Respond with `cat.png` if requested to `/cat.png`.
  // Otherwise respond with the value of env var `FOO`.
  switch (url.pathname) {
    case "/cat.png": {
      const file = await Deno.open("./static/cat.png");
      return new Response(file.readable, {
        headers: {
          "content-type": "image/png",
        },
      });
    }
    default: {
      return new Response(`FOO is ${Deno.env.get("FOO")}`);
    }
  }
});

定义 Terraform 文件

接下来,让我们定义 .tf 文件。请注意,有两个必需的参数:您的组织 ID 和访问令牌,您可以在 deploy 仪表板上生成访问令牌。在下面的示例中,我们将它们硬编码在 terraform 文件中;或者,您可以通过环境变量 DENO_DEPLOY_ORGANIZATION_IDDENO_DEPLOY_TOKEN 分别传递它们。

terraform {
  required_providers {
    deno = {
      source = "denoland/deno"
    }
  }
}

provider "deno" {
  // As an alternative to specifying your organization ID in the terraform code
  // itself, you can also use the `DENO_DEPLOY_ORGANIZATION_ID` environment
  // variable.
  organization_id = "e1ae3bcc-923e-4930-a6a7-5841e69849ed"

    // Similarly, you may omit the token here and use the `DENO_DEPLOY_TOKEN`
  // environment variable.
  token = "ddo_3JfdN41GxH0PufmzcoahcJtQXcbde144Z7l9"
}

接下来,让我们使用 deno_project 资源创建一个项目。请注意,name 是可选的(如果省略,则会自动生成一个随机名称),但它必须是全局唯一的。

resource "deno_project" "sample_project" {
  name = "awesome-deno-40"
}

然后,让我们使用 deno_assets 数据源定义“assets”。您可以将其视为要上传的一组文件。第一个必需的参数 path 用作 Terraform 查找 assets 的根目录路径。第二个参数 pattern 是一个 glob 字符串,其匹配项确定要包含的 assets。

在此示例中,将包含 src/main.tsstatic/cat.png

data "deno_assets" "sample_assets" {
  path    = "../"
  pattern = "{src,static}/**/*.{ts,png}"
}

我们现在准备好使用 deno_deployment 资源创建部署。此资源接受到目前为止已创建的参数 project_idassets 数据。也可以提供其他参数,例如 env_vars。(有关更多详细信息,请查看此用法示例。)

resource "deno_deployment" "sample_deployment" {
  project_id      = deno_project.sample_project.id
  entry_point_url = "src/main.ts"
  assets          = data.deno_assets.sample_assets.output
  env_vars = {
    FOO = "42"
  }
}

就是这样!

运行 Terraform

让我们运行 terraform plan 来看看我们是否正确配置了所有内容

$ cd terraform
$ terraform plan
data.deno_assets.sample_assets: Reading...
data.deno_assets.sample_assets: Read complete after 0s

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # deno_deployment.sample_deployment will be created
  + resource "deno_deployment" "sample_deployment" {
      + assets          = {
          + "src/main.ts" = {
              + content_source_path = "../src/main.ts"
              + kind                = "file"
            },
          + "static/cat.png" = {
              + content_source_path = "../static/cat.png"
              + kind                = "file"
            },
        }
      + created_at      = (known after apply)
      + deployment_id   = (known after apply)
      + domains         = (known after apply)
      + entry_point_url = "src/main.ts"
      + env_vars        = {
          + "FOO" = "42"
        }
      + project_id      = (known after apply)
      + status          = (known after apply)
      + updated_at      = (known after apply)
      + uploaded_assets = (known after apply)
    }

  # deno_project.sample_project will be created
  + resource "deno_project" "sample_project" {
      + created_at = (known after apply)
      + id         = (known after apply)
      + name       = "awesome-deno-40"
      + updated_at = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.

看起来不错!现在让我们运行 terraform apply

$ terraform apply
data.deno_assets.sample_assets: Reading...
data.deno_assets.sample_assets: Read complete after 0s

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # deno_deployment.sample_deployment will be created
  + resource "deno_deployment" "sample_deployment" {
      + assets          = {
          + "src/main.ts" = {
              + content_source_path = "../src/main.ts"
              + kind                = "file"
            },
          + "static/cat.png" = {
              + content_source_path = "../static/cat.png"
              + kind                = "file"
            },
        }
      + created_at      = (known after apply)
      + deployment_id   = (known after apply)
      + domains         = (known after apply)
      + entry_point_url = "src/main.ts"
      + env_vars        = {
          + "FOO" = "42"
        }
      + project_id      = (known after apply)
      + status          = (known after apply)
      + updated_at      = (known after apply)
      + uploaded_assets = (known after apply)
    }

  # deno_project.sample_project will be created
  + resource "deno_project" "sample_project" {
      + created_at = (known after apply)
      + id         = (known after apply)
      + name       = "awesome-deno-40"
      + updated_at = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

deno_project.sample_project: Creating...
deno_project.sample_project: Creation complete after 0s [id=82672716-5aee-4279-a7da-3a3554ec9f74]
deno_deployment.sample_deployment: Creating...
deno_deployment.sample_deployment: Creation complete after 4s

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

完成!让我们看一下 Terraform 状态以获取访问部署的 URL

$ terraform state show deno_deployment.sample_deployment
# deno_deployment.sample_deployment:
resource "deno_deployment" "sample_deployment" {
    // ...
    deployment_id   = "rpxayrskrhzt"
    domains         = [
        "awesome-deno-40-rpxayrskrhzt.deno.dev",
    ]
    entry_point_url = "src/main.ts"
    // ...
}

为了检查我们的代码是否已成功部署在 Deno Deploy 上并正常工作,让我们访问 https://awesome-deno-40-rpxayrskrhzt.deno.dev/,它应该显示配置的环境变量。此外,访问 https://awesome-deno-40-rpxayrskrhzt.deno.dev/cat.png 应该显示您可爱的猫图片。

Cute cat picture

下一步是什么

即使 Deno Subhosting 提供了简单的 API 来管理 Deno Deploy 上的项目、部署等,但对于那些熟悉基础设施即代码模式的人来说,使用 Deno Terraform Provider 可能会更简单,尤其是在协调数百个项目时。

我们希望添加文档和更多示例,但是如果您需要技术帮助,请加入我们活跃的 Discord在此处创建 issue

💡️ 了解如何通过 Deno Subhosting 解锁您平台最后 10% 的价值,在我们的直播中,Kevin 和 Bert 将提供来自创新公司的 Subhosting 示例,并演示 Subhosting API。

Linkedin Event