Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform not uploading Lambda code zip file to AWS

Right now I have the following in my 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"
}

My directory structure is like so:

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

When I run terraform apply and then, in the console, go to the lambda created the code section is empty and it invites me to upload a zip file. How do I make sure the code actually gets uploaded?

like image 218
BWStearns Avatar asked May 15 '18 19:05

BWStearns


1 Answers

You may also try this using archive_file, https://www.terraform.io/docs/providers/archive/d/archive_file.html So that when you run "terraform apply" the file will be re-zipped and uploaded.

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"
}
like image 84
sothish Avatar answered Oct 14 '22 22:10

sothish