Terraform不会将Lambda代码zip文件上传到AWS

时间:2018-05-15 19:14:13

标签: amazon-web-services aws-lambda terraform

现在我的main.tf

中有以下内容:
resource "aws_lambda_function" "terraform_lambda" {
  filename = "tf_lambda.zip"
  function_name = "tf_lambda"
  role = "lambda_basic_execution"
  handler = "tf_lambda.lambda_handler"
  source_code_hash = "${base64sha256(file("tf_lambda.zip"))}"
  runtime = "python3.6"
}

我的目录结构如下:

.
|-- main.tf
|-- tf_lambda.zip
|-- tf_lambda
    └── tf_lambda.py

当我运行terraform apply然后,在控制台中,转到创建的lambda,代码部分为空,它邀请我上传一个zip文件。如何确保代码实际上传?

1 个答案:

答案 0 :(得分:7)

您也可以使用archive_file https://www.terraform.io/docs/providers/archive/d/archive_file.html尝试此操作 因此,当您运行“terraform apply”时,文件将被重新压缩并上传。

data "archive_file" "zipit" {
  type        = "zip"
  source_file = "tf_lambda/tf_lambda.py"
  output_path = "tf_lambda.zip"
}


resource "aws_lambda_function" "terraform_lambda" {
  function_name = "tf_lambda"
  role = "lambda_basic_execution"
  handler = "tf_lambda.lambda_handler"
  filename = "tf_lambda.zip"
  source_code_hash = "${data.archive_file.zipit.output_base64sha256}"
  runtime = "python3.6"
}