javascript - InvalidParameterValueException: The role defined for the function cannot be assumed by Lambda -
i'm using aws sdk javascript , returning following error when try create lambda function:
invalidparametervalueexception: role defined function cannot assumed lambda.
i've double-checked role , valid. however, i'm still unable create lambda function.
my role trust relationship is:
{ "version": "2012-10-17", "statement": [ { "effect": "allow", "principal": { "service": [ "lambda.amazonaws.com" ] }, "action": [ "sts:assumerole" ] } ] }
this error happens when role invalid (which not case) or when try create lambda function just after role creation. amazon needs few seconds replicate new role through regions. so, fix here wait few seconds before creating lambda function.
solution - example 1:
var aws = require('aws-sdk'); var lambda = new aws.lambda(); var params = {}; // define parameters lambda.createfunction(params, function(err, data) { if (err && err.code === 'invalidparametervalueexception') { // try again after few seconds settimeout(function(){ lambda.createfunction(params, callback); }, 10000); } else { callback(err, data); } });
solution - example 2:
usually, waiting 5 seconds enough, can take little more. more robust solution, can use retry module this one.
var aws = require('aws-sdk'); var retry = require('retry'); var lambda = new aws.lambda(); var params = {}; // define parameters var operation = retry.operation({ retries: 3, // try 1 time , retry 3 times if needed, total = 4 mintimeout: 1 * 1000, // number of milliseconds before starting first retry maxtimeout: 15 * 1000 // maximum number of milliseconds between 2 retries }); operation.attempt(function(currentattempt) { lambda.createfunction(params, function(err, data) { if (operation.retry(err) && err.code === 'invalidparametervalueexception') return; callback(err); }); });
Comments
Post a Comment