0

I have a lambda function that verifies user credentials. Upon success it should call another lambda function, as a destination, that generates a token. When I test the first function, it is successful but it does not call the destination which is the other lambda function. It only gives me the success message from the first function.

Function one

exports.handler = function (event, context) {

    var id = event.email;
    var params = {
      TableName: "User",
      KeyConditionExpression: "#email = :email",
      ExpressionAttributeNames:{
        "#email": "email",
        },
      ExpressionAttributeValues: {
          ":email": {S: event.email},
      }

    };

    if (id && id !== '') {

        dynamo.query(params, function (err, data, callback) {
            if (err) {
                context.done(err);
            }
            else {
                var user = data.Items[0];
                if (user) {
                    var encryptedParams = {
                        CiphertextBlob: Buffer.from(user.password.B),
                    };
                    kms.decrypt(encryptedParams, function (err, decrypteddata) {
                        if (err) {
                            console.log(err, err.stack);
                            context.done(err);
                        }
                        else {
                            if(event.password == decrypteddata.Plaintext.toString()) {
                                console.log("User authenticated");
                            }
                        }
                    });
                }
            }
        });
    }
    else {
        return {
            statusCode: 400,
            body: "No email provided."
        }           
    }

};

Function two

exports.handler = async (event) => {

    var expires = moment().add('days', 7).valueOf();

    var token = jwt.encode({
      iss: event.email,
      exp: expires
    }, app.get('jwtTokenSecret'));

    const response = {
        token: token,
        express: expires,
        statusCode: 200
    };

    console.log("token granted");

    return response;
};
4
  • I don't see any code in the first function which would call the second? Also you should chain lambdas using SNS or Step functions. Calling them directly is not a very good practice when developing distributed system. Commented Mar 21, 2020 at 23:11
  • 1
    I think the OP is using AWS Lambda Destinations | AWS Compute Blog, which is rather new. Commented Mar 22, 2020 at 0:44
  • 1
    @Skip The above link mentions "Ensure that your Lambda function execution role includes access to the destination resource". What is in the IAM Role associated with the first Lambda function? Commented Mar 22, 2020 at 0:46
  • @JohnRotenstein Yes the lambda role attached to the first function has full access to lambda functions. I ran into the issue prior. Commented Mar 22, 2020 at 1:34

1 Answer 1

1

Your code doesn't seem to be indicating when a successful execution has been completed. Destinations needs the "OnSuccess" indicator to determine which destination to trigger. This is possibly why the second function is not being executed.

See: Lambda Destinations: What We Learned the Hard Way - CloudProse - Trek10 Blog

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.