MaddieP
MaddieP

Reputation: 23

Lambda not triggering codebuild to run?

I am trying to have lambda trigger a codebuild function when it hits the point within the lambda function, here is the current code im using for 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)
            };
            }
        }
};

Specifically this part should trigger it:

                            codebuild.startBuild(build,function(err, data){
                                if (err) {
                                    console.log(err, err.stack);
                                }
                                else {
                                    console.log(data);
                                }
                            });

But its not, it even outputs after the function? Im thinking its something to do with promises/await/async but cant find the right solution? Any help would be appreciated.

Upvotes: 0

Views: 824

Answers (1)

As you pointed out the problem is related to promises. Change your code like this:

const result = await codebuild.startBuild(build).promise();

And if you configured your lambda permissions for CodeBuild it should work.

You can change your s3.getObject the same way without the callback function:

const file = await s3.getObject(getParams).promise();
console.log(file.Body);

Upvotes: 2

Related Questions