title: How to write unit test for the function which is accessing aws resources?
body: <p>I have a function which is accessing multiple <code>aws</code> resources and now need to test this function, but I don't know how to mock these resources.</p>
  
  <p>I have tried following github of aws-sdk-mock, but didn't get much there. </p>
  
  <pre><code>function someData(event, configuration, callback) {
  
      // sts set-up
      var sts = new AWS.STS(configuration.STS_CONFIG);
  
      sts.assumeRole({
        DurationSeconds: 3600,
        RoleArn: process.env.CROSS_ACCOUNT_ROLE,
        RoleSessionName: configuration.ROLE_NAME
      }, function(err, data) {
        if (err) {
          // an error occurred
          console.log(err, err.stack);
        } else {
          // successful response
  
          // resolving static credential
          var creds = new AWS.Credentials({
            accessKeyId: data.Credentials.AccessKeyId,
            secretAccessKey: data.Credentials.SecretAccessKey,
            sessionToken: data.Credentials.SessionToken
          });
  
           // Query function
           var dynamodb = new AWS.DynamoDB({apiVersion: configuration.API_VERSION, credentials:  creds, region: configuration.REGION});
           var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: configuration.API_VERSION, region: configuration.REGION, endpoint: configuration.DDB_ENDPOINT, service: dynamodb });
  
              // extract params
              var ID = event.queryStringParameters.Id;
              console.log('metrics of id ' + ID);
  
              var params = {
                  TableName: configuration.TABLE_NAME,
                  ProjectionExpression: configuration.PROJECTION_ATTR,
                  KeyConditionExpression: '#ID = :ID',
                  ExpressionAttributeNames: {
                      '#ID': configuration.ID
                  },
                  ExpressionAttributeValues: {
                      ':ID': ID
                  }
              };
  
              queryDynamoDB(params, docClient).then((response) =&gt; {
                  console.log('Params: ' + JSON.stringify(params));
                  // if the query is Successful
                  if( typeof(response[0]) !== 'undefined'){
                      response[0]['Steps'] = process.env.STEPS;
                      response[0]['PageName'] = process.env.STEPS_NAME;
                  }
                  console.log('The response you get', response);
                  var success = {
                      statusCode: HTTP_RESPONSE_CONSTANTS.SUCCESS.statusCode,
                      body: JSON.stringify(response),
                      headers: {
                          'Content-Type': 'application/json'
                      },
                      isBase64Encoded: false
                  };
                  return callback(null, success);
              }, (err) =&gt; {
                  // return internal server error
                  return callback(null, HTTP_RESPONSE_CONSTANTS.BAD_REQUEST);
              });
        }
  
      });
  
  }
  </code></pre>
  
  <p>This is <code>lambda</code> function which I need to test, there are some env variable also which is being used here.</p>
  
  <p>Now I tried writing Unit test for above function using <code>aws-sdk-mock</code> but still I am not able to figure out how to actually do it. Any help will be appreciated. Below is my test code</p>
  
  <pre><code>describe('test getMetrics', function() {
  
      var expectedOnInvalid = HTTP_RESPONSE_CONSTANTS.BAD_REQUEST;
  
      it('should assume role ', function(done){
          var event = {
            queryStringParameters : {
                Id: '123456'
            }
          };
  
          AWS.mock('STS', 'assumeRole', 'roleAssumed');
          AWS.restore('STS');
          AWS.mock('Credentials', 'credentials');
          AWS.restore('Credentials');
          AWS.mock('DynamoDB.DocumentClient', 'get', 'message');
          AWS.mock('DynamoDB', 'describeTable', 'message');
          AWS.restore('DynamoDB');
          AWS.restore('DynamoDB.DocumentClient');
  
          someData(event, configuration, (err, response) =&gt; {
              expect(response).to.deep.equal(expectedOnInvalid);
              done();
          });
  
  
      });
  
  
  });
  </code></pre>
  
  <p>I am getting the following error :</p>
  
  <pre><code>{ MultipleValidationErrors: There were 2 validation errors:
  * MissingRequiredParameter: Missing required key 'RoleArn' in params
  * MissingRequiredParameter: Missing required key 'RoleSessionName' in params
  </code></pre>