|
| 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 | +``` |
0 commit comments