gpt4 book ai didi

terraform - 如何解决这个 "error archiving directory: could not archive missing directory"

转载 作者:行者123 更新时间:2023-12-05 08:26:13 29 4
gpt4 key购买 nike

在我的项目开始时,有两个 terraform 模块:basereusable_module

base/main.tf

# Provide abstraction to define a lambda function
terraform {
required_version = "0.11.7"
}

variable "env" {}
variable "role" {}
variable "function_name" {
default = ""
}
variable "lambda_filename" {}
variable "script_env_vars" {
type = "map"
}

data "archive_file" "package_zip" {
type = "zip"
# There is a bug in Terraform which does not allow '..' in source_dir, thus we use path.root:
# https://github.com/terraform-providers/terraform-provider-archive/issues/5
source_dir = "${path.root}/scripts/" # Path from top level module.
# The output path has to be relative. Otherwise the buildkite will always show a diff.
output_path = "./.terraform/${var.env}-${var.lambda_filename}.zip"
}

resource "aws_lambda_function" "lambda" {
function_name = "${var.function_name}"
description = "Simple function"
role = "${var.role}"

runtime = "python3.6"
timeout = 300 // seconds. Max hard limit is 5 min.

filename = "${data.archive_file.package_zip.output_path}"
// The handler is always the file name + function name "handler".
handler = "${var.lambda_filename}.handler"
source_code_hash = "${data.archive_file.package_zip.output_base64sha256}"

// Environment variables for the script.
environment {
variables = "${var.script_env_vars}"
}
}

reusable_module/main.tf

variable "env" {}
variable "region" {}
variable "function_name" {}

provider "aws" {
region = "${var.region}"
}

resource "aws_iam_role" "mylambda_role" {
name = "${var.env}-mylambda-role"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

locals {
default_function_name = "${var.env}-mylambda"
final_function_name = "${var.function_name != "" ? var.function_name : local.default_function_name}"
}

module "mylambda" {
source = "../base"
lambda_filename = "mylambda"
function_name = "${local.final_function_name}"
env = "${var.env}"
role = "${aws_iam_role.mylambda_role.arn}"
script_env_vars = {
DUMMY = "123"
}
}

模块 mylambda 使用 base/main.tf 创建 lambda 函数。

reusable_module 下,有一个 scripts 目录,所有 python 脚本都存放在这里。

现在我想通过为不同的团队重用和实例化此 reusable_module/main.tf 来扩展我的项目。

team_template/main.tf

variable "env" {}
variable "region" {}
variable "team" {}


module "team_template" {
source = "../reusable_module"
env = "${var.env}"
region = "${var.region}"
function_name = "${var.team}-essential-function"
}

# More resources specific to team_template

team-sales/main.tf

为每个团队创建一个 lambda

variable "env" {}
variable "region" {}

module "realdeal" {
source = "../team_template"
env = "${var.env}"
region = "${var.region}"
team = "sales"
}
# More stuff tailored for each teams

当我在 team-sales 中运行 terraform plan -var-file=dev.tfvars 时,出现了这个错误:

data.archive_file.package_zip: Refreshing state...

Error: Error refreshing state: 1 error(s) occurred:

  • module.realdeal.module.team_template.module.mylambda.data.archive_file.package_zip: 1 error(s) occurred:

  • module.realdeal.module.team_template.module.mylambda.data.archive_file.package_zip: data.archive_file.package_zip: error archiving directory: could not archive missing directory: /Users/antkong/Documents/Personal/wd/StackoverflowCode/terraform/lambda/team/scripts/

问题是 data.archive_file.package_zip 正在 team_template/scripts 中寻找脚本。

但在这种情况下,我实际上在 team_template 中没有 python 代码。我只是想继续使用 reusable_module/script 中的 python 代码。

必须保持文件的分离。 (康威定律等)

我该怎么做?

最佳答案

可能不适用于你的问题,但当我遇到同样的错误时适用于我的。

如果您错误地使用 source_dir 而不是 source_file 作为您的 archive_file,您会得到同样的错误。

例如改变这个:

data "archive_file" "source" {
type = "zip"
source_dir = "${path.module}/lambda_function.py"
output_path = "${path.module}/function.zip"
}

对此:

data "archive_file" "source" {
type = "zip"
source_file = "${path.module}/lambda_function.py"
output_path = "${path.module}/function.zip"
}

关于terraform - 如何解决这个 "error archiving directory: could not archive missing directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57054136/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com