Terraform - 前提生命周期

问题描述 投票:0回答:1

我想做以下事情

  1. 更新 KMS 密钥资源以包含生命周期前提条件

  2. 如果引用的policy为null,则使用构造的本地policy

  3. 为策略添加验证器,如果未设置且密钥类型为直接,则 direct_key_principal 不应为空

resource "aws_kms_key" "direct_key" {
  description              = var.description
  enable_key_rotation      = true
  deletion_window_in_days  = var.deletion_window
  policy                   = var.policy != null ? var.policy : data.aws_iam_policy_document.kms_key_policy_direct[0].json
  key_usage                = var.key_usage
  customer_master_key_spec = var.customer_master_key_spec

lifecycle {
  precondition {
    condition     = var.policy == null && var.key_type == "direct" && length(values(var.direct_key_principal)[0]) > 0
    error_message = "When the policy is not set and the key type is direct, all direct_key_principal elements should be non-empty."
  }
}
variable "direct_key_principal" {
  description = "Principal Information - Type & Identifier required for a 'direct' key"
  type        = map(list(string))
  default = {
    AWS = []
  }
  validation {
    condition     = alltrue([for principal_type in keys(var.direct_key_principal) : contains(["AWS", "Service"], principal_type)])
    error_message = "Valid values for Principal type are AWS and Service."
  }
}

我在下面收到此错误消息

│   12:     condition     = var.policy == null && var.key_type == "direct" && length(values(var.direct_key_principal)[0]) > 0
│     ├────────────────
│     │ var.direct_key_principal is map of list of string with 1 element
│     │ var.key_type is "direct"
│     │ var.policy is "{\n  \"Version\": \"2012-10-17\",\n  \"Statement\": [\n    {\n      \"Sid\": \"\",\n      \"Effect\": \"Allow\",\n      \"Action\": \"kms:*\",\n      \"Resource\": \"arn:aws:*:*:765862725291:*\",\n      \"Principal\": {\n        \"AWS\": \"arn:aws:iam::765862725291499556464691:root\"\n      }\n    },\n    {\n      \"Sid\": \"sid1\",\n      \"Effect\": \"Allow\",\n      \"Action\": [\n        \"kms:TagResource\",\n        \"kms:List*\",\n        \"kms:Get*\",\n        \"kms:Describe*\",\n        \"kms:Decrypt\"\n      ],\n      \"Resource\": [\n        \"arn:aws:*:*:499556464691:*\",\n        \"arn:aws:*:*:999999955:*\"\n      ],\n      \"Principal\": {\n        \"AWS\": \"999999955\"\n      }\n    }\n  ]\n}"
│ 
│ When the policy is not set and the key type is direct, all
│ direct_key_principal elements should be non-empty.
terraform terraform-provider-aws lifecycle preconditions
1个回答
0
投票

此错误消息对于您编写的条件是正确的,因为您要求

var.policy
null
但我们可以从错误消息中看到它不为空。

根据您写的错误消息,我相信您打算按照规则写的是,该规则仅在

var.policy
null
var.key_type
"direct"
时才相关。

如果是这样,您需要先使用条件表达式进行测试,然后在执行您的规则或仅返回

true
以使规则始终通过之间进行选择,如下所示:

  precondition {
    condition     = (
      var.policy == null && var.key_type == "direct" ? 
      length(values(var.direct_key_principal)[0]) > 0 :
      true
    )
    error_message = "When the policy is not set and the key type is direct, all direct_key_principal elements should be non-empty."
  }

如果

var.policy
null
并且
var.key_type
"direct"
那么 Terraform 将使用第一个测试长度是否大于零的表达式的结果。如果这些先决条件中的任何一个不为真,那么 Terraform 将使用第二个表达式的结果,即字面上的
true
,因此将始终通过检查。


请注意,您的错误消息显示“所有 direct_key_principal 元素都应为非空”,但您的条件表达式仅测试地图的一个元素——以

values
的结果中排在第一位的元素为准。使此测试适用于所有元素是可能的,但超出了您在此问题中提出的范围,因此如果您也想实施该更改并且您不确定如何继续,我建议您开始另一个单独的在您确认我上面的回答可以避免您询问的错误后的问题。

© www.soinside.com 2019 - 2024. All rights reserved.