javascript - Pass AWS config to imported module -


i trying unit test js aws lambda running locally. emulate lambda environment assuming same role lambda have aws.config.credentials , calling lambda function imported require.

this works if copy , paste lambda function test file if import seems function runs clean aws.config doesn't have configuration. change way lambda module imports aws (make global or else) in order config test prefer keep file should uploading lambda.

in aws lambda aws credentials somehow preconfigured without module having , emulate functionality. how can done in node?

edit: can run example function in aws lambda. example following code works when run in proper aws lambda.

var aws = require("aws-sdk");  var dynamo = new aws.dynamodb.documentclient();  dynamo.query(...)

to run locally have precede code following:

aws.config.update({region: 'us-east-1'});   aws.config.credentials = new aws.sharedinifilecredentials();  aws.config.credentials = new aws.temporarycredentials({    rolearn: 'arn:aws:iam::1234567890:role/temporarycredentials',  });

the problem running config step in unit test file not make config available imported lambda function , therefore not emulate environment properly.

here's solution using jest. sure can adapt other testing frameworks. think it's easier in jest.

basically, create mock of aws-sdk module returns actual aws-sdk module and own modifications.

// __mocks__/aws-sdk.js (jest automocks module if follow convention) 'use strict';  const aws = require('aws');   // make own modifications aws.config.update({region: 'us-east-1'});  aws.config.credentials = new aws.sharedinifilecredentials(); aws.config.credentials = new aws.temporarycredentials({   rolearn: 'arn:aws:iam::1234567890:role/temporarycredentials', });   //export modified module module.exports = aws 

with this, when lambda requires aws-sdk, loads module instead. , comes actual aws-sdk module , additional config.

reference: https://facebook.github.io/jest/docs/en/manual-mocks.html#content


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -