{"id":13522196,"url":"https://github.com/PauloMigAlmeida/aws-lambda-c-runtime","last_synced_at":"2025-03-31T22:30:46.188Z","repository":{"id":78717405,"uuid":"206480485","full_name":"PauloMigAlmeida/aws-lambda-c-runtime","owner":"PauloMigAlmeida","description":"AWS Lambda C Runtime","archived":false,"fork":false,"pushed_at":"2023-06-20T07:01:12.000Z","size":65,"stargazers_count":17,"open_issues_count":4,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-02T06:31:46.267Z","etag":null,"topics":["amazon","aws","aws-lambda","c","custom-runtime"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PauloMigAlmeida.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-05T05:19:36.000Z","updated_at":"2024-09-20T15:56:23.000Z","dependencies_parsed_at":"2024-11-02T06:31:08.317Z","dependency_job_id":"6377a102-0777-45cb-a69a-6182089831f9","html_url":"https://github.com/PauloMigAlmeida/aws-lambda-c-runtime","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PauloMigAlmeida%2Faws-lambda-c-runtime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PauloMigAlmeida%2Faws-lambda-c-runtime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PauloMigAlmeida%2Faws-lambda-c-runtime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PauloMigAlmeida%2Faws-lambda-c-runtime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PauloMigAlmeida","download_url":"https://codeload.github.com/PauloMigAlmeida/aws-lambda-c-runtime/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246552095,"owners_count":20795762,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["amazon","aws","aws-lambda","c","custom-runtime"],"created_at":"2024-08-01T06:00:43.921Z","updated_at":"2025-03-31T22:30:46.183Z","avatar_url":"https://github.com/PauloMigAlmeida.png","language":"C","funding_links":[],"categories":["Layers"],"sub_categories":["Runtimes"],"readme":"[![GitHub](https://img.shields.io/github/license/PauloMigAlmeida/aws-lambda-c-runtime.svg)](https://github.com/PauloMigAlmeida/aws-lambda-c-runtime/blob/master/LICENSE)\n[![Build Status](https://travis-ci.com/PauloMigAlmeida/aws-lambda-c-runtime.svg?branch=master)](https://travis-ci.com/PauloMigAlmeida/aws-lambda-c-runtime)\n\n## AWS Lambda C Runtime\n\nC implementation of the lambda runtime API. \n\n## Disclaimer\nMost implementation details of this project is based on Marco Magdy's nice work on the [awslabs/aws-lambda-cpp](https://github.com/awslabs/aws-lambda-cpp)\nrepository. \n\nDocumentation and packaging mechanisms have been either adapted,ported or copied since I agree with most of the development\n decisions made for the Lambda Cpp runtime. Hence, all the credits should go to the AWS' guys.\n\n## Design Goals\n1. Negligible cold-start overhead (single digit millisecond).\n2. Freedom of choice in compilers, build platforms and C standard library versions.\n\n## Building and Installing the Runtime\nSince AWS Lambda runs on GNU/Linux, you should build this runtime library and your logic on GNU/Linux as well.\n\n### Prerequisites\nMake sure you have the following packages installed first:\n1. CMake (version 3.12 or later)\n1. git\n1. make\n1. zip\n1. libcurl-devel (on Debian-basded distros it's libcurl4-openssl-dev)\n\nIn a terminal, run the following commands:\n```bash\ngit clone https://github.com/PauloMigAlmeida/aws-lambda-c-runtime.git\ncd aws-lambda-c-runtime\nmkdir build\ncd build\n# GCC compiler optimisation breaks the runtime..Only use debug build type\ncmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=~/lambda-install\nmake \u0026\u0026 make install\n```\n\nTo consume this library in a project that is also using CMake, you would do:\n\n```cmake\ncmake_minimum_required(VERSION 3.13)\nproject(demo\n        VERSION 0.0.1\n        LANGUAGES C)\nset(CMAKE_C_STANDARD 11)\nfind_package(aws-lambda-c-runtime)\nadd_executable(${PROJECT_NAME} \"main.c\")\ntarget_link_libraries(${PROJECT_NAME} aws-lambda-c-runtime)\n# this line creates a target that packages your binary and zips it up\naws_lambda_package_target(${PROJECT_NAME})\n```\n\nAnd here is how a sample `main.c` would look like:\n```c\n#include \"aws-lambda/c-runtime/runtime.h\"\n\nvoid my_handler(invocation_request *request, invocation_response **response){\n    if(rand() == 42)\n        *response = failure(\"Hello World from AWS Lambda C Runtime\",\"Application_Error\");\n    else\n        *response = success(\"Hello World from AWS Lambda C Runtime\",\"text/plain\");  \n}\n\nint main(void){\n    run_handler(\u0026my_handler);\n    return 0;\n}\n```\n\nAnd finally, here's how you would package it all. Run the following commands from your application's root directory:\n\n```bash\n$ mkdir build\n$ cd build\n$ cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=~/lambda-install\n$ make\n$ make aws-lambda-package-demo\n```\nThe last command above `make aws-lambda-package-demo` will create a zip file called `demo.zip` in the current directory.\n\nNow, create an IAM role and the Lambda function via the AWS CLI.\n\nFirst create the following trust policy JSON file\n\n```\n$ cat trust-policy.json\n{\n \"Version\": \"2012-10-17\",\n  \"Statement\": [\n    {\n      \"Effect\": \"Allow\",\n      \"Principal\": {\n        \"Service\": [\"lambda.amazonaws.com\"]\n      },\n      \"Action\": \"sts:AssumeRole\"\n    }\n  ]\n}\n\n```\nThen create the IAM role:\n\n```bash\n$ aws iam create-role --role-name lambda-demo --assume-role-policy-document file://trust-policy.json\n```\n\nNote down the role Arn returned to you after running that command. We'll need it in the next steps:\n\nAttach the following policy to allow Lambda to write logs in CloudWatch:\n```bash\n$ aws iam attach-role-policy --role-name lambda-demo --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole\n```\n\nMake sure you attach the appropriate policies and/or permissions for any other AWS services that you plan on using.\n\nAnd finally, create the Lambda function:\n\n```\n$ aws lambda create-function --function-name demo \\\n--role \u003cspecify role arn from previous step here\u003e \\\n--runtime provided --timeout 15 --memory-size 128 \\\n--handler demo --zip-file fileb://demo.zip\n```\n\nAnd to invoke the function:\n```bash\n$ aws lambda invoke --function-name demo --payload '{\"my_cool_payload\":\"yay!\"}' output.txt\n```\n\n## Can I integrate it with the AWS SDK ?\nYes, you can. However, as you might already know AWS doesn't offer a SDK in C language but does offer one in C++. \n\nThere are some examples on how to make this work in the [examples folder of this project](https://github.com/PauloMigAlmeida/aws-lambda-c-runtime/tree/master/examples/) =]\n\n## Supported Compilers\nAny *fully* compliant C11 compiler targeting GNU/Linux x86-64 should work. Please avoid compiler versions that provide half-baked C11 support.\n\n- Use GCC v5.x or above\n- Use Clang v3.3 or above\n\n## Packaging, ABI, GNU C Library, Oh My!\nLambda 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.\n\nHowever, 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.\n\nTo ensure that your application will run correctly on Lambda, we must package the entire C runtime library with your function.\nIf you choose to build on the same [Amazon Linux version used by lambda](https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html), you can avoid packaging the C runtime in your zip file.\nThis can be done by passing the `NO_LIBC` flag in CMake as follows:\n\n```cmake\naws_lambda_package_target(${PROJECT_NAME} NO_LIBC)\n```\n\n## FAQ \u0026 Troubleshooting\n1. **Why is the zip file so large? what are all those files?**\n   Typically, the zip file is large because we have to package the entire C standard library.\n   You can reduce the size by doing some or all of the following:\n   - Ensure you're building in release mode `-DCMAKE_BUILD_TYPE=Release`\n   - 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.\n1. **How to upload a zip file that's bigger than 50MB via the CLI?**\n    Upload your zip file to S3 first:\n    ```bash\n    $ aws s3 cp demo.zip s3://mys3bucket/demo.zip\n    ```\n    NOTE: you must use the same region for your S3 bucket as the lambda.\n\n    Then you can create the Lambda function this way:\n\n    ```bash\n    $ aws lambda create-function --function-name demo \\\n    --role \u003cspecify role arn here\u003e \\\n    --runtime provided --timeout 15 --memory-size 128 \\\n    --handler demo\n    --code \"S3Bucket=mys3bucket,S3Key=demo.zip\"\n    ```\n1. **My code is crashing, how can I debug it?**\n   \n   - Run your code locally on an Amazon Linux AMI or Docker container to reproduce the problem\n     - If you go the AMI route, [use the official one](https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html) recommended by AWS Lambda \n     - If you go the Docker route, use the following command to launch a container running AL2017.03\n       `$ docker run -v /tmp:/tmp -it --security-opt seccomp=unconfined amazonlinux:2017.03`\n       The `security-opt` argument is necessary to run `gdb`, `strace`, etc.\n1. **CURL problem with the SSL CA cert**\n   - Make sure you are using a `libcurl` version built with OpenSSL, or one of its flavors (BoringSSL, LibreSSL)\n   - Make sure you tell `libcurl` where to find the CA bundle file.\n   - You can try hitting the non-TLS version of the endpoint if available. (Not Recommended).\n\n## License\n\nThis library is licensed under the Apache 2.0 License. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPauloMigAlmeida%2Faws-lambda-c-runtime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FPauloMigAlmeida%2Faws-lambda-c-runtime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FPauloMigAlmeida%2Faws-lambda-c-runtime/lists"}