【问题标题】:Prevent creating new local_file resource in terraform防止在 terraform 中创建新的 local_file 资源
【发布时间】:2017-12-07 01:28:51
【问题描述】:

我正在谷歌云平台上创建一个pkcs12格式的服务帐号密钥,将其保存为文件并将其转换为pem格式。

第二次应用计划时,即使服务帐户密钥未更改,也会重新创建 p12 和 pem 文件。 如何避免重新创建文件?

resource "google_service_account_key" "test_key_v1" {
  service_account_id = "${google_service_account.test.id}"
  private_key_type = "TYPE_PKCS12_FILE"
}

resource "local_file" "test_key_v1_file" {
  content     = "${base64decode(google_service_account_key.test_key_v1.private_key)}"
  filename = "./keys/test-key-v1.p12"

  provisioner "local-exec" {
    command = "cat ./keys/test-key-v1.p12 | openssl pkcs12 -nodes -nocerts -passin pass:notasecret | openssl rsa > ./keys/test-key-v1.pem"
  }
}

terraform aply on the second time 给了我这个信息:

-/+ local_file.test_key_v1_file (new resource required)
      id:       "13a5202a06ef07569caa544efe2c21cd2b534d11" => <computed> (forces new resource)

【问题讨论】:

    标签: terraform


    【解决方案1】:

    您可以使用 null_resource 配置器,如果 trigger 变量已更改,它会执行其他部分

    我已经更新了你的例子(我没有测试,但想法就在这里:))

    资源 test_key_v1_file 仅在 contenthash 变量发生变化时重新运行

    resource "google_service_account_key" "test_key_v1" {
      service_account_id = "${google_service_account.test.id}"
      private_key_type = "TYPE_PKCS12_FILE"
    }
    
    
    resource "null_resource" "test_key_v1_file" {
      triggers {
      contenthash="${base64decode(google_service_account_key.test_key_v1.private_key)}"
      }
    
      provisioner "local-exec" {
      command = "echo \"${base64decode(google_service_account_key.test_key_v1.private_key)}\" | openssl pkcs12 -nodes -nocerts -passin pass:notasecret | openssl rsa > ./keys/test-key-v1.pem"
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-08-28
      • 1970-01-01
      • 2022-10-24
      • 2022-07-24
      • 1970-01-01
      • 2018-12-23
      • 2018-09-24
      • 1970-01-01
      • 2018-10-23
      相关资源
      最近更新 更多