Back

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

I deploy lambda using Terraform as follows but have the following questions:

1) I want null_resource.lambda to be called always or when stop_ec2.py is changed so that stop_ec2_upload.zip is not out-of-date. What should I write in triggers{}?

2) how to make aws_lambda_function.stop_ec2 update the new stop_ec2_upload.zip to cloud when stop_ec2_upload.zip is changed?

right now I have to destroy aws_lambda_function.stop_ec2 then create it again. is there anything I can write in the code so that when I run terraform apply, 1) and 2) will happen automatically?

resource "null_resource" "lambda" {

  triggers {

   #what should I write here?

  }

  provisioner "local-exec" {

    command = "mkdir -p lambda_func && cd lambda_py && zip     

../lambda_func/stop_ec2_upload.zip stop_ec2.py && cd .."

  }

}

resource "aws_lambda_function" "stop_ec2" {

    depends_on = ["null_resource.lambda"]

    function_name = "stopEC2"

    handler = "stop_ec2.handler"

    runtime = "python3.6"

    filename = "lambda_func/stop_ec2_upload.zip"

    source_code_hash =     

"${base64sha256(file("lambda_func/stop_ec2_upload.zip"))}"

    role = "..."

}

1 Answer

0 votes
by (44.3k points)

I do not need a trigger when there is “archive_file” and source_code_hash. So when I create a stop_ec2.py file or try to modify it and then run terraform. The file will be automatically re-zipped and then uploaded to the cloud.

data "archive_file" "stop_ec2" {

  type        = "zip"

  source_file = "src_dir/stop_ec2.py"

  output_path = "dest_dir/stop_ec2_upload.zip"

}

resource "aws_lambda_function" "stop_ec2" {

  function_name    = "stopEC2"

  handler          = "stop_ec2.handler"

  runtime          = "python3.6"

  filename         = "dest_dir/stop_ec2_upload.zip"

  source_code_hash =     

"${data.archive_file.stop_ec2.output_base64sha256}"

  role             = "..."

}

Related questions

Want to get 50% Hike on your Salary?

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

0 votes
1 answer

Browse Categories

...