Terraform
通过我们与Terraform 的集成,您可以将基础设施的状态与 Port 中代表它们的实体结合起来。
通过使用 Port 的 Terraform Provider,您可以轻松地将 Port 与现有的 IaC 定义集成,Terraform 提供的每个资源也可以使用相同的 .tf
定义文件报告到软件目录。
💡 Terraform Provider 常见用例
例如,我们的 Terraform Providers 可让您直接从 IaC 定义中获取数据,轻松填充软件目录:
- 报告云账户;
- 报告数据库;
- 报告lambdas和托管 Kubernetes 服务(EKS、AKS、GKE 等);
- 等等。
安装
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
...
}
定义
- String
- Number
- Boolean
- Object
- Array
- URL
- User
- Team
- Datetime
- Timer
- YAML
properties = {
string_props = {
"myStringProp" = "My string"
}
}
properties = {
number_props = {
"myNumberProp" = 7
}
}
properties = {
boolean_props = {
"myBooleanProp" = true
}
}
properties = {
object_props = {
"myObjectProp" = jsonencode({ "my" : "object" })
}
}
properties = {
array_props = {
string_props = {
"myArrayProp" = ["a", "b", "c"])
}
}
}
properties = {
string_props = {
"myUrlProp" = "https://example.com"
}
}
properties = {
string_props = {
"myEmailProp" = "[email protected]"
}
}
properties = {
string_props = {
"myUserProp" = "argo-admin"
}
}
properties = {
string_props = {
"myTeamProp" = "argo-admins"
}
}
properties = {
string_props = {
"myDatetimeProp" = "2023-04-18T11:44:15.345Z"
}
}
properties = {
string_props = {
"myUserProp" = "argo-admin"
}
}
properties = {
string_props = {
"myYamlProp" = "myKey: myValue"
}
}
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"]
}
}
}
定义
- Single
- Many
模式如下
relations {
single_relations = {
# Key-value pair of the relation identifier and the target identifier
"mySingleRelation" = "myTargetEntityIdentifier"
}
}
模式如下
relations {
many_relations = {
# Key-value pair of the relation identifier and the target identifiers
"myManyRelation" = ["myTargetEntityIdentifier", "myTargetEntityIdentifier2"]
}
}
使用 Terraform Provider 被用于数据
要使用 Terraform Provider 将数据引用到软件目录,需要在 Terraform 定义文件中定义port_entity
资源:
- Create
- Update
- Delete
要使用 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 更新实体,请更新.tf
定义文件中现有的port_entity
资源,然后运行terraform apply
。
也可以使用 Port 的 Terraform Provider 开始管理现有实体,要开始管理现有实体,请在.tf
定义文件中添加新的port_entity
资源,并进行所需的更改:
resource "port_entity" "myExistingEntity" {
identifier = "myExistingEntity"
title = "My Entity"
blueprint = "myBlueprint"
# Entity properties and relations
...
}
- 指定实体的 "标识符 "非常重要,否则 terraform 将创建一个带有自动生成的标识符的新实体。
- Port 的 Terraform Providers 使用create/override 策略,这意味着对于现有实体,资源定义中未定义的任何属性都将被用于空值。
要使用 Terraform 删除实体,只需删除在 .tf
定义文件中定义的 port_entity
资源,然后运行 terraform apply
即可。
将现有数据导入 Terraform 状态
- Blueprint
- Entity
要将现有蓝图导入 Terraform 状态,请在.tf
定义文件中添加port_blueprint
资源:
resource "port_blueprint" "myBlueprint" {
...
}
然后运行以下命令将蓝图导入 Terraform 状态:
terraform import port_blueprint.myBlueprint "{blueprintIdentifier}"
要将现有实体导入 Terraform 状态,请在.tf
定义文件中添加port_entity
资源:
resource "port_entity" "myEntity" {
...
}
然后运行以下命令将实体导入 Terraform 状态:
terraform import port_entity.myEntity "{blueprintIdentifier}:{entityIdentifier}"
示例
有关实用配置及其相应的蓝图定义,请参阅examples 页面。