https://github.com/awslabs/aws-lambda-cpp

C++ implementation of the AWS Lambda runtime

https://github.com/awslabs/aws-lambda-cpp

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (3.0%) to scientific vocabulary

Keywords

aws aws-lambda cpp cpp11 cpp14 cpp17 lambda
Last synced: 5 months ago · JSON representation

Repository

C++ implementation of the AWS Lambda runtime

Basic Info
  • Host: GitHub
  • Owner: awslabs
  • License: apache-2.0
  • Language: C++
  • Default Branch: master
  • Homepage:
  • Size: 530 KB
Statistics
  • Stars: 456
  • Watchers: 19
  • Forks: 95
  • Open Issues: 38
  • Releases: 12
Topics
aws aws-lambda cpp cpp11 cpp14 cpp17 lambda
Created over 7 years ago · Last pushed 7 months ago
Metadata Files
Readme Contributing License Code of conduct

README.md

GitHub Code Quality badge

| OS | Arch | Status | |----|------|--------| | Amazon Linux 2 | x8664 | | | Amazon Linux 2 | aarch64 | | | Amazon Linux (ALAMI) | x8664 | | | Alpine | x8664 | | | Arch Linux | x8664 | | | Ubuntu 18.04 | x86_64 | |

AWS Lambda C++ Runtime

C++ implementation of the lambda runtime API

Design Goals

  1. Negligible cold-start overhead (single digit millisecond).
  2. Freedom of choice in compilers, build platforms and C standard library versions.

Building and Installing the Runtime

Since AWS Lambda runs on GNU/Linux, you should build this runtime library and your logic on GNU/Linux as well.

Prerequisites

Make sure you have the following packages installed first: 1. CMake (version 3.9 or later) 1. git 1. Make or Ninja 1. zip 1. libcurl-devel (on Debian-based distros it's libcurl4-openssl-dev)

In a terminal, run the following commands: bash $ git clone https://github.com/awslabs/aws-lambda-cpp.git $ cd aws-lambda-cpp $ mkdir build $ cd build $ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=~/lambda-install $ make && make install

Running Unit Tests Locally

To run the unit tests locally, follow these steps to build:

bash $ cd aws-lambda-cpp $ mkdir build $ cd build $ cmake .. -DCMAKE_BUILD_TYPE=Debug -DENABLE_TESTS=ON $ make

Run unit tests: bash $ ctest

To consume this library in a project that is also using CMake, you would do:

```cmake cmakeminimumrequired(VERSION 3.9) set(CMAKECXXSTANDARD 11) project(demo LANGUAGES CXX) findpackage(aws-lambda-runtime) addexecutable(${PROJECTNAME} "main.cpp") targetlinklibraries(${PROJECTNAME} PRIVATE AWS::aws-lambda-runtime) targetcompilefeatures(${PROJECTNAME} PRIVATE "cxxstd11") targetcompileoptions(${PROJECTNAME} PRIVATE "-Wall" "-Wextra")

this line creates a target that packages your binary and zips it up

awslambdapackagetarget(${PROJECTNAME}) ```

And here is how a sample main.cpp would look like: ```cpp

include

using namespace aws::lambda_runtime;

static invocationresponse myhandler(invocationrequest const& req) { if (req.payload.length() > 42) { return invocationresponse::failure("error message here"/error_message/, "error type here" /error_type/); }

return invocation_response::success("{\"message:\":\"I fail if body length is bigger than 42!\"}" /*payload*/,
                                    "application/json" /*MIME type*/);

}

int main() { runhandler(myhandler); return 0; } ```

And finally, here's how you would package it all. Run the following commands from your application's root directory:

bash $ mkdir build $ cd build $ cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=~/lambda-install $ make $ make aws-lambda-package-demo The last command above make aws-lambda-package-demo will create a zip file called demo.zip in the current directory.

Now, create an IAM role and the Lambda function via the AWS CLI.

First create the following trust policy JSON file

``` $ cat trust-policy.json { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": ["lambda.amazonaws.com"] }, "Action": "sts:AssumeRole" } ] }

``` Then create the IAM role:

bash $ aws iam create-role --role-name lambda-demo --assume-role-policy-document file://trust-policy.json

Note down the role Arn returned to you after running that command. We'll need it in the next steps:

Attach the following policy to allow Lambda to write logs in CloudWatch: bash $ aws iam attach-role-policy --role-name lambda-demo --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

Make sure you attach the appropriate policies and/or permissions for any other AWS services that you plan on using.

And finally, create the Lambda function:

$ aws lambda create-function --function-name demo \ --role <specify role arn from previous step here> \ --runtime provided.al2023 --timeout 15 --memory-size 128 \ --handler demo --zip-file fileb://demo.zip

N.B. If you are building on arm64, you have to explicitly add the param --architectures arm64, so that you are setting up the proper architecture on AWS to run your supplied Lambda function.

And to invoke the function: bash $ aws lambda invoke --function-name demo --cli-binary-format raw-in-base64-out --payload '{"answer":42}' output.txt

You can update your supplied function: bash $ aws lambda update-function-code --function-name demo --zip-file fileb://demo.zip

Using the C++ SDK for AWS with this runtime

This library is completely independent from the AWS C++ SDK. You should treat the AWS C++ SDK as just another dependency in your application. See the examples section for a demo utilizing the AWS C++ SDK with this Lambda runtime.

Supported Compilers

Any fully compliant C++11 compiler targeting GNU/Linux x86-64 should work. Please avoid compiler versions that provide half-baked C++11 support.

  • Use GCC v5.x or above
  • Use Clang v3.3 or above

Packaging, ABI, GNU C Library, Oh My!

Lambda runs your code on some version of Amazon Linux. It would be a less than ideal customer experience if you are forced to build your application on that platform and that platform only.

However, the freedom to build on any linux distro brings a challenge. The GNU C Library ABI. There is no guarantee the platform used to build the Lambda function has the same GLIBC version as the one used by AWS Lambda. In fact, you might not even be using GNU's implementation. For example you could build a C++ Lambda function using musl libc.

To ensure that your application will run correctly on Lambda, we must package the entire C runtime library with your function. If you choose to build on the same Amazon Linux version used by lambda, you can avoid packaging the C runtime in your zip file. This can be done by passing the NO_LIBC flag in CMake as follows:

cmake aws_lambda_package_target(${PROJECT_NAME} NO_LIBC)

Common Pitfalls with Packaging

  • Any library dependency your Lambda function has that is dynamically loaded via dlopen will NOT be automatically packaged. You must add those dependencies manually to the zip file. This applies to any configuration or resource files that your code depends on.

  • If you are making HTTP calls over TLS (https), keep in mind that the CA bundle location is different between distros. For example, if you are using the AWS C++ SDK, it's best to set the following configuration options:

cpp Aws::Client::ClientConfiguration config; config.caFile = "/etc/pki/tls/certs/ca-bundle.crt"; If you are not using the AWS C++ SDK, but happen to be using libcurl directly, you can set the CA bundle location by doing: c curl_easy_setopt(curl_handle, CURLOPT_CAINFO, "/etc/pki/tls/certs/ca-bundle.crt");

FAQ & Troubleshooting

  1. Why is the zip file so large? what are all those files? Typically, the zip file is large because we have to package the entire C standard library. You can reduce the size by doing some or all of the following:
    • Ensure you're building in release mode -DCMAKE_BUILD_TYPE=Release
    • If possible, build your function using musl libc, it's tiny. The easiest way to do this, assuming your code is portable, is to build on Alpine linux, which uses musl libc by default.
  2. How to upload a zip file that's bigger than 50MB via the CLI? Upload your zip file to S3 first: bash $ aws s3 cp demo.zip s3://mys3bucket/demo.zip NOTE: you must use the same region for your S3 bucket as the lambda.

    Then you can create the Lambda function this way:

    bash $ aws lambda create-function --function-name demo \ --role <specify role arn here> \ --runtime provided.al2023 --timeout 15 --memory-size 128 \ --handler demo --code "S3Bucket=mys3bucket,S3Key=demo.zip"

    N.B. See hint above if you are building on arm64.

  3. My code is crashing, how can I debug it?

  • Starting with v0.2.0 you should see a stack-trace of the crash site in the logs (which are typically stored in CloudWatch).
    • To get a more detailed stack-trace with source-code information such as line numbers, file names, etc. you need to install one of the following packages:
      • On Debian-based systems - sudo apt install libdw-dev or sudo apt install binutils-dev
      • On RHEL based systems - sudo yum install elfutils-devel or sudo yum install binutils-devel If you have either of those packages installed, CMake will detect them and automatically link to them. No other steps are required.
  • Turn up the logging verbosity to the maximum.
    • Build the runtime in Debug mode. -DCMAKE_BUILD_TYPE=Debug. Verbose logs are enabled by default in Debug builds.
    • To enable verbose logs in Release builds, build the runtime with the following CMake flag -DLOG_VERBOSITY=3
    • If you are using the AWS C++ SDK, see this FAQ on how to adjust its logging verbosity
  • Run your code locally on an Amazon Linux AMI or Docker container to reproduce the problem
    • If you go the AMI route, use the official one recommended by AWS Lambda
    • If you go the Docker route, use the following command to launch a container running AL2017.03 $ docker run -v /tmp:/tmp -it --security-opt seccomp=unconfined amazonlinux:2017.03 The security-opt argument is necessary to run gdb, strace, etc.
      1. CURL problem with the SSL CA cert
  • Make sure you are using a libcurl version built with OpenSSL, or one of its flavors (BoringSSL, LibreSSL)
  • Make sure you tell libcurl where to find the CA bundle file.
  • You can try hitting the non-TLS version of the endpoint if available. (Not Recommended).
    1. No known conversion between std::string and Aws::String
    2. Either turn off custom memory management in the AWS C++ SDK or build it as a static library (-DBUILD_SHARED_LIBS=OFF)

License

This library is licensed under the Apache 2.0 License.

Owner

  • Name: Amazon Web Services - Labs
  • Login: awslabs
  • Kind: organization
  • Location: Seattle, WA

AWS Labs

GitHub Events

Total
  • Issues event: 3
  • Watch event: 14
  • Issue comment event: 4
  • Push event: 39
  • Pull request review event: 6
  • Pull request event: 5
  • Fork event: 7
  • Create event: 1
Last Year
  • Issues event: 3
  • Watch event: 14
  • Issue comment event: 4
  • Push event: 39
  • Pull request review event: 6
  • Pull request event: 5
  • Fork event: 7
  • Create event: 1

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 143
  • Total Committers: 26
  • Avg Commits per committer: 5.5
  • Development Distribution Score (DDS): 0.462
Past Year
  • Commits: 3
  • Committers: 3
  • Avg Commits per committer: 1.0
  • Development Distribution Score (DDS): 0.667
Top Committers
Name Email Commits
Marco Magdy m****y@g****m 77
Bryan Moffatt b****t 24
James Siri j****i@a****m 7
Ildar Sagdejev i****v@p****m 5
Krzysiek Karbowiak k****k@i****l 4
Thomas Bodner t****r@h****e 3
Markus Rothe m****e@r****c 2
Jake Stoeffler j****e@2****m 2
Hugo Benício h****o@g****m 2
Ștefan Toma 3****3 1
nlewycky n****s@m****a 1
koki watanabe 5****o 1
Rawiri Blundell r****l 1
Adam Hunter a****r@g****m 1
Adam Monsen h****t@g****m 1
Amir Szekely k****k@g****m 1
Bradley Bottomlee b****m@g****m 1
Christian Ulbrich c****h@z****e 1
Jaehyun Lyu n****n@n****t 1
Keshav Yadav 7****d 1
Mart Haarman m****n@g****m 1
Maxi Mittelhammer m****l@o****e 1
Maxime David m****y@a****m 1
Michael Hart m****u@g****m 1
Mohamed-Ikbel Boulabiar b****r@g****m 1
Ovidijus Okinskas o****1@l****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 61
  • Total pull requests: 56
  • Average time to close issues: about 2 months
  • Average time to close pull requests: 18 days
  • Total issue authors: 50
  • Total pull request authors: 18
  • Average comments per issue: 3.67
  • Average comments per pull request: 0.34
  • Merged pull requests: 51
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 5
  • Pull requests: 4
  • Average time to close issues: N/A
  • Average time to close pull requests: 9 months
  • Issue authors: 5
  • Pull request authors: 4
  • Average comments per issue: 0.2
  • Average comments per pull request: 1.5
  • Merged pull requests: 2
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • webbnh (4)
  • boulabiar (3)
  • jwang01 (3)
  • NatxoM (3)
  • erksch (2)
  • bowie7070 (2)
  • cmil (1)
  • raltboumflir (1)
  • dendisuhubdy (1)
  • instw (1)
  • phadej (1)
  • ashu0992sharma (1)
  • jmanno1 (1)
  • VikingExplorer (1)
  • bmacphee (1)
Pull Request Authors
  • marcomagdy (29)
  • bmoffatt (7)
  • tobodner (4)
  • maxday (3)
  • math-hiyoko (2)
  • ghost (2)
  • ChristianUlbrich (2)
  • ramisa2108 (2)
  • JakeStoeffler (2)
  • Smruti009 (1)
  • boulabiar (1)
  • meonkeys (1)
  • kichik (1)
  • AlbertXingZhang (1)
  • adamrhunter (1)
Top Labels
Issue Labels
question (6) bug (5) libcurl (2) documentation (2) wontfix (1)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads: unknown
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 11
proxy.golang.org: github.com/awslabs/aws-lambda-cpp
  • Versions: 11
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.5%
Average: 5.6%
Dependent repos count: 5.8%
Last synced: 6 months ago

Dependencies

.github/workflows/code-quality.yml actions
  • actions/checkout v3 composite
  • github/codeql-action/analyze v2 composite
  • github/codeql-action/init v2 composite
.github/workflows/workflow.yml actions
  • actions/checkout v3 composite
  • uraimo/run-on-arch-action v2 composite
examples/Dockerfile docker
  • alpine latest build