避免在目录Terraform中重复代码以归档文件

发布于 2025-02-12 14:07:45 字数 431 浏览 0 评论 0原文

我在下面的文件中定义了lambda函数:

../lambda_functions
├── index_to_s3.py
├── from_s3.py
├── to_s3.py
├── to_fetch.py
├── sql_fetch.py
└── sql_def.py

我想将这些功能中的每个功能存档到自己的zip文件中,然后将每个功能作为单独的lambda上传。目前,我必须重复此代码

data "archive_file" "from_s3" {
  type        = "zip"
  source_file = local.from_s3_source
  output_path = local.from_s3_output
}

以使每个lambda能够执行此操作。是否有一种保持代码干燥的最佳方法?

I lambda functions defined in the files below:

../lambda_functions
├── index_to_s3.py
├── from_s3.py
├── to_s3.py
├── to_fetch.py
├── sql_fetch.py
└── sql_def.py

I want to archive each one of these functions into their own zip file and upload each as a separate lambda. Currently, I have to repeat this code

data "archive_file" "from_s3" {
  type        = "zip"
  source_file = local.from_s3_source
  output_path = local.from_s3_output
}

for each lambda to be able to do this. Is there an optimal way to keep the code dry?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2025-02-19 14:07:45

您可以组合 for_each fileset 为此:

data "archive_file" "from_s3" {

  for_each = fileset("${path.module}", "lambda_functions/*.py")
  type        = "zip"
  source_file = "${path.module}/${each.value}"
  output_path = "${path.module}/archived_files/${trimsuffix(trimprefix(each.value, "lambda_functions/"),".py")}.zip"

}

You can combine for_each and fileset for this:

data "archive_file" "from_s3" {

  for_each = fileset("${path.module}", "lambda_functions/*.py")
  type        = "zip"
  source_file = "${path.module}/${each.value}"
  output_path = "${path.module}/archived_files/${trimsuffix(trimprefix(each.value, "lambda_functions/"),".py")}.zip"

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文