Codepipeline lambda action never complete

Viewed 640

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)
}

};

2 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

};



Whenever I see this issue, most of the time it is due to 'PutJobSuccessResult' never being invoked. The best way to check this is to go to CloudTrail > 'Event History' and look for 'Event name' = 'PutJobSuccessResult' during the time range you expect the Lambda function calling this API. Probably you will not find the 'PutJobSuccessResult', then please have a look at the code again and the Lambda execution logs in CloudWatch.

Related