Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codepipeline lambda action never complete

Well, my lambda function work's well according to the log's, but it never get completed in the codepipeline stage, I have already set permission to role for allow notificate pipeline ("codepipeline:PutJobSuccessResult", "codepipeline:PutJobFailureResult") and even set maximun time to 20sec but still not working (it actually ends at 800ms).

const axios = require('axios')
const AWS = require('aws-sdk');

const url = 'www.exampleurl.com'

exports.handler = async (event, context) => {
  const codepipeline = new AWS.CodePipeline();

  const jobId = event["CodePipeline.job"].id;
  const stage = event["CodePipeline.job"].data.actionConfiguration.configuration.UserParameters; 

  const putJobSuccess = function(message) {
    var params = {
        jobId: jobId
    };

    codepipeline.putJobSuccessResult(params, function(err, data) {
        if (err) {context.fail(err); }
        else     {context.succeed(message);}
    });
};

  const putJobFailure = function(message) {
    var params = {
        jobId: jobId,
        failureDetails: {
            message: JSON.stringify(message),
            type: 'JobFailed',
            externalExecutionId: context.invokeid
        }
    };
    codepipeline.putJobFailureResult(params, function(err, data) {
        if (err) console.log(err)
        context.fail(message);      
    });
};

try {
    await axios.post(url, { content: stage})

    putJobSuccess('all fine')

} catch (e) {
    putJobFailure(e)
}

};

like image 917
Gerson Flores Acuña Avatar asked Jan 23 '26 15:01

Gerson Flores Acuña


1 Answers

The root issue

Because nodeJS runs everything async by default, codepipeline.putJobSuccessResult is being run async. The issue seems to be that the Lambda function is finishing it's execution before codepipeline.putJobSuccessResult has a chance to complete.

The solution

Run codepipeline.putJobSuccessResult synchronously so that it is forced to complete before the response is returned to Lambda for the lambdaHandler.

const putJobSuccess = function(id) {
    //await sleep(60);
    console.log("Telling Codepipeline test passed for job: " + id)
    var params = {
        jobId: id
    };
    return codepipeline.putJobSuccessResult(params, function(err, data) {
        if(err) {
            console.error(err)
        } else {
            console.log(data) 
        }

    }).promise()
};

exports.lambdaHandler = async (event, context) => {

    ...   
    await putJobSuccess( jobId )
    return response

};



like image 86
Chart96 Avatar answered Jan 26 '26 07:01

Chart96



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!