1
votes

I'm using localstack/terraform/aws (latest versions) to play with lambda on aws locally. The configuration can be found here https://github.com/wentao-daommo/aws-local

While I can successfully setup/deploy everything and list my lambda function via 'aws lambda list-functions'. I was unable to invoke the function with command

aws --endpoint-url=http://localhost:4566 lambda invoke --function-name=handler --payload='' test.json

From the command line, I got error

{
    "StatusCode": 200,
    "FunctionError": "Unhandled",
    "LogResult": "",
    "ExecutedVersion": "$LATEST"
}

and from localstack, I saw this error message, which I don't understand at all

localstack_1  | ERROR: 'docker create --rm --name "localstack_lambda_arn_aws_lambda_us-east-1_000000000000_function_handler" --entrypoint /bin/bash  --interactive -e AWS_LAMBDA_EVENT_BODY="$AWS_LAMBDA_EVENT_BODY" -e HOSTNAME="$HOSTNAME" -e LOCALSTACK_HOSTNAME="$LOCALSTACK_HOSTNAME" -e EDGE_PORT="$EDGE_PORT"  -e AWS_LAMBDA_EVENT_BODY='{}' -e LOCALSTACK_HOSTNAME=192.168.65.2 -e EDGE_PORT=4566 -e _HANDLER=exports.handler -e AWS_LAMBDA_FUNCTION_TIMEOUT=3 -e AWS_LAMBDA_FUNCTION_NAME=handler -e AWS_LAMBDA_FUNCTION_VERSION='$LATEST' -e AWS_LAMBDA_FUNCTION_INVOKED_ARN=arn:aws:lambda:us-east-1:000000000000:function:handler -e AWS_LAMBDA_COGNITO_IDENTITY='{}' -e _LAMBDA_SERVER_PORT=5002     "lambci/lambda:"': exit code 1; output: b'invalid reference format\n'
localstack_1  | 2021-01-26T04:08:07:INFO:localstack.services.awslambda.lambda_api: Error executing Lambda function arn:aws:lambda:us-east-1:000000000000:function:handler: Command 'docker create --rm --name "localstack_lambda_arn_aws_lambda_us-east-1_000000000000_function_handler" --entrypoint /bin/bash  --interactive -e AWS_LAMBDA_EVENT_BODY="$AWS_LAMBDA_EVENT_BODY" -e HOSTNAME="$HOSTNAME" -e LOCALSTACK_HOSTNAME="$LOCALSTACK_HOSTNAME" -e EDGE_PORT="$EDGE_PORT"  -e AWS_LAMBDA_EVENT_BODY='{}' -e LOCALSTACK_HOSTNAME=192.168.65.2 -e EDGE_PORT=4566 -e _HANDLER=exports.handler -e AWS_LAMBDA_FUNCTION_TIMEOUT=3 -e AWS_LAMBDA_FUNCTION_NAME=handler -e AWS_LAMBDA_FUNCTION_VERSION='$LATEST' -e AWS_LAMBDA_FUNCTION_INVOKED_ARN=arn:aws:lambda:us-east-1:000000000000:function:handler -e AWS_LAMBDA_COGNITO_IDENTITY='{}' -e _LAMBDA_SERVER_PORT=5002     "lambci/lambda:"' returned non-zero exit status 1. Traceback (most recent call last):
localstack_1  |   File "/opt/code/localstack/localstack/services/awslambda/lambda_api.py", line 550, in run_lambda
localstack_1  |     result = LAMBDA_EXECUTOR.execute(func_arn, func_details, event, context=context,
localstack_1  |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 178, in execute
localstack_1  |     return do_execute()
localstack_1  |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 170, in do_execute
localstack_1  |     return _run(func_arn=func_arn)
localstack_1  |   File "/opt/code/localstack/localstack/utils/cloudwatch/cloudwatch_util.py", line 149, in wrapped
localstack_1  |     raise e
localstack_1  |   File "/opt/code/localstack/localstack/utils/cloudwatch/cloudwatch_util.py", line 145, in wrapped
localstack_1  |     result = func(*args, **kwargs)
localstack_1  |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 161, in _run
localstack_1  |     raise e
localstack_1  |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 149, in _run
localstack_1  |     result = self._execute(func_arn, func_details, event, context, version)
localstack_1  |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 399, in _execute
localstack_1  |     return super(LambdaExecutorReuseContainers, self)._execute(func_arn, *args, **kwargs)
localstack_1  |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 323, in _execute
localstack_1  |     cmd = self.prepare_execution(func_details, environment, command)
localstack_1  |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 366, in prepare_execution
localstack_1  |     container_info = self.prime_docker_container(func_details, env_vars.items(), lambda_cwd)
localstack_1  |   File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 474, in prime_docker_container
localstack_1  |     run(cmd)
localstack_1  |   File "/opt/code/localstack/localstack/utils/common.py", line 1312, in run
localstack_1  |     return do_run(cmd)
localstack_1  |   File "/opt/code/localstack/localstack/utils/common.py", line 1309, in do_run
localstack_1  |     return bootstrap.run(cmd, **kwargs)
localstack_1  |   File "/opt/code/localstack/localstack/utils/bootstrap.py", line 656, in run
localstack_1  |     raise e
localstack_1  |   File "/opt/code/localstack/localstack/utils/bootstrap.py", line 616, in run
localstack_1  |     output = subprocess.check_output(cmd, shell=True, stderr=stderr, env=env_dict, cwd=cwd)
localstack_1  |   File "/usr/lib/python3.8/subprocess.py", line 411, in check_output
localstack_1  |     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
localstack_1  |   File "/usr/lib/python3.8/subprocess.py", line 512, in run
localstack_1  |     raise CalledProcessError(retcode, process.args,
localstack_1  | subprocess.CalledProcessError: Command 'docker create --rm --name "localstack_lambda_arn_aws_lambda_us-east-1_000000000000_function_handler" --entrypoint /bin/bash  --interactive -e AWS_LAMBDA_EVENT_BODY="$AWS_LAMBDA_EVENT_BODY" -e HOSTNAME="$HOSTNAME" -e LOCALSTACK_HOSTNAME="$LOCALSTACK_HOSTNAME" -e EDGE_PORT="$EDGE_PORT"  -e AWS_LAMBDA_EVENT_BODY='{}' -e LOCALSTACK_HOSTNAME=192.168.65.2 -e EDGE_PORT=4566 -e _HANDLER=exports.handler -e AWS_LAMBDA_FUNCTION_TIMEOUT=3 -e AWS_LAMBDA_FUNCTION_NAME=handler -e AWS_LAMBDA_FUNCTION_VERSION='$LATEST' -e AWS_LAMBDA_FUNCTION_INVOKED_ARN=arn:aws:lambda:us-east-1:000000000000:function:handler -e AWS_LAMBDA_COGNITO_IDENTITY='{}' -e _LAMBDA_SERVER_PORT=5002     "lambci/lambda:"' returned non-zero exit status 1.

Please help. Thanks!

1
Can you please confirm that this is version 0.12.5 or again old 0.10.5?Marcin
It is the 0.12.5 version, as I'm using port 4566 everywhereWentao Zheng
That's good to know. Thanks. You can have a look at the answer provided. If it is useful, its acceptance would be appreciated.Marcin

1 Answers

0
votes

Your handler is incorrect and you are missing runtime. I would also recommend using standard main.js in the form of:

exports.handler = async (event) => {
    return {
        statusCode: 200,
        body: JSON.stringify("Hi from lambda on localstack")
    }
};

Then, your aws_lambda_function with correct handler and runtime should be:

resource "aws_lambda_function" "lambda" {
  filename      = "lambda_file.zip"
  function_name = "handler"
  runtime       = "nodejs12.x"
  role          = aws_iam_role.iam_for_lambda.arn
  handler       = "main.handler"
  source_code_hash = filebase64sha256(data.archive_file.lambda_file.output_path)
}