3. Create a Lambda Function for Handling Requests


AWS Lambda will run your code in response to events such as an HTTP request. In this step you’ll build the core function that will process API requests from the web application to dispatch a unicorn. In the next module you’ll use Amazon API Gateway to create a RESTful API that will expose an HTTP endpoint that can be invoked from your users’ browsers. You’ll then connect the Lambda function you create in this step to that API in order to create a fully functional backend for your web application.

High-Level Instructions

Use the AWS Lambda console to create a new Lambda function called RequestUnicorn that will process the API requests. Use the provided requestUnicorn.js example implementation for your function code. Just copy and paste from that file into the AWS Lambda console’s editor.

Make sure to configure your function to use the WildRydesLambda IAM role you created in the previous section.

✅ Step-by-step directions

  1. Go to the AWS Lambda console
  2. Click Create function.
  3. Keep the default Author from scratch card selected.
  4. Enter RequestUnicorn in the Name field.
  5. Select Node.js 12.x for the Runtime.
  6. Expand Choose or create an execution role under Permissions.
  7. Ensure Choose an existing role is selected from the Role dropdown.
  8. Select WildRydesLambda from the Existing Role dropdown. Create Lambda function screenshot
  9. Click on Create function.
  10. Scroll down to the Function code section and replace the existing code in the index.js code editor with the contents of requestUnicorn.js. Create Lambda function screenshot
  11. Click “Save” in the upper right corner of the page.

Implementation Validation

For this module you will test the function that you built using the AWS Lambda console. In the next module you will add a REST API with API Gateway so you can invoke your function from the browser-based application that you deployed in the first module.

✅ Step-by-step directions

  1. From the main edit screen for your function, select Configure test events from the the Select a test event… dropdown. Configure test event
  2. Keep Create new test event selected.
  3. Enter TestRequestEvent in the Event name field
  4. Copy and paste the following test event into the editor:

        "path": "/ride",
        "httpMethod": "POST",
        "headers": {
            "Accept": "*/*",
            "Authorization": "eyJraWQiOiJLTzRVMWZs",
            "content-type": "application/json; charset=UTF-8"
        "queryStringParameters": null,
        "pathParameters": null,
        "requestContext": {
            "authorizer": {
                "claims": {
                    "cognito:username": "the_username"
        "body": "{\"PickupLocation\":{\"Latitude\":47.6174755835663,\"Longitude\":-122.28837066650185}}"

    Configure test event

  5. Click Create.

  6. On the main function edit screen click Test with TestRequestEvent selected in the dropdown.

  7. Scroll to the top of the page and expand the Details section of the Execution result section.

  8. Verify that the execution succeeded and that the function result looks like the following:

      "statusCode": 201,
      "body": "{\"RideId\":\"1h0zDZ-6KLZaEQCPyqTxeQ\",\"Unicorn\":{\"Name\":\"Shadowfax\",\"Color\":\"White\",\"Gender\":\"Male\"},\"UnicornName\":\"Shadowfax\",\"Eta\":\"30 seconds\",\"Rider\":\"the_username\"}",
      "headers": {
        "Access-Control-Allow-Origin": "*"