首页 > 解决方案 > 如何为 Terraform 提供程序模式的 2 个字段编写 ValidateFunc?

问题描述

我正在编写一个自定义提供程序,其中我在架构中定义了 2 个字段,即:host_instance_type 和 storage_capacity。为不同 host_instance_type 的行为不同的 storage_capacity 编写验证函数的最佳方法是什么?在此 storage_capacity,必须只为特定的 host_instance_type(比如 A)指定。但是不应为 host_instance_type 指定 storage_capacity(比如 B)。

标签: terraform

解决方案


ValidateFunc,不幸的是,仅将其定义为参数的字段。

但是,您可以在资源本身的 CRUD(创建、读取、更新、删除)逻辑中执行验证逻辑。例如,查看 AWS 的 lambda 函数资源如何在其“创建”函数中执行验证:https ://github.com/terraform-providers/terraform-provider-aws/blob/master/aws/resource_aws_lambda_function.go#L297


推荐阅读