2017-09-03 64 views

回答

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运行代码。