使用 'aws lambda invoke' 时在 localstack 上出现 'invalid reference format' 错误
Getting 'invalid reference format' error on localstack when use 'aws lambda invoke'
我正在使用 localstack/terraform/aws(最新版本)在本地的 aws 上使用 lambda。配置可以在这里找到 https://github.com/wentao-daommo/aws-local
虽然我可以成功 setup/deploy 一切并通过 'aws lambda list-functions' 列出我的 lambda 函数。我无法使用命令
调用函数
aws --endpoint-url=http://localhost:4566 lambda invoke --function-name=handler --payload='' test.json
从命令行,我收到错误
{
"StatusCode": 200,
"FunctionError": "Unhandled",
"LogResult": "",
"ExecutedVersion": "$LATEST"
}
然后从localstack看到了这个错误信息,完全看不懂
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.
请帮忙。谢谢!
您的处理程序不正确并且您缺少运行时。我还建议使用以下形式的标准 main.js
:
exports.handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify("Hi from lambda on localstack")
}
};
那么,你的 aws_lambda_function
正确 handler
和 runtime
应该是:
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)
}
我正在使用 localstack/terraform/aws(最新版本)在本地的 aws 上使用 lambda。配置可以在这里找到 https://github.com/wentao-daommo/aws-local
虽然我可以成功 setup/deploy 一切并通过 'aws lambda list-functions' 列出我的 lambda 函数。我无法使用命令
调用函数aws --endpoint-url=http://localhost:4566 lambda invoke --function-name=handler --payload='' test.json
从命令行,我收到错误
{
"StatusCode": 200,
"FunctionError": "Unhandled",
"LogResult": "",
"ExecutedVersion": "$LATEST"
}
然后从localstack看到了这个错误信息,完全看不懂
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.
请帮忙。谢谢!
您的处理程序不正确并且您缺少运行时。我还建议使用以下形式的标准 main.js
:
exports.handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify("Hi from lambda on localstack")
}
};
那么,你的 aws_lambda_function
正确 handler
和 runtime
应该是:
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)
}