我试图让 lambda 在到达 lambda 函式中的点时触发 codebuild 函式,这是我用于 lamda 的当前代码:
console.log('Loading function');
const aws = require('aws-sdk');
const s3 = new aws.S3();
exports.handler = async (event, context) => {
        const codebuild = new aws.CodeBuild();
        let body = JSON.parse(event.body);
        let key = body.model;
        var getParams = {
            Bucket: 'bucketname', // your bucket name,
            Key: key   '/config/training_parameters.json' // path to the object you're looking for
        }
        if (key) {
            const objects = await s3.listObjects({
             Bucket: 'bucketname',
             Prefix: key   "/data"
            }).promise();
            console.log(objects)
            if (objects.Contents.length == 3) {
                console.log("Pushing")
                    await s3.getObject(getParams, function(err, data) {
                        if (err)
                            console.log(err);
                        if (data) {
                           let objectData = JSON.parse(data.Body.toString('utf-8')); 
                           const build = {
                                projectName: "projname",
                                environmentVariablesOverride: [
                                    {
                                        name: 'MODEL_NAME', 
                                        value: objectData.title, 
                                        type: 'PLAINTEXT',
                                    },
                                ]
                            };
                            console.log(objectData.title)
                            codebuild.startBuild(build,function(err, data){
                                if (err) {
                                    console.log(err, err.stack);
                                }
                                else {
                                    console.log(data);
                                }
                            });
                            console.log("Done with codebuild")
                        }
                }).promise();
            const message = {
                'message': 'Execution started successfully!',
            }
            return {
                'statusCode': 200,
                'headers': {'Content-Type': 'application/json'},
                'body': JSON.stringify(message)
            };
            }
        }
};
具体来说,这部分应该触发它:
                            codebuild.startBuild(build,function(err, data){
                                if (err) {
                                    console.log(err, err.stack);
                                }
                                else {
                                    console.log(data);
                                }
                            });
但它不是,它甚至在函式之后输出?我认为它与承诺/等待/异步有关,但找不到正确的解决方案?任何帮助,将不胜感激。
uj5u.com热心网友回复:
正如您所指出的,问题与承诺有关。像这样更改您的代码:
const result = await codebuild.startBuild(build).promise();
如果您为 CodeBuild 配置了 lambda 权限,它应该可以作业。
您可以在没有回呼函式的情况下以相同的方式更改 s3.getObject:
const file = await s3.getObject(getParams).promise();
console.log(file.Body);

 
							 
										
										 
										
										 
										
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										 
										
										
0 评论