InvalidParameterValueException: The role defined for the function cannot be assumed by Lambda

后端 未结 1 363
渐次进展
渐次进展 2020-12-16 19:08

I\'m using the AWS SDK for JavaScript and it is returning the following error when I try to create a Lambda function:

InvalidParameterValueException:

1条回答
  •  时光说笑
    2020-12-16 19:28

    This error happens when the role is invalid (which is not the case) or when you try to create the Lambda function just after the role creation. Amazon needs a few seconds to replicate your new role through all regions. So, the fix here is to wait a few seconds before creating the Lambda function.

    Solution - Example 1:

    var AWS = require('aws-sdk');
    var lambda = new AWS.Lambda();
    
    var params = {}; // define your parameters
    
    lambda.createFunction(params, function(err, data) {
        if (err && err.code === 'InvalidParameterValueException') {
    
            // try again after a few seconds
            setTimeout(function(){
                lambda.createFunction(params, callback);
            }, 10000);
        } else {
            callback(err, data);
        }
    });
    

    Solution - Example 2:

    Usually, waiting 5 seconds is enough, but it can also take a little more. For a more robust solution, you can use a retry module like this one.

    var AWS = require('aws-sdk');
    var retry = require('retry');
    var lambda = new AWS.Lambda();
    
    var params = {}; // define your parameters
    
    var operation = retry.operation({
        retries: 3,           // try 1 time and retry 3 times if needed, total = 4
        minTimeout: 1 * 1000, // the number of milliseconds before starting the first retry
        maxTimeout: 15 * 1000 // the maximum number of milliseconds between two retries
    });
    
    operation.attempt(function(currentAttempt) {
        lambda.createFunction(params, function(err, data) {
            if (operation.retry(err) && err.code === 'InvalidParameterValueException')
                return;
    
            callback(err);
        });
    });
    

    0 讨论(0)
提交回复
热议问题