Amazon Lambda


I was having problem with limits on request / response body size, and was having problem with accessing RDS. - done reading - done reading, but I should come back to this later because it covers command-line usage. - done reading - later - done reading - Later. This is for BeanStalk environment - done reading - done reading - done reading - later - done reading

How to create our first Lambda function using Node?

  1. In the AWS Lambda interface, we can click on the "Create function" in the top right corner of the screen.
  2. Give the function a name
  3. Select a run-time (Node version)
  4. Select an existing role or create a new role
  5. Click on the "Create function" button at the bottom of the screen

On the subsequent screen, we can select "Edit code inline", "Upload a ZIP file", or "Upload a file from Amazon S3". If we choose the "Edit code inline" option, and it keeps saying "Loading your function", we can right click in this middle section of the page and select "Reload frame" (if we are using Chrome). This is a nice online editor for some simple code, but for things that are more complicated, like requiring other modules, we need to create a Node project:

  1. Open a command prompt
  2. Navigate to a folder where we want to keep our project
  3. mkdir projectName
  4. cd projectName
  5. Run "npm init" and provide answers to on-screen questions.
  6. npm install mysql --save

The above steps assume that we already have Node, and npm installed, and that we are already familiar with creating a Node project. Now create the index.js:

var mysql = require("mysql");
exports.handler = (event, context, callback) => {
    const connection = mysql.createConnection({
      host     : '...',
      user     : '...',
      password : '...',
      database : '...'

    connection.connect(function(err) {
        if (err) {
            return callback(err);
        if (err) throw err;

        const sql = 'SELECT NOW() FROM DUAL';
        connection.query(sql, function (error, results, fields) {
            if (error) {
                return callback(error)
            callback(null, results)


Open Windows Explorer and goes into our project folder. This folder should now contains: index.js, package.json, and the node_modules folder. Select these items, right click on these items, and select "Send to -> Compressed (zipped) folder". Give this zip file a name. Switch back to the browser. Select the "Upload a .ZIP file" option. Click on the Upload button. Select the zip file that we just created and upload. Click on the Test button in the top right corner of the screen and follow the on-screen instruction. If everything is configured correctly, the test should pass, otherwise, it may fails or timed-out, and we will need to look at the log. When we run a test, and if it failed, it should display "Execution result: failed(logs)" at the top. We can click on (logs) to see more detail.

The above code is just a function. There is no URL associated with it yet. We can specify the URL for this function using AWS API Gateway. Our project can be complex and involve multiple URLs, so we can modify the logic in the exports.handler function to match whatever the equivalent of a Traditional Node project need to be.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License