在我的项目开始时,有两个 terraform 模块:base和reusable_module。
基础/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}"
}
}
Run Code Online (Sandbox Code Playgroud)
可重用模块/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"
}
}
Run Code Online (Sandbox Code Playgroud)
模块mylambda用于base/main.tf创建 lambda 函数。
在 下reusable_module,有一个scripts目录,其中包含所有 python 脚本。
现在我想通过reusable_module/main.tf为不同的团队重用和实例化它来扩展我的项目。
团队模板/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
Run Code Online (Sandbox Code Playgroud)
团队销售/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
Run Code Online (Sandbox Code Playgroud)
当我terraform plan -var-file=dev.tfvars在团队销售中运行时,出现以下错误:
data.archive_file.package_zip:刷新状态...
错误:刷新状态时出错:发生 1 个错误:
module.realdeal.module.team_template.module.mylambda.data.archive_file.package_zip:发生 1 个错误:
module.realdeal.module.team_template.module.mylambda.data.archive_file.package_zip:data.archive_file.package_zip:错误归档目录:无法归档丢失的目录:/Users/antkong/Documents/Personal/wd/StackoverflowCode/terraform/lambda /团队/脚本/
问题是data.archive_file.package_zip正在寻找team_template/scripts.
但在这种情况下,我实际上没有 python 代码team_template。我只是想继续使用Python代码reusable_module/script。
必须保持文件的分离。(康威定律等)
我该怎么做?
Pau*_*aul 10
也许不适用于您的问题,但当我遇到相同的错误时适用于我的问题。
如果你错误地使用source_dir而不是source_filefor your archive_file,你会得到同样的错误。
例如改变这个:
data "archive_file" "source" {
type = "zip"
source_dir = "${path.module}/lambda_function.py"
output_path = "${path.module}/function.zip"
}
Run Code Online (Sandbox Code Playgroud)
对此:
data "archive_file" "source" {
type = "zip"
source_file = "${path.module}/lambda_function.py"
output_path = "${path.module}/function.zip"
}
Run Code Online (Sandbox Code Playgroud)
您可以考虑将 base 和 reusable_module 合并到一个模块,并使用path.module变量来获取模块路径,因为默认情况下 terraform 解析相对于当前工作目录的路径。
所以在你的情况下:
source_dir = "${path.module}/scripts/" # Path from top level module.
编辑:
如果代码无法合并,您可以将path.module作为变量从reusable_module传递到base,然后使用变量而不是path.root
| 归档时间: |
|
| 查看次数: |
15684 次 |
| 最近记录: |