2

我想尝试 AWS Lambda 的自定义 C++ 运行时并使用SAM在本地对其进行测试。不幸的是,我收到了错误Runtime exited without providing a reason(比较下面的错误详细信息)。如何使用 SAM 在本地运行 C++ Lambda 函数?

方法:

我正在遵循官方 C++ 简介博客中描述的确切步骤,直到“创建您的 C++ 函数”的最后一步。博客的其余部分是关于在 Lambda 上部署函数(我不想这样做,因为我想在本地使用 SAM)。

为了使用 SAM,我template.yaml在构建目录中添加了一个。现在的结构build dir如下所示:

├── CMakeCache.txt
├── CMakeFiles
|   |...
├── cmake_install.cmake
├── hello
├── hello.zip
├── Makefile
└── template.yaml

6 directories, 37 files

这是template.yaml构建目录中的内容:

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
  cpp hello world

Globals: # default settings across all resources if nothing else is specified
  Function:
    Timeout: 15

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello.zip # relative location or S3 key
      Handler: hello # function to handle call? Why is this hello and not main?
      Runtime: provided
      Events:
        HelloWorld: # the name of the event
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

Outputs:
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

调用:

sam local start-api --debugdebug文件夹中运行。我通过127.0.0.1:3000/hello在 chrome 中调用该函数。

错误:

调用 URL 产生的消息中的一些详细信息:

...
Invoking hello (provided)
Decompressing /home/path/to/folder/test_cpp_local/aws-lambda-cpp/build/hello_cpp/build/hello.zip

Fetching lambci/lambda:provided Docker container image......
Mounting /tmp/tmpm9djt4mb as /var/task:ro,delegated inside runtime container
/var/task/bin/hello: error while loading shared libraries: /var/task/lib/libc.so.6: file too short
START RequestId: 3435a342-d86d-1a59-df1a-10167070cd22 Version: $LATEST
END RequestId: 3435a342-d86d-1a59-df1a-10167070cd22
REPORT RequestId: 3435a342-d86d-1a59-df1a-10167070cd22  Init Duration: 29.71 ms Duration: 0.00 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 6 MB   
{
  "errorType": "Runtime.ExitError",
  "errorMessage": "RequestId: 3435a342-d86d-1a59-df1a-10167070cd22 Error: Runtime exited without providing a reason"
}
Function returned an invalid response (must include one of: body, headers, multiValueHeaders or statusCode in the response object
...

我的系统:

我正在使用 cmake 3.5.1、g++ 4.5.0、gcc 4.5.0 在 Ubuntu 16.04 上构建

如何解决这个问题的想法:

我必须以某种方式在使用 AWS Linux 的机器上构建远程(我希望不是这种情况)

我可以使用这里推荐的CloudFormationPackage stackoverflow。我想避免这种情况,因为我只想在本地进行测试。

4

2 回答 2

1

问题很可能在这里: /var/task/bin/hello: error while loading shared libraries: /var/task/lib/libc.so.6: file too short

您的二进制文件甚至没有调用。

您应该能够通过在匹配版本上构建二进制文件AWS Linux或通过构建静态二进制文件来解决此问题。

于 2019-10-18T15:53:31.890 回答
1

在 docker 容器内运行

我认为这是 SAM 的问题。我能够以使用docker-lambda构建它的方式做到这一点。为此,我必须提取hello.zip而不是调用它

docker run --rm -v "<path/to/hello>":/var/task lambci/lambda:provided handler

使用此解决方法,我不需要静态构建。

在 docker 容器内构建

您还可以在 amazonlinux 容器中构建。一个hello_worldDockerfile 看起来像这样(包括程序本身):

FROM amazonlinux:latest

RUN yum -y install make 
RUN yum -y install git 
RUN yum -y install gcc-c++
RUN yum -y install nano 
RUN yum -y install zip 
RUN yum -y install clang 
RUN yum -y install gcc-c++
RUN yum -y install libcurl-devel 
RUN yum -y install cmake3
RUN export CC=gcc && \
    export CXX=g++ && \
    cd ~  && \
    git clone https://github.com/awslabs/aws-lambda-cpp.git && \
    cd aws-lambda-cpp && \
    mkdir build && \
    cd build && \
    cmake3 .. -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=~/out && \
    make &&\
    make install && \
    mkdir hello-cpp-world && \
    cd hello-cpp-world && \
    echo -e "// main.cpp\n#include <aws/lambda-runtime/runtime.h>\nusing namespace aws::lambda_runtime;\ninvocation_response my_handler(invocation_request const& request)\n{\n   return invocation_response::success(\"Hello, World!\", \"application/json\");\n}\n\nint main()\n{\nrun_handler(my_handler);\n   return 0; \n}" >> main.cpp && \
    echo -e "cmake_minimum_required(VERSION 3.5)\nset(CMAKE_CXX_STANDARD 11)\nproject(hello LANGUAGES CXX)\nfind_package(aws-lambda-runtime REQUIRED)\nadd_executable(hello \"main.cpp\")\ntarget_link_libraries(hello PUBLIC AWS::aws-lambda-runtime)\naws_lambda_package_target(hello NO_LIBC)" >> CMakeLists.txt && \
    mkdir build &&\
    cd build &&\
    cmake3 .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=~/out && \
    make && \
    make aws-lambda-package-hello
RUN cd / && \
    cp /root/aws-lambda-cpp/build/hello-cpp-world/build/hello.zip /artifacts/hello.zip

重要的是,如果您在 amazonlinux 环境中构建,则必须aws_lambda_package_target(hello NO_LIBC)CMakeLists.txt. 这是为了不将 C 运行时包含为依赖项。

在示例中,我artifacts在容器内安装了一个名为的文件夹,我将生成的 zip 文件复制到该文件夹​​。

于 2019-10-22T11:14:36.340 回答