Skip to main content

Terraform

通过我们与Terraform 的集成,您可以将基础设施的状态与 Port 中代表它们的实体结合起来。

通过使用 Port 的 Terraform Provider,您可以轻松地将 Port 与现有的 IaC 定义集成,Terraform 提供的每个资源也可以使用相同的 .tf 定义文件报告到软件目录。

Port terraform Provider 您可以查看 Terraform Provider 的官方注册表页面here

💡 Terraform Provider 常见用例

例如,我们的 Terraform Providers 可让您直接从 IaC 定义中获取数据,轻松填充软件目录:

  • 报告云账户
  • 报告数据库
  • 报告lambdas托管 Kubernetes 服务(EKS、AKS、GKE 等);
  • 等等。

安装

Prerequisites

To install and use Port's Terraform provider, you will need to install the Terraform CLI

To install the Terraform provider, create a .tf file specifying the provider and the required Port credentials:

terraform {
required_providers {
port = {
source = "port-labs/port-labs"
version = "~> 1.0.0"
}
}
}

provider "port" {
client_id = "{YOUR CLIENT ID}" # or set the environment variable PORT_CLIENT_ID
secret = "{YOUR CLIENT SECRET}" # or set the environment variable PORT_CLIENT_SECRET
}

Then run the following command to install the provider in your Terraform workspace:

terraform init

Terraform 定义结构

Port 的 Terraform Provider 支持以下资源将数据摄取到目录中:

`Port实体

port_entity 资源定义了一个基本实体:

resource "port_entity" "myEntity" {
identifier = "myEntity" # Entity identifier
title = "My Entity" # Entity title
blueprint = "myBlueprint" # Identifier of the blueprint to create this entity from

# Entity property values
properties = {
...
}
...
# Entity relations
...
}

以下参数是必需的:

  • blueprint` - 据以创建此实体的蓝图的标识符;
  • title: 实体的标题;
  • 一个或多个properties 模式定义。

还可以指定以下参数作为 port_entity 资源的一部分:

  • identifier - 实体的标识符;
    • 如果未提供identifier,则会自动生成一个标识符。
  • teams - 拥有该实体的团队数组;
  • run_id - 创建实体的操作的运行 ID。

属性模式

properties 模式会为实体的某个属性指定一个指定值。

resource "port_entity" "myEntity" {
identifier = "myEntity" # Entity identifier
title = "My Entity" # Entity title
blueprint = "myBlueprint" # Identifier of the blueprint to create this entity from

properties = {
string_props = {
"myStringProp" = "My string"
}

number_props = {
"myNumberProp" = 7
}

array_props = {
string_items = {
"myArrayProp" = ["a", "b", "c"]
}
}
}
# Entity relations
...
}

定义

properties = {
string_props = {
"myStringProp" = "My string"
}
}

relations 模式

relations 模式将目标实体映射到源实体定义:

resource "port_entity" "myEntity" {
identifier = "myEntity" # Entity identifier
title = "My Entity" # Entity title
blueprint = "myBlueprint" # Identifier of the blueprint to create this entity from

# Entity properties
...

relations = {
single_relations = {
"mySingleRelation" = "myTargetEntityIdentifier"
}
}

relations = {
many_relations = {
"myManyRelation" = ["myTargetEntityIdentifier", "myTargetEntityIdentifier2"]
}
}
}

定义

模式如下

relations {
single_relations = {
# Key-value pair of the relation identifier and the target identifier
"mySingleRelation" = "myTargetEntityIdentifier"
}
}

使用 Terraform Provider 被用于数据

要使用 Terraform Provider 将数据引用到软件目录,需要在 Terraform 定义文件中定义port_entity 资源:

要使用 Terraform 创建实体,请在.tf定义文件中添加port_entity资源:

resource "port_entity" "myEntity" {
identifier = "myEntity"
title = "My Entity"
blueprint = "myBlueprint"

properties = {
"string_props" = {
"myStringProp" = "My string"
}
"number_props" = {
"myNumberProp" = 7
}
"boolean_props" = {
"myBooleanProp" = true
}
"object_props" = {
"myObjectProp" = jsonencode({ "my" : "object" })
}
"array_props" = {
"string_props" = {
"myArrayProp" = ["a", "b", "c"]
}
}
}
}

然后运行以下命令应用更改并更新目录:

# To view Terraform's planned changes based on your .tf definition file:
terraform plan
# To apply the changes and update the catalog
terraform apply

运行这些命令后,您将看到目录已更新为新实体。

将现有数据导入 Terraform 状态

要将现有实体导入 Terraform 状态,请在.tf定义文件中添加port_entity资源:


resource "port_entity" "myEntity" {
...
}

然后运行以下命令将实体导入 Terraform 状态:

terraform import port_entity.myEntity "{blueprintIdentifier}:{entityIdentifier}"

示例

有关实用配置及其相应的蓝图定义,请参阅examples 页面。