簡體   English   中英

通過 terraform 創建資源時如何使用 bash 腳本中的值

[英]How to use value from bash script when creating resource via terraform

這是一個通用的工作流程terraform問題(我是terraform初學者)。

我需要通過 terraform 創建一個新的 aws 資源。我的結構是

resource "some_aws_resource" "resource_name" {
   name = var.resource_name
   role_name = var.role_name

   another_key = another_value
}

例如,值another_value來自用戶通過 bash 腳本find_another_value.sh輸入的變量

resource "null_resource" "find_another_value" {
   provisioner "local-exec" {
      command     = "shell_scripts/find_another_value.sh"
    interpreter = ["/bin/bash"]
    working_dir = path.module

    environment = {
      DAR = var.aws_dar
      RAD = var.aws_rad
    }
   }

}

應該返回another_value

如何將 null_resource 的null_resource another_value傳遞給第一個資源,並確保非 null 資源等待null_resource返回值?

find_another_value.sh腳本將使用輸入變量通過 aws cli 查詢 aws 以找到它需要返回的值。

如果有更好/更簡單的方法也很高興知道。

對於等待,您將使用 depends_on = []

resource "some_aws_resource" "resource_name" {
   depends_on =[null_resource.find_another_value]
   name = var.resource_name
   role_name = var.role_name
   another_key = another_value.value
}

從資源中提取價值我們將寫出一個資源我們將添加價值編輯資源,然后是名稱,然后是我們想要它的值的行

output "another_value" {

  description = "another_value from null resource find_another_value ."

  value = null_resource.find_another_value.provisioner "local-exec"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM