Skip to content

Instantly share code, notes, and snippets.

@cmawhorter
Last active December 24, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmawhorter/f56f441a403ae48a487c to your computer and use it in GitHub Desktop.
Save cmawhorter/f56f441a403ae48a487c to your computer and use it in GitHub Desktop.
Script to trigger lambda throttling. Creates many concurrent lambda calls to a long-running function.
// just a control; doesn't matter specifics
exports.handler = function(event, context) {
console.log('starting');
setTimeout(function() {
console.log('done');
context.succeed();
}, 1000);
};
// succeeds after 5m
exports.handler = function(event, context) {
console.log('starting');
setTimeout(function() {
console.log('done');
context.succeed();
}, 300000);
};
'use strict';
// expects your aws key/secret to be in env
// and both async & aws-sdk installed
// calls the target lambda function with parallelLimit N times; logs results
// 1. create two lambda functions slowFunctionTest (5m execution duration) and functionWhileThrottled (whatever)
// 2. run ~500 iterations on slowFunctionTest with Event invocation; all should return http 202
// 3. check console to ensure throttling is in effect
// 4. change target function to functionWhileThrottled and RequestResponse invocation and run ~20 iterations
// 5. will likely show "Rate Limited" errors (http 429)
var async = require('async');
var AWS = require('aws-sdk');
var lambda = new AWS.Lambda({ region: 'us-east-1' });
var tasks = [];
for (var i=0; i < 5; i++) {
(function(index) {
tasks.push(function(taskCallback) {
console.log('Invoking', index);
lambda.invoke({
// FunctionName: 'slowFunctionTest',
// InvocationType: 'Event',
FunctionName: 'functionWhileThrottled',
InvocationType: 'RequestResponse',
LogType: 'None',
Payload: ''
}, function(err, res) {
if (err) {
taskCallback(null, { index: index, response: err.message });
}
else {
taskCallback(null, { index: index, response: res.StatusCode });
}
});
});
})(i);
}
async.parallelLimit(tasks, 8, console.log.bind(console));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment