Skip to content

Commit 2beedaa

Browse files
author
Bradley Bottomlee
authored
Add simple Hello World lambda example using API Gateway (awslabs#97)
1 parent fe55d84 commit 2beedaa

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

examples/api-gateway/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
set(CMAKE_CXX_STANDARD 11)
3+
4+
project(api LANGUAGES CXX)
5+
6+
find_package(aws-lambda-runtime REQUIRED)
7+
find_package(AWSSDK COMPONENTS core)
8+
9+
add_executable(${PROJECT_NAME} "main.cpp")
10+
target_link_libraries(${PROJECT_NAME} PUBLIC AWS::aws-lambda-runtime ${AWSSDK_LINK_LIBRARIES})
11+
12+
aws_lambda_package_target(${PROJECT_NAME})

examples/api-gateway/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Example using the AWS C++ Lambda runtime and Amazon API Gateway
2+
3+
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](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html). 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.
4+
5+
## Build the AWS C++ SDK
6+
Start by building the SDK from source.
7+
8+
```bash
9+
$ mkdir ~/install
10+
$ git clone https://github.com/aws/aws-sdk-cpp.git
11+
$ cd aws-sdk-cpp
12+
$ mkdir build
13+
$ cd build
14+
$ cmake .. -DBUILD_ONLY="core" \
15+
-DCMAKE_BUILD_TYPE=Release \
16+
-DBUILD_SHARED_LIBS=OFF \
17+
-DENABLE_UNITY_BUILD=ON \
18+
-DCUSTOM_MEMORY_MANAGEMENT=OFF \
19+
-DCMAKE_INSTALL_PREFIX=~/install \
20+
-DENABLE_UNITY_BUILD=ON
21+
$ make
22+
$ make install
23+
```
24+
25+
## Build the Runtime
26+
We need to build the C++ Lambda runtime as outlined in the other examples.
27+
28+
```bash
29+
$ git clone https://github.com/awslabs/aws-lambda-cpp-runtime.git
30+
$ cd aws-lambda-cpp-runtime
31+
$ mkdir build
32+
$ cd build
33+
$ cmake .. -DCMAKE_BUILD_TYPE=Release \
34+
-DBUILD_SHARED_LIBS=OFF \
35+
-DCMAKE_INSTALL_PREFIX=~/install \
36+
$ make
37+
$ make install
38+
```
39+
40+
## Build the application
41+
The next step is to build the Lambda function in `main.cpp` and run the packaging command as follows:
42+
43+
```bash
44+
$ mkdir build
45+
$ cd build
46+
$ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=~/install
47+
$ make
48+
$ make aws-lambda-package-api
49+
```
50+
51+
You should now have a zip file called `api.zip`. Follow the instructions in the main README to upload it and return here once complete.
52+
53+
## Using Amazon API Gateway
54+
For the rest of this example, we will use the AWS Management Console to create the API endpoint using Amazon API Gateway.
55+
56+
1. Navigate to AWS Lambda within the console [here](https://console.aws.amazon.com/lambda/home)
57+
1. Select the newly created function. Within the specific function, the "Designer" window should appear.
58+
1. Simply click "Add trigger" -> "API Gateway" -> "Create an API". Please view the settings below.
59+
* API Type: HTTP API
60+
* Security: Open
61+
* API name: Hello-World-API (or desired name)
62+
* Deployment stage: default
63+
1. Once you have added the API gateway, locate the newly created endpoint. View how to test the endpoint below.
64+
65+
## Test the endpoint
66+
Feel free to test the endpoint any way you desire. Below is a way to test using cURL:
67+
68+
```
69+
curl -v -X POST \
70+
'<YOUR-API-ENDPOINT>?name=Bradley&city=Chicago' \
71+
-H 'content-type: application/json' \
72+
-H 'day: Sunday' \
73+
-d '{ "time": "evening" }'
74+
```
75+
76+
With the expected response being:
77+
```
78+
{
79+
"message": "Good evening, Bradley of Chicago. Happy Sunday!"
80+
}
81+
```

examples/api-gateway/main.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <aws/lambda-runtime/runtime.h>
2+
#include <aws/core/utils/json/JsonSerializer.h>
3+
#include <aws/core/utils/memory/stl/SimpleStringStream.h>
4+
5+
using namespace aws::lambda_runtime;
6+
7+
invocation_response my_handler(invocation_request const& request)
8+
{
9+
10+
using namespace Aws::Utils::Json;
11+
12+
JsonValue json(request.payload);
13+
if (!json.WasParseSuccessful()) {
14+
return invocation_response::failure("Failed to parse input JSON", "InvalidJSON");
15+
}
16+
17+
auto v = json.View();
18+
Aws::SimpleStringStream ss;
19+
ss << "Good ";
20+
21+
if (v.ValueExists("body") && v.GetObject("body").IsString()) {
22+
auto body = v.GetString("body");
23+
JsonValue body_json(body);
24+
25+
if (body_json.WasParseSuccessful()) {
26+
auto body_v = body_json.View();
27+
ss << (body_v.ValueExists("time") && body_v.GetObject("time").IsString() ? body_v.GetString("time") : "");
28+
}
29+
}
30+
ss << ", ";
31+
32+
if (v.ValueExists("queryStringParameters")) {
33+
auto query_params = v.GetObject("queryStringParameters");
34+
ss << (query_params.ValueExists("name") && query_params.GetObject("name").IsString()
35+
? query_params.GetString("name")
36+
: "")
37+
<< " of ";
38+
ss << (query_params.ValueExists("city") && query_params.GetObject("city").IsString()
39+
? query_params.GetString("city")
40+
: "")
41+
<< ". ";
42+
}
43+
44+
if (v.ValueExists("headers")) {
45+
auto headers = v.GetObject("headers");
46+
ss << "Happy "
47+
<< (headers.ValueExists("day") && headers.GetObject("day").IsString() ? headers.GetString("day") : "")
48+
<< "!";
49+
}
50+
51+
JsonValue resp;
52+
resp.WithString("message", ss.str());
53+
54+
return invocation_response::success(resp.View().WriteCompact(), "application/json");
55+
}
56+
57+
int main()
58+
{
59+
run_handler(my_handler);
60+
return 0;
61+
}

0 commit comments

Comments
 (0)