Search code examples
amazon-web-servicesdockeraws-lambdaterraformterraform-provider-aws

InvalidEntrypoint when running containerised lambda function with linux/amd64 base image


I'm on a OSX M1 chip. I'm not sure why I'm getting this error as I think I've built the image correctly with a linux/amd64 base image. I have the following files:

Dockerfile:

FROM --platform=linux/amd64 python:3.12

COPY main.py ${LAMBDA_TASK_ROOT}

CMD ["main.handler"]

In the same directory as my Dockerfile I have a main.py with this:

def handler(event, context):
    print(event)
    return event

I have the following lambda.tf file:

module "my_test_lambda" {

  source        = "terraform-aws-modules/lambda/aws"
  version       = "4.17.0"
  function_name = local.deployment_prefix
  handler       = "main.handler"

  timeout     = 900
  memory_size = 1536

  # use_existing_cloudwatch_log_group       = true

  create_package = false

  package_type  = "Image"
  architectures = ["x86_64"]
  image_uri     = "1234567890.dkr.ecr.eu-west-2.amazonaws.com/my-test-repo:latest"

  role_permissions_boundary = var.role_permissions_boundary_arn

  attach_policies    = true
  number_of_policies = 1

  policies = [
    aws_iam_policy.my-test-lambda.arn
  ]

  tags = {
    Component = local.module_name
  }
}

I'm running the following commands following login to ECR

docker build -t my-test-image .

docker tag my-test-image 1234567890.dkr.ecr.eu-west-2.amazonaws.com/my-test-repo:latest

docker push 1234567890.dkr.ecr.eu-west-2.amazonaws.com/sam-test-repo:latest

Then when I run the lambda via the console I get the following error:

Status: error Error Type: Runtime.InvalidEntrypoint Error: fork/exec /lambda-entrypoint.sh: exec format error Runtime.InvalidEntrypoint

I'm not quite sure what I'm doing wrong here. I've followed other answers in other questions but keep getting this error.


Solution

  • When creating a Lambda function, the handler consists of two parts:

    1. Lambda function name
    2. Handler name from the Lambda function code

    By default, the naming convention is lambda_function.lambda_handler for Python functions. In your case, the Lambda function name is defined with the local.deployment_prefix value, which means the function name has to be used instead of main. The name of the handler function in your code is handler (defined with def handler). In other words, what needs to be changed in terraform is the following:

    module "my_test_lambda" {
    
      source        = "terraform-aws-modules/lambda/aws"
      version       = "4.17.0"
      function_name = local.deployment_prefix
      handler       = "${local.deployment_prefix}.handler"
    
      timeout     = 900
      memory_size = 1536
    
      # use_existing_cloudwatch_log_group       = true
    
      create_package = false
    
      package_type  = "Image"
      architectures = ["x86_64"]
      image_uri     = "1234567890.dkr.ecr.eu-west-2.amazonaws.com/my-test-repo:latest"
    
      role_permissions_boundary = var.role_permissions_boundary_arn
    
      attach_policies    = true
      number_of_policies = 1
    
      policies = [
        aws_iam_policy.my-test-lambda.arn
      ]
    
      tags = {
        Component = local.module_name
      }
    }