我正在以pkcs12格式在Google云平台上创建服务帐户密钥,将其保存到文件并将其转换为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第二次给我这个消息:
-/+ local_file.test_key_v1_file (new resource required)
id: "13a5202a06ef07569caa544efe2c21cd2b534d11" => <computed> (forces new resource)
答案 0 :(得分:0)
您可以使用 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"
}
}