使用 Terraform 管理您的 Deno Deploy 和 Deno Subhosting 项目
我们最近推出了 Deno Subhosting(基于 Deno Deploy 基础设施),这是运行和托管用户代码最简单、最安全的方式。公司可以使用 Subhosting 通过开发者平台或第三方应用程序市场扩展其产品的功能。 Deno Subhosting 的简单 API 使其易于集成到您的平台中,因此您可以以编程方式管理部署、项目等等。
但是,许多开发人员更喜欢使用基础设施即代码模式(如 Terraform)在云中管理业务关键基础设施,因为它是配置和管理云服务的一种安全、可靠且可预测的方式。现在,我们很高兴地宣布,您可以使用我们官方的 Deno Terraform Provider 通过 Terraform 使用 Deno Subhosting 和 Deno Deploy。
此提供程序将允许您使用 Terraform 管理大量项目、自定义域、部署等等。我们将维护此提供程序,并使其与新的 Subhosting API保持同步。
虽然我们仍在努力完成文档,但您可以在 此处找到自动生成的提供程序文档,以及 在 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 和访问令牌,您可以在部署仪表板中生成。在下面的示例中,我们硬编码了这些参数,它们分别在 terraform 文件中;或者,您可以通过环境变量 DENO_DEPLOY_ORGANIZATION_ID
和 DENO_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
数据源定义“资产”。您可以将其视为要上传的一组文件。第一个必需参数 path
用作 Terraform 搜索资产的根目录路径。第二个参数 pattern
是一个 glob 字符串,其匹配项决定要包含哪些资产。
在本示例中,src/main.ts
和 static/cat.png
将被包含。
data "deno_assets" "sample_assets" {
path = "../"
pattern = "{src,static}/**/*.{ts,png}"
}
现在我们可以使用 deno_deployment
资源创建部署了。此资源接受参数 project_id
和到目前为止创建的 assets
数据。还可以提供其他参数,例如 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
应该显示您的可爱猫咪图片。
下一步
尽管 Deno Subhosting 提供了简单的 API 来管理 Deno Deploy 上的项目、部署等等,但对于熟悉基础设施即代码模式的人来说,使用 Deno Terraform Provider 可能更简单,尤其是在协调数百个项目时。
我们希望添加更多文档和示例,但如果您需要技术帮助,请 加入我们活跃的 Discord 或 在此创建问题。
💡️ 了解如何在我们的直播中使用 Deno Subhosting 解锁平台价值的最后 10%,Kevin 和 Bert 在直播中提供了来自创新公司的 Subhosting 示例,并演示了 Subhosting API。