Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Github Relase .zip file for Lambda Function

I am trying to use Terraform to spin up a lambda function that uses source code in a github release package. The location of the package is:

https://github.com/DataDog/datadog-serverless-functions/releases

This will allow me to manually create the AWS DataDog forwarder without using their Cloudformation template (we want to control as much of the process as possible).

I'm not entirely sure how to pull down that zip file for lambda functions to use

resource "aws_lambda_function" "test_lambda" {
  filename      = "lambda_function_payload.zip"
  function_name = "datadog-forwarder"
  role          = aws_iam_role.datadog_forwarder_role.arn

  source_code_hash = filebase64sha256("lambda_function_payload.zip")

  runtime = "python3.7"

  environment {
    variables = {
      DD_API_KEY_SECRET_ARN = aws_secretsmanager_secret_version.dd_api_key.arn
      #This stops the Forwarder from generating enhanced metrics itself, but it will still forward custom metrics from other lambdas.
      DD_ENHANCED_METRICS = false
      DD_S3_BUCKET_NAME = aws_s3_bucket.datadog_forwarder.name
    }
  }
}

I know that the source_code_hash file name will change and the filename of the lambda function will change as well. Any help would be appreciated.

like image 717
sd-gallowaystorm Avatar asked Dec 06 '25 07:12

sd-gallowaystorm


2 Answers

There is no build in functionality to download files from the internet in terraform. But you could relatively easily do that by using external data source. For that you would create a bash script that could use curl to download your zip, open it up, inspect or do any processing you need. The source would also return data that you can use for creation of your function.

Alternative is to use null_resource with local-exec to curl your zip file. But local-exec is less versitile then using the external data source.

like image 173
Marcin Avatar answered Dec 08 '25 20:12

Marcin


There is a way to specify a zip file for an AWS Lambda. Checkout the example configuration in https://github.com/hashicorp/terraform-provider-aws/blob/main/examples/lambda.

It uses a data source of type archive_file

data "archive_file" "zip" {
   type        = "zip"
   source_file = "hello_lambda.py"
   output_path = "hello_lambda.zip"
 }

to set the filename and source_code_hash for the aws_lambda_function resource:

 resource "aws_lambda_function" "lambda" {
   function_name = "hello_lambda"

   filename         = data.archive_file.zip.output_path
   source_code_hash = data.archive_file.zip.output_base64sha256
   .....
 }

See the example files for complete details.

The Terraform AWS provider is calling the CreateFunction API ( https://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html), which allows you to specify a zip file.

like image 28
user3192122 Avatar answered Dec 08 '25 22:12

user3192122