AWS Compute Blog
Introducing the C++ Lambda Runtime
This post is courtesy of Marco Magdy, AWS Software Development Engineer – AWS SDKs and Tools
Today, AWS Lambda announced the availability of the Runtime API. The Runtime API allows you to write your Lambda functions in any language, provided that you bundle it with your application artifact or as a Lambda layer that your application uses.
As an example of using this API and based on the customer demand, AWS is releasing a reference implementation of a C++ runtime for Lambda. This C++ runtime brings the simplicity and expressiveness of interpreted languages while maintaining the superiority of C++ performance and low memory footprint. These are benefits that align well with the event-driven, function-based, development model of Lambda applications.
Hello World
Start by writing a Hello World Lambda function in C++ using this runtime.
Prerequisites
You need a Linux-based environment (I recommend Amazon Linux. You can find the Amazon Linux AMI that Lambda uses in Lambda Execution Environment and Available Libraries, Note: Amazon Linux 2 is not yet supported for Runtime API), with the following packages installed:
- A C++11 compiler, either GCC 5.x or later or Clang 3.3 or later. On Amazon Linux, run the following commands:
- CMake v.3.5 or later. On Amazon Linux, run the following command:
- Git
Download and compile the runtime
The first step is to download & compile the runtime:
This builds and installs the runtime as a static library under the directory ~/out.
Create your C++ function
The next step is to build the Lambda C++ function.
- Create a new directory for this project:
- In that directory, create a file named main.cpp with the following content:
- Create a file named CMakeLists.txt in the same directory, with the following content:
- To build this executable, create a build directory and run CMake from there:
This compiles and links the executable in release mode.
- To package this executable along with all its dependencies, run the following command:
This creates a zip file in the same directory named after your project, in this case hello.zip.
Create the Lambda function
Using the AWS CLI, you create the Lambda function. First, create a role for the Lambda function to execute under.
- Create the following JSON file for the trust policy and name it trust-policy.json.
- Using the AWS CLI, run the following command:
This should output JSON that contains the newly created IAM role information. Make sure to note down the “Arn” value from that JSON. You need it later. The Arn looks like the following:
“Arn”: “arn:aws:iam::<account_id>:role/lambda-cpp-demo”
- Create the Lambda function:
- Invoke the function using the AWS CLI:
<bash>You should see the following output:
A file named output.txt containing the words “Hello, World!” should be in the current directory.
Beyond Hello
OK, well that was exciting, but how about doing something slightly more interesting?
The following example shows you how to download a file from Amazon S3 and do some basic processing of its contents. To interact with AWS, you need the AWS SDK for C++.
Prerequisites
If you don’t have them already, install the following libraries:
- zlib-devel
- openssl-devel
- Build the AWS SDK for C++:
This builds the S3 SDK as a static library and installs it in ~/out.
- Create a directory for the new application’s logic:
- Now, create the following main.cpp:
This Lambda function expects an input payload to contain an S3 bucket and S3 key. It then downloads that resource from S3, encodes it as base64, and sends it back as the response of the Lambda function. This can be useful to display an image in a webpage, for example.
- Next, create the following CMakeLists.txt file in the same directory.
- Follow the same build steps as before:
Notice how the target name for packaging has changed to aws-lambda-package-encoder. The CMake function aws_lambda_package_target() always creates a target based on its input name.
You should now have a file named “encoder.zip” in your build directory.
- Before you create the Lambda function, modify the IAM role that you created earlier to allow it to access S3.
- Using the AWS CLI, create the new Lambda function:
- Using the AWS CLI, run the function. Make sure to use a S3 bucket in the same Region as the Lambda function:
You can use an online base64 image decoder and paste the contents of the output file to verify that everything is working. In a real-world scenario, you would inject the output of this Lambda function in an HTML img tag, for example.
Conclusion
With the new Lambda Runtime API, a new door of possibilities is open. This C++ runtime enables you to do more with Lambda than you ever could have before.
More in-depth details, along with examples, can be found on the GitHub repository. With it, you can start writing Lambda functions with C++ today. AWS will continue evolving the contents of this repository with additional enhancements and samples. I’m so excited to see what you build using this runtime. I appreciate feedback sent via issues in GitHub.
Happy hacking!