避免在Terraform模块中资源之间的赛车条件

发布于 2025-02-14 00:00:53 字数 1099 浏览 0 评论 0原文

我在Terraform模块中有这两个资源定义:

resource "null_resource" "install_dependencies" {
  provisioner "local-exec" {
    command = "pip install -r requirements.txt -t . ; exit"
  }



  triggers = {
    dependencies_versions = filemd5(".${var.package_requirements_path}")
  }
}


data "archive_file" "lambda_source_package" {
  type        = "zip"
  source_dir  = ".${var.source_directory}"
  output_path = var.zipped_lambda

}

NULL资源安装了许多依赖项。然后,这些资源由lambda_source_package存档。但是,我发现,数据“ Archive_file”“ lambda_source_package”在安装所有PIP软件包之前执行。

为了解决这个问题,我为此使用了依赖性 meta-argument。我意识到这也无法解决问题,因为Terraform找不到数据“ Archive_file”“ lambda_source_package”在完成执行之前生成的zip文件。输出错误消息是│错误:无法加载“ ../outputs/xx.zip”:open ../outputs/xx.zip:no这样的文件或目录

文档说, 依赖性参数“创建更多保守的计划,替换比必要的更多资源”,因此,不建议在创建模块时使用它(这是我的用例)。

该文档还表明:“我们建议在可能的情况下使用表达式参考来暗示依赖关系。”这是什么意思?如何将其应用于我的用例?

I have these two resource definitions in a terraform module:

resource "null_resource" "install_dependencies" {
  provisioner "local-exec" {
    command = "pip install -r requirements.txt -t . ; exit"
  }



  triggers = {
    dependencies_versions = filemd5(".${var.package_requirements_path}")
  }
}


data "archive_file" "lambda_source_package" {
  type        = "zip"
  source_dir  = ".${var.source_directory}"
  output_path = var.zipped_lambda

}

The null resource installs a host of dependencies. These resources are then archived by the lambda_source_package. However, I find that, data "archive_file" "lambda_source_package" executes before all the pip packages are installed.

To solve this, I used the depends_on meta-argument for this. I realized this also doesn't solve the issue since terraform isn't able to find the zip file generated by data "archive_file" "lambda_source_package" before it completes execution. The output error message is │ Error: unable to load "../outputs/xx.zip": open ../outputs/xx.zip: no such file or directory

The documentation says that, the depends_on argument "create more conservative plans that replace more resources than necessary" and hence it is not advisable to use it especially when creating a module(which is my use case).

The documentation also suggests that "Instead of depends_on, we recommend using expression references to imply dependencies when possible.". What does this mean? How can I apply it to my use-case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文