最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - InvalidParameterValueException: The role defined for the function cannot be assumed by Lambda - Stack Overflow

programmeradmin3浏览0评论

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

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

I've double-checked my role and it is perfectly valid. However, I'm still unable to create the Lambda function.

My role trust relationship is:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "lambda.amazonaws"
                ]
            },
            "Action": [
                "sts:AssumeRole"
            ]
        }
    ]
}

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

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

I've double-checked my role and it is perfectly valid. However, I'm still unable to create the Lambda function.

My role trust relationship is:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": [
                    "lambda.amazonaws.com"
                ]
            },
            "Action": [
                "sts:AssumeRole"
            ]
        }
    ]
}
Share Improve this question asked May 28, 2016 at 19:32 ZanonZanon 30.8k21 gold badges118 silver badges126 bronze badges 1
  • I've posted this self-answered question since I've spent a very long time to find the problem. I hope that it may be useful to others. – Zanon Commented May 28, 2016 at 19:34
Add a comment  | 

1 Answer 1

Reset to default 18

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)

  1. 暂无评论