In this example, we'll build a simple "Hello, World" lambda function that can be invoked using an api endpoint created using Amazon API gateway. This example can be viewed as the C++ counterpart to the NodeJS "Hello, World" API example as viewed here. At the end of this example, you should be able to invoke your lambda via an api endpoint and receive a raw JSON response. This example employs the use of the AWS C++ SDK to parse the request and write the necessary response.
Start by building the SDK from source.
$ mkdir ~/install
$ git clone https://github.com/aws/aws-sdk-cpp.git
$ cd aws-sdk-cpp
$ mkdir build
$ cd build
$ cmake .. -DBUILD_ONLY="core" \
-DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DCUSTOM_MEMORY_MANAGEMENT=OFF \
-DCMAKE_INSTALL_PREFIX=~/install \
-DENABLE_UNITY_BUILD=ON
$ make
$ make installWe need to build the C++ Lambda runtime as outlined in the other examples.
$ git clone https://github.com/awslabs/aws-lambda-cpp-runtime.git
$ cd aws-lambda-cpp-runtime
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_BUILD_TYPE=Release \
-DBUILD_SHARED_LIBS=OFF \
-DCMAKE_INSTALL_PREFIX=~/install \
$ make
$ make installThe next step is to build the Lambda function in main.cpp and run the packaging command as follows:
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=~/install
$ make
$ make aws-lambda-package-apiYou should now have a zip file called api.zip. Follow the instructions in the main README to upload it and return here once complete.
For the rest of this example, we will use the AWS Management Console to create the API endpoint using Amazon API Gateway.
- Navigate to AWS Lambda within the console here
- Select the newly created function. Within the specific function, the "Designer" window should appear.
- Simply click "Add trigger" -> "API Gateway" -> "Create an API". Please view the settings below.
- API Type: HTTP API
- Security: Open
- API name: Hello-World-API (or desired name)
- Deployment stage: default
- Once you have added the API gateway, locate the newly created endpoint. View how to test the endpoint below.
Feel free to test the endpoint any way you desire. Below is a way to test using cURL:
curl -v -X POST \
'<YOUR-API-ENDPOINT>?name=Bradley&city=Chicago' \
-H 'content-type: application/json' \
-H 'day: Sunday' \
-d '{ "time": "evening" }'
With the expected response being:
{
"message": "Good evening, Bradley of Chicago. Happy Sunday!"
}