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.

preguntada hace un año895 visualizaciones
1 Respuesta
4
Respuesta aceptada

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
EXPERTO
respondido hace un año
  • 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

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas