Arshad
Arshad

Reputation: 139

Lambda not able to execute the aws sdk API

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

Upvotes: 0

Views: 204

Answers (1)

Chris Williams
Chris Williams

Reputation: 35188

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.

Upvotes: 1

Related Questions