问题描述
我正在使用适用于JavaScript的AWS开发工具包,并且它正在返回尝试创建Lambda函数时出现以下错误:
I'm using the AWS SDK for JavaScript and it is returning the following error when I try to create a Lambda function:
我已经仔细检查了我的角色,这是完全有效的.但是,我仍然无法创建Lambda函数.
I've double-checked my role and it is perfectly valid. However, I'm still unable to create the Lambda function.
我的角色信任关系是:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
},
"Action": [
"sts:AssumeRole"
]
}
]
}
推荐答案
当角色无效(不是这种情况)或您尝试创建Lambda函数紧随其后时,会发生此错误.角色创建.亚马逊需要几秒钟的时间才能在所有地区复制您的新角色.因此,此处的解决方法是在创建Lambda函数之前先等待几秒钟.
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.
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);
}
});
解决方案-示例2:
通常,等待5秒钟就足够了,但也可能花费更多时间.对于更强大的解决方案,您可以使用诸如此这样的重试模块.
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);
});
});
这篇关于InvalidParameterValueException:Lambda无法承担为函数定义的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!