Terraform的相对路径



我正在尝试使用Terraform创建AWS lambda函数。我的Terraform目录看起来像

  • Terraform
    • iam-Policies
      • main.tf
    • lambda
      • 文件/
      • main.tf
    • main.tf

我的lambda函数存储在/terraform/lambda/files/lambda_function.py

每当我使用Terraform时,我都会有一个" null_resource",该" null_resource"在本地计算机中执行某些命令,该命令将sip python文件

variable "pythonfile" {
  description = "lambda function python filename"
  type        = "string"
}
resource "null_resource" "lambda_preconditions" {
  triggers {
    always_run = "${uuid()}"
  }
  provisioner "local-exec" {
    command = "rm -rf ${path.module}/files/zips"
  }
  provisioner "local-exec" {
    command = "mkdir -p ${path.module}/files/zips"
  }
  provisioner "local-exec" {
    command = "cp -R ${path.module}/files/${var.pythonfile} ${path.module}/files/zips/lambda_function.py"
  }
  provisioner "local-exec" {
    command = "cd ${path.module}/files/zips && zip -r lambda.zip ."
  }
}

我的" aws_lambda_function"资源看起来像这样。

resource "aws_lambda_function" "lambda_function" {
  filename         = "${path.module}/files/zips/lambda.zip"
  function_name    = "${format("%s-%s-%s-lambda-function", var.name, var.environment, var.function_name)}"
  role             = "${aws_iam_role.iam_for_lambda.arn}"
  handler          = "lambda_function.lambda_handler"
  source_code_hash = "${base64sha256(format("%s/files/zips/lambda.zip", path.module))}", length(path.cwd) + 1, -1)}")}"
  runtime          = "${var.function_runtime}"
  timeout          = "${var.function_timeout}"
  memory_size      = "${var.function_memory}"
  environment {
    variables = {
      region      = "${var.region}"
      name        = "${var.name}"
      environment = "${var.environment}"
    }
  }
  vpc_config {
    subnet_ids         = ["${var.subnet_ids}"]
    security_group_ids = ["${aws_security_group.lambda_sg.id}"]
  }
  depends_on = [
    "null_resource.lambda_preconditions"
  ]
}

问题:每当我更改lambda_function.py文件并再次应用Terraform时,一切正常,但是Lambda函数中的实际代码不会更改。另外,如果我删除所有Terraform状态文件并再次申请,则新更改将毫无问题地传播。

可能是什么原因?

而不是使用null_resource,我使用的是archive_file数据源,该数据源在检测到新更改时会自动创建zip文件。接下来,我从lambda资源source_code_hash属性中的Archive_file数据中获取了参考。

Archive_file数据源

data "archive_file" "lambda_zip" {
  type        = "zip"
  output_path = "${path.module}/files/zips/lambda.zip"
  source {
    content  = "${file("${path.module}/files/ebs_cleanup_lambda.py")}"
    filename = "lambda_function.py"
  }
}

lambda资源

resource "aws_lambda_function" "lambda_function" {
  filename         = "${path.module}/files/zips/lambda.zip"
  function_name    = "${format("%s-%s-%s-lambda-function", var.name, var.environment, var.function_name)}"
  role             = "${aws_iam_role.iam_for_lambda.arn}"
  handler          = "lambda_function.lambda_handler"
  source_code_hash = "${data.archive_file.lambda_zip.output_base64sha256}"
  runtime          = "${var.function_runtime}"
  timeout          = "${var.function_timeout}"
  memory_size      = "${var.function_memory}"
  environment {
    variables = {
      region      = "${var.region}"
      name        = "${var.name}"
      environment = "${var.environment}"
    }
  }
  vpc_config {
    subnet_ids         = ["${var.subnet_ids}"]
    security_group_ids = ["${aws_security_group.lambda_sg.id}"]
  }
}

相关内容

  • 没有找到相关文章

最新更新