Explore Courses Blog Tutorials Interview Questions
0 votes
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 is changed so that is not out-of-date. What should I write in triggers{}?

2) how to make aws_lambda_function.stop_ec2 update the new to cloud when 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/ && 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/"

    source_code_hash =     


    role = "..."


1 Answer

0 votes
by (44.4k points)

I do not need a trigger when there is “archive_file” and source_code_hash. So when I create a 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/"

  output_path = "dest_dir/"


resource "aws_lambda_function" "stop_ec2" {

  function_name    = "stopEC2"

  handler          = "stop_ec2.handler"

  runtime          = "python3.6"

  filename         = "dest_dir/"

  source_code_hash =     


  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