0

I am trying to create a lambda function which will check if a particular repository exist in codecommit. Lamda service role is having admin priviledge. Below is the code. The lambda is unable to call getRepository method. It is niether giving any exception nor passing. Any help on this? console.log("Before calling cc") This is last printed statement. After that I am not getting any success or error log.

const CloudFormation = require('aws-sdk/clients/cloudformation');
const Codecommit = require('aws-sdk/clients/codecommit');
exports.handler = async (event) => {
    try{
        console.log("event",event);
    console.log("event",JSON.stringify(event));
    var repositoryName = event.detail.repositoryName;
    var cfn = new CloudFormation({ 
    region: "ap-northeast-1"
    });
    var cc = new Codecommit({ 
    region: "ap-northeast-1"
    });
    const stackName = repositoryName+"-infra-stack";
    var cloneUrl;
    console.log("RepositoryName"+repositoryName);
    console.log("StackName"+stackName);
    var codeCommitParam = {
        repositoryName: repositoryName
      };
    try{
        console.log("Before calling cc")
        cc.getRepository(codeCommitParam, function(err, data) {
        if (err){
            console.log(err, err.stack);
        }else {
            console.log(data.repositoryMetadata.cloneUrlHttp);
            cloneUrl=data.repositoryMetadata.cloneUrlHttp; 
            console.log("Clone url "+cloneUrl);        
            checkStackDescription();
        }   
      });
    }catch(error){
        console.log(error);
    }
}
4
  • Do you have a sample event? Commented Aug 5, 2020 at 16:38
  • what timeout for the lambda? Commented Aug 5, 2020 at 16:39
  • yes.. This is the event { version: '0', id: '***', 'detail-type': 'CodeCommit Repository State Change', source: 'aws.codecommit', account: '***', time: '2020-08-05T07:40:51Z', region: '***', resources: [ '*' ], detail: { callerUserArn: '***', commitId: *** event: 'referenceUpdated', oldCommitId: '****', referenceFullName: 'refs/heads/master', referenceName: 'master', referenceType: 'branch', repositoryId: '***', repositoryName: 'custom-ui-pipeline' } } Commented Aug 5, 2020 at 16:40
  • @NghiaDo Timeout is 4 mins. Commented Aug 5, 2020 at 16:45

1 Answer 1

1

I believe this is coming down to the JavaScript in the Lambda being invoked asynchronously so the Lambda is finishing invoking before the callback processes the response.

Try updating to use this synchronously by updating to the below syntax.

console.log("Before calling cc")
let result = await cc.getRepository(codeCommitParam).promise();

console.log(result);

Be aware that result could either be an error or valid response.

Sign up to request clarification or add additional context in comments.

6 Comments

Try is properly closed with a Catch later in the code. Regarding lambda timeout, i have increased it to 4 mins. Also, Lambda is not within any VPC.
Hmm ok, I think its actually the async running on your Lambdas NodeJS callback. Updated to make it synchronous and I can view in the logs. Updated answer
Yes that was the issue. Thanks for helping me out here.:)
No problem, glad I could help :)
instead of using await, I removed top async. Hope this will not create any problem in lambda.
|

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.