如何使提供程序假设_角色块有条件

Mat*_*t W 6 amazon-web-services conditional-statements terraform terraform-provider-azure

我有一个provider块,我想为其提供assume_role属性,但前提是它没有在我的本地计算机上运行。

islocal我在所有环境文件中定义了一个变量.tfvars,只有本地文件的值为true.

这是provider块:

provider "aws" {
    region = var.region1
    profile = var.islocal == true ? "default" : null # ONLY USED LOCALLY
    
    assume_role {       # NOT TO BE USED LOCALLY
        role_arn = var.terraform_execution_role
    }
}
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 如果我将该role_arn属性设置为null是否会使该assume_role块无效?(即:与不存在相同)
  2. 如果该assume_role块确实有影响,即使值为role_arnnull我怎样才能在为 时完全删除var.islocaltrue

我考虑过动态块,但我不确定如何构建它。

Mar*_*cin 8

您可以在您的提供程序中使用动态块

provider "aws" {
    region = var.region1
    profile = var.islocal == true ? "default" : null # ONLY USED LOCALLY
    
  dynamic "assume_role" {    
    for_each = var.islocal == true ? [] : [1]  
    content {      
        role_arn = var.terraform_execution_role
    }  
  }
}
Run Code Online (Sandbox Code Playgroud)