0

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.

3
  • If you're running this on Lambda are you able to verify that the Execution Role attached to the Lambda function has the codebuild:StartBuild permission? If you're running this locally via access keys (for example), then you would need to verify the associated IAM user has the same permission. Commented Jan 14, 2022 at 2:28
  • @Norman Yes it was working before, i had it on a s3 bucket on change trigger but now i want to trigger it using an update. Commented Jan 14, 2022 at 4:37
  • So, this Lambda is triggered by an S3 update event notification? Or is that how it was working previously? Commented Jan 14, 2022 at 5:07

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.