Create Function

  • Author from scratch
  • Name: myFunction
  • Runtime: Node.js 6.10
  • Role: create new role from template
  • Role name: lambdaRole
  • Policy templates: Big Edge Lambda permissions

The starter code is very simple. It gets a request of any kind and returns the string "Hello from Lambda".

exports.handler = (event, context, callback) => {
    // TODO implement
    callback(null, 'Hello from Lambda');
};

Right now our lambda is just a small program. There is a web IDE, Cloud9, you can use in browser to make modifications and play around. To execute any modified code, press the Test button at the top right new to Save. The test button is an excellent way to ensure you haven't broken anything. By pairing this program with Triggers you can have this code execute whenever a certrain event happens such as Alexa skill, S3 bucket change, API web call, code commit, etc. In this example the focus will be making API web calls.

Adding the API

In the Designer ribbon, add a trigger of type API Gateway. Immediately a ribbon shows up labeled Configure Trigger.

  • API - Create New API
  • API Name - lambdaAPI
  • Depolyment Stage - tempstage can be anything; will be referenced later
  • Security - Open with access key
  • WHEN DONE, click ADD then SAVE in the top right.

To access this new API Gatewate, click the name of the API you just created in the same ribbon.

Tip:
When using this Lambda console, I noticed the screen is not super responsive. It takes a while for the buttons to become enabled after actions are taken.
Also, the diagram at the top of the page is actually a modal selection that displays another screen with additional configurations.
function_apigw
Clicking API Gateway will get you the API Gateway ribbon at the bottom and clicking the top Lambda icon will take you back to the default screen.

The API Gateway console has its own test that are runnable. The API and Lambda tests are two different sets that can conclude with two different results. From the API Gateway console, find the "lambdaAPI" API on the left resources pane. It brings up a series of end points for this specific API. myFunction is listed and shows it takes ANY type of request. By clicking ANY, a new diagram will pop up along with a test button. Any method, request body, and query/path parameters can be set here to test various scenarios.

Immedaitely, the API tests will fail. I kept receiving an error and at the bottom of the error message in the API tests is saw Malformed Lambda proxy response then found the solution. The return from the Lambda has to have a specific structure for the API. This new defined structure has status code, headers, body, and if its base64 encoded. Only now will the API Gateway be able to interpret our response. Now our code now look like:

exports.handler = (event, context, callback) => {

    var responseBody = "Hello";

    var response = {
        "statusCode": 200,
        "headers": {
            "my_header": "my_value"
        },
        "body": JSON.stringify(responseBody),
        "isBase64Encoded": false
    };
    callback(null, response);
};

Under the API Gateway test, set the Method to GET leaving everything else to blank and click Test. The tests should be passing!

To get the exposed URL to call your lambda, go to the lambda screen > click on the API Gateway in the chart at the top > Expand the Api Gateway section. Here you will find Invoke URL. Use this to intact till your heart is content or you hit your 1 million free calls ;P.