DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Migrating MuleSoft System API to AWS Lambda (Part 1)
  • Keep Your Application Secrets Secret
  • Building a Scalable ML Pipeline and API in AWS
  • Container Checkpointing in Kubernetes With a Custom API

Trending

  • Web Crawling for RAG With Crawl4AI
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Infrastructure as Code (IaC) Beyond the Basics
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Deploy Your C++ Lambda with Docker

Deploy Your C++ Lambda with Docker

In this article we will explore how you can use your C++ runtime with Lambda Functions.

By 
Sudip Sengupta user avatar
Sudip Sengupta
DZone Core CORE ·
Jul. 21, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.8K Views

Join the DZone community and get the full member experience.

Join For Free

In this article we will explore how you can use your C++ runtime with Lambda Functions. AWS Lambda allows us to use our app in a serverless architecture. As an addition to all the available runtimes in AWS Lambda, AWS announced Custom Runtimes at AWS Re:Invent 2018 where they released open-source custom runtimes for C++.

The advantage of using a custom runtime is that it allows you to use any sort of code in your Lambda function. Though there are few examples to use C++ Api Runtime but there is not enough reference on HOW to use a handler function and send/receive JSON data, which we will explore below.

Prerequisites

You need a Linux-based environment. It will be used for compiling and deploying your code -

Shell
 




xxxxxxxxxx
1


 
1
##C++11 compiler, either GCC 5.x or later 
2
Clang 3.3 or later $ yum install gcc64-c++ libcurl-devel 
3
$ export CC=gcc64 
4
$ export CXX=g++64


Shell
 




xxxxxxxxxx
1


 
1
##CMake v.3.5 or later. 
2
$ yum install cmake3


Shell
 




xxxxxxxxxx
1


 
1
##Git $ 
2
yum install git


Here we will install all these tools with Docker. Running the docker build command with Dockerfile will install the necessary AWS Linux machine image's environment, complying C++ code and deploy to AWS Lambda.

Note:

  1. This Dockerfile is only for an existing Lambda update, hence please ensure to create your Lambda instance by either the AWS CLI or the AWS Console.
  2. Please ensure to create the API Gateway and bind with C++ Lambda.

The reason for using Docker is that it will create an isolated place to work, preventing all possible errors and making it simple and re-use.

C++ Lambda Compiler With Dockerfile

Open a project directory which includes all project codes and the Dockerfile, following which create the Docker image in the same directory. An example on how it should look like:

Let's start to build our Docker image from the Dockerfile.

Shell
 




xxxxxxxxxx
1


 
1
FROM amazonlinux:latest 
2

             
3
## install required development packages 
4
RUN yum -y groupinstall "Development tools" 
5
RUN yum -y install gcc-c++ libcurl-devel cmake3 git 
6
RUN pip3 install awscli


The above will use Amazon Linux for the base image and install necessary components.

Following that, we will add the C++ API runtime environment. This will let you install and use Lambda libraries for C++ in your code; through the call-handler function, you would be able to use the C++ dependencies.

Shell
 




xxxxxxxxxx
1


 
1
# install C++ runtime environment 
2
RUN git clone https://github.com/awslabs/aws-lambda-cpp.git && \
3
    cd aws-lambda-cpp && mkdir build && cd /aws-lambda-cpp/build && \
4
    cmake3 .. -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF \
5
    -DCMAKE_INSTALL_PREFIX=/out -DCMAKE_CXX_COMPILER=g++ && \
6
    make && make install


This will install the Lambda C++ environment — you do not need to change anything in this part.
CMAKE will handle and use this library creating an output point which our main code will use as a dependency while compiling.

Shell
 




xxxxxxxxxx
1


 
1
# include C++ source code and build configuration 
2
ADD adaptiveUpdate.cpp /adaptiveUpdate 
3
ADD CMakeLists.txt /adaptiveUpdate


Copy your main code and the CMAKE file to the Docker container.

Shell
 




x


 
1
# compile & package C++ code 
2
RUN cd /adaptiveUpdate && mkdir build && cd build && \
3
    cmake3 .. -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF \
4
    -DCMAKE_PREFIX_PATH=/out -DCMAKE_CXX_COMPILER=g++  && \
5
    make && make aws-lambda-package-adaptiveUpdate


This will compile our code and make a deployable zip file. You can change your package name and will find it in your build folder which is located under the code directory. Next step is to deploy this zip file as a Lambda function to your AWS account. To make this happen we need to add ENV which defines our user access-id and secret-key.

Shell
 




xxxxxxxxxx
1


 
1
ENV AWS_DEFAULT_REGION='' 
2
ENV AWS_ACCESS_KEY_ID='' 
3
ENV AWS_SECRET_ACCESS_KEY='' 
4

             
5
RUN aws lambda update-function-code --function-name hello-world \
6
--zip-file fileb://adaptiveUpdate/build/adaptiveUpdate.zip 


Add your secret key, access id and region to access and deploy your code to AWS. Let us start by creating a Lambda named hello-world. Additional details can be found in the link below -

awslabs/aws-lambda-cpp
C++ implementation of the AWS Lambda runtime. Contribute to awslabs/aws-lambda-cpp development by creating an account on GitHub.

Handler Method For Lambda

When you start using Lambda, you will notice that Lambda needs handler function, and below we will see how you can import the C++ handler function to your main code and how you retrieve your function as a return. The handler function is used to access the Lambda function and to send and retrieve JSON data.

Firstly you need to create a main function which will call the handler function.

JSON
 




xxxxxxxxxx
1


 
1
int main() 
2
 {
3
  run_handler(my_handler);
4
     return 0;
5
 }


That's it!

Secondly we will add our code handler function which includes response and request when you invoke Lambda.

JSON
 




xxxxxxxxxx
1


 
1
invocation_response my_handler(invocation_request const& req)
2
 {
3
  string json = req.payload;
4
   test(json);
5
    return invocation_response::success(json, "application/json");
6
 } 
7

             
8

             


In the section below,  we are receiving the payload of the JSON value which separated from header.

JSON
 




xxxxxxxxxx
1


 
1
  string json = req.payload;   


JSON value will be similar to:


Create a string variable considering the req.payload JSON value as a string. Use it update the test function.  

Shell
 




xxxxxxxxxx
1


 
1
  test(json);  


Shell
 




xxxxxxxxxx
1


 
1
  string json = req.payload;   


Return the main function with the updated value. Note that this will return it as JSON format.

Shell
 




xxxxxxxxxx
1


 
1
  return invocation_response::success(json, "application/json");   


In the next stage we will invoke Lambda with the API Gateway.

API Gateway to Invoke Lambda

There are several ways to invoke your Lambda function, which can be through CLI, S3 or API Gateway. For the purpose of this article, we are going to use the API Gateway (REST API) method to invoke Lambda. You can configure this gateway through the Swagger Yaml, details of which can be found in the link below.

AWS Lambda Runtime Interface
AWS Lambda provides an HTTP API for custom runtimes to receive invocation events from Lambda and send response data back within the Lambda execution environment.

We are going to use our API Gateway as a proxy for this project. We will create a stage, resources and method, following which we will integrate it with our Lambda function.

  1. Give a name to your REST API: ours is cppjsonupdate. Leave everything as default. Click on Create API.
  1. Next is to create new resources for our API. Choose Create Resource under the Actions menu and set a Resource Name as per your choice, and give the path or can leave it as Default. Click on Create Resource.
  1. Under the Actions menu, add a Method. We are choosing Any for the purpose of this article. Please note to click the green tick to save.
  1. Deploy your API by clicking Deploy API under the Actions menu. Choose Popup Menu Stage. You will also need to add ARN of the Lambda function which you can find in Lambda service page. Following that choose New Stage and set a Stage Name. Click on Deploy.
JSON
 




xxxxxxxxxx
1


 
1
arn:aws:lambda:us-east-1:34728394278492:function:adaptiveupdate



  1. Your API Gateway is now configured and integrated with C++ lambda. You can use endpoint for invocation. It should be similar to this.

As the final step, you can find the invoke URL under the Stages section as shown below.

Conclusion

Hopefully, with the help of this article, you can now understand and use your code in Lambda Runtime API to easily manage, create, and deploy your function on Docker.

Previously posted at https://appfleet.com/.

Docker (software) API AWS AWS Lambda shell JSON

Published at DZone with permission of Sudip Sengupta. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Migrating MuleSoft System API to AWS Lambda (Part 1)
  • Keep Your Application Secrets Secret
  • Building a Scalable ML Pipeline and API in AWS
  • Container Checkpointing in Kubernetes With a Custom API

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!