我正在使用https://github.com/Tim-B/grunt-aws-lambda创建一个zip文件以部署到lambda但是在部署到aws lambda时我需要先在amazon控制台中创建该函数。我们可以使用grunt而不是amazon控制台创建一个函数吗?谢谢。
答案 0 :(得分:1)
您可以使用AWS JavaScript SDK for Lambda从grunt创建函数。
使用createFunction方法,如下所示。
/* This example creates a Lambda function. */
var params = {
Code: {
},
Description: "",
FunctionName: "MyFunction",
Handler: "souce_file.handler_name", // is of the form of the name of your source file and then name of your function handler
MemorySize: 128,
Publish: true,
Role: "arn:aws:iam::123456789012:role/service-role/role-name", // replace with the actual arn of the execution role you created
Runtime: "nodejs4.3",
Timeout: 15,
VpcConfig: {
}
};
lambda.createFunction(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
/*
data = {
CodeSha256: "",
CodeSize: 123,
Description: "",
FunctionArn: "arn:aws:lambda:us-west-2:123456789012:function:MyFunction",
FunctionName: "MyFunction",
Handler: "source_file.handler_name",
LastModified: "2016-11-21T19:49:20.006+0000",
MemorySize: 128,
Role: "arn:aws:iam::123456789012:role/service-role/role-name",
Runtime: "nodejs4.3",
Timeout: 123,
Version: "1",
VpcConfig: {
}
}
*/
});
注意:您可以使用代码填充代码参数或使用添加属性来引用压缩并上传到S3的代码。
E.g。
Code: { /* required */
S3Bucket: 'STRING_VALUE',
S3Key: 'STRING_VALUE',
S3ObjectVersion: 'STRING_VALUE',
ZipFile: new Buffer('...') || 'STRING_VALUE'
},
还要确保向IAM用户和setup JavaScript SDK credentials提供所需的权限才能运行代码。