Snippets

Steele Parker Administer Mocha Test, Accessing DynamoDB via Lambda Function using NPM

Created by Steele Parker last modified
/* This file is actually a package.json but for the sake of comments, it's using JavaScript syntax. Remove comments if you copy/paste this
 *
 * It is critcal to provide the -A switch to the mocha test script to force it to wait for the context to be notified of completion
 * Otherwise the test will terminate before the callback from dynamoDB is recieved, terminating the request with no output.
 */
{
  "name": "my-service",
  "version": "1.0.0",
  "description": "do-something-with-lambda",
  "private": true,
  "main": "index.js",
  "scripts": {
    "test": "mocha -A"
    //Other test scripts here
  },
  "devDependencies": {
    "aws-sdk": "2.7.11",
    "lambda-local": "1.3.0",
    "mocha": "2.3.0"
    //Other packages here
  }
}
var AWS = require("aws-sdk");

AWS.config.update({
    region: "ap-southeast-2",
    endpoint: "https://dynamodb.ap-southeast-2.amazonaws.com/"
});

var dynamodb = new AWS.DynamoDB({
    apiVersion: "2012-08-10"
    /*
     * This is the gotcha. If running this test in lambdaLocal, the accessKeyId and SecretAccessKey IS NOT AUTOMATICALLY ATTACHED TO DYNAMODB
     * Any attempt to use dynamoDB will throw errors regarding your credentials defined in your credentials profile (~/.aws/credentials)
     * However you will not be notified of a lack of credentials, you will simply recieve a UnrecognizedClientException.
     *
     * It's almost as though the EnvironmentCredentials are simply not attached by lambda-local
     *
     * Note: If you output dynamodb.config it will show an instance of EnvironmentCredentials with the correct accessKeyId, however
     * omitting the accessKeyId line from this set of args will cause the error to occur.
     */
    accessKeyId:AWS.config.credentials.accessKeyId, 
    secretAccessKey:AWS.config.credentials.secretAccessKey
});

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

    var params = {};

    dynamodb.listTables(params, function (err, data) {
        console.log("ListTables");
        console.log(err, data);

        context.done()
    });
}
const lambdaLocal = require('lambda-local');

console.log("Initialising Test");

describe("Testing Lambda Functions", function() {
    it("Test Lambda Execution", function (done) {
        //Ensure we wait appropriately for the lambdaLocal to do its thing
        this.timeout(5000);

        lambdaLocal.execute({
            event: {},
            lambdaPath: 'index.js',
            profilePath: '~/.aws/credentials',
            /*Optional*/profileName: 'someProfile',
            timeoutMs: 3000,
            region: "ap-southeast-2",
            callback: function (err, data) {
                /* Without done() the test will throw an exception */
                if (err) done(err);
                else done();
            }
        });
    });
});

console.log("Test Complete");

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.