Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger terraform to upload new lambda code

I deploy lambda using Terraform as follows but have 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 = "..."
}
like image 873
user389955 Avatar asked Feb 02 '18 07:02

user389955


People also ask

How do you trigger a Lambda code?

You can invoke Lambda functions directly using the Lambda console, a function URL HTTP(S) endpoint, the Lambda API, an AWS SDK, the AWS Command Line Interface (AWS CLI), and AWS toolkits.

How do I use TerraForm to deploy Lambda function?

To enable TerraForm to deploy Lambda functions, you need to create three . tf files in the \lambda-test\ project folder: iam-lambda.tf – defines two TerraForm resources and assigns the IAM policies to them. provider.tf – defines AWS as a TerraForm provider.

How do I import Lambda into TerraForm?

Open your Lambda in the AWS console, and select Actions > Export Function > Download deployment package. Download the file to the directory containing the Terraform file that you just created with the name lambda. zip . Next, run terraform init and terraform plan .


2 Answers

I read the link provided by Chandan and figured out. Here is my code and it works perfectly.

In fact, with "archive_file", and source_code_hash, I do not need trigger. whenever I create a new file stop_ec2.py or modify it. when I run terraform, the file will be re-zipped and uploaded to 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             = "..."
}
like image 55
user389955 Avatar answered Nov 09 '22 10:11

user389955


These might help:

triggers {
    main         = "${base64sha256(file("source/main.py"))}"
    requirements = "${base64sha256(file("source/requirements.txt"))}"
  }

triggers = {
    source_file = "${sha1Folder("${path.module}/source")}"
  }

REF: https://github.com/hashicorp/terraform/issues/8344

like image 32
Chandan Nayak Avatar answered Nov 09 '22 09:11

Chandan Nayak