避免在Terraform模块中资源之间的赛车条件
我在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论