Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in AWS by (19.1k points)

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?

1 Answer

0 votes
by (44.4k points)

Use an archive_file, so that when you run “terraform apply”, the file will be automatically re-zipped and uploaded. Check this documentation - https://www.terraform.io/docs/providers/archive/d/archive_file.html 

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"

}

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer

Browse Categories

...