Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform execute script before lambda creation

I have a terraform configuration that correctly creates a lambda function on aws with a zip file provided.

My problem is that I always have to package the lambda first (I use serverless package method for this), so I would like to execute a script that package my function and move the zip to the right directory before terraform creates the lambda function.

Is that possible? Maybe using a combination of null_resource and local-exec?

like image 650
4 revs, 4 users 59% Avatar asked Sep 20 '18 09:09

4 revs, 4 users 59%


People also ask

How do you trigger Lambda in TerraForm?

You can use an aws_cloudwatch_event_target resource to tie the scheduled event source (event rule) to your lambda function. You need to grant it permission to invoke your lambda function; you can use an aws_lambda_permission resource for this.

Can I run shell script in AWS Lambda?

AWS recently announced the "Lambda Runtime API and Lambda Layers", two new features that enable developers to build custom runtimes. So, it's now possibile to directly run even bash scripts in Lambda without hacks. This actually opens up the possibility to run any programming language within a Lambda.

How do I invoke AWS Lambda directly?

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 make Lambda execution faster?

If we need to reduce the Lambda execution time, we can try increasing memory (and by extension, CPU) to process it faster. However, when we try to increase the memory for a function past a certain limit, it won't improve the execution time as AWS currently offers a maximum of 2 cores CPU.


2 Answers

The answer is already given, but I was looking for a way to install NPM modules on the fly, zip and then deploy Lambda function along with timeout if your lambda function size is large. So here is my finding may help someone else.

#Install NPM module before creating ZIP

resource "null_resource" "npm" {
  provisioner "local-exec" {
    command = "cd ../lambda-functions/loadbalancer-to-es/ && npm install --prod=only"
  }
}

# Zip the Lamda function on the fly
data "archive_file" "source" {
  type        = "zip"
  source_dir  = "../lambda-functions/loadbalancer-to-es"
  output_path = "../lambda-functions/loadbalancer-to-es.zip"
  depends_on  = ["null_resource.npm"]
}


# Created AWS Lamdba Function: Memory Size, NodeJS version, handler, endpoint, doctype and environment settings
resource "aws_lambda_function" "elb_logs_to_elasticsearch" {
  filename      = "${data.archive_file.source.output_path}"
  function_name = "someprefix-alb-logs-to-elk"
  description   = "elb-logs-to-elasticsearch"
  memory_size   = 1024
  timeout       = 900
  timeouts {
  create = "30m"
  }
  runtime          = "nodejs8.10"
  role             = "${aws_iam_role.role.arn}"
  source_code_hash = "${base64sha256(data.archive_file.source.output_path)}"
  handler          = "index.handler"
  #  source_code_hash = "${base64sha256(file("/elb-logs-to-elasticsearch.zip"))}"


  environment {
    variables = {
      ELK_ENDPOINT = "someprefix-elk.dns.co"
      ELK_INDEX    = "test-web-server-"
      ELK_REGION   = "us-west-2"
      ELK_DOCKTYPE = "elb-access-logs"
    }
  }
}
like image 20
Adiii Avatar answered Oct 21 '22 02:10

Adiii


You already proposed the best answer :)

When you add a depends_on = ["null_resource.serverless_execution"] to your lambda resource, you can ensure, that packaging will be done before uploading the zip file.

Example:

resource "null_resource" "serverless_execution" {
  provisioner "local-exec" {
    command = "serverless package ..."
  }
}

resource "aws_lambda_function" "update_lambda" {
  depends_on = ["null_resource.serverless_execution"]
  filename   = "${path.module}/path/to/package.zip"
  [...]
}

https://www.terraform.io/docs/provisioners/local-exec.html

like image 65
DJAlPee Avatar answered Oct 21 '22 03:10

DJAlPee