create lambda using terraform in a separate repo

0

I need to create a lambda function without putting any source, so that terraform script will just create a lambda. This will be in infra repo you can say that where I am just provisioning the services. Then in another repo which will be used by developers where we will have a application code which needs to be deployed in that lambda. So when I am creating the lambda using terraform in Infra repo, then source_path seems mandatory... please suggest how I can achieve this.

asked a year ago877 views
1 Answer
4
Accepted Answer

you can use the filename parameter to specify a dummy source file for the function

like the following

resource "aws_lambda_function" "my_lambda" {
  function_name = "my-lambda-function"
  role         = aws_iam_role.lambda_role.arn
  handler      = "index.handler"
  runtime      = "nodejs14.x"
  filename     = "/dev/null"
  source_code_hash = "0" # Required to bypass source_code_hash validation when no source code is specified
}

Once you've created the Lambda function in your infrastructure repository, you can then reference it in your application repository using the function's ARN. You can use a data source to look up the ARN by function name:

data "aws_lambda_function" "my_lambda" {
  function_name = "my-lambda-function"
}

resource "aws_lambda_alias" "my_alias" {
  name             = "my-lambda-alias"
  function_name    = data.aws_lambda_function.my_lambda.arn
  function_version = "$LATEST"
}

hope this helps to you

profile picture
EXPERT
answered a year ago
  • Hi, I have tried this and it works if we do terraform plan, but its not working when applying it

    │ Error: creating Lambda Function (): operation error Lambda: CreateFunction, https response error StatusCode: 400, RequestID: 00000000-0000-0000-0000-000000000, InvalidParameterValueException: Uploaded file must be a non-empty zip

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions