2

I am using AWS Amplify to build some backend fun in AWS, and I have a Lambda function that is triggered from an update to DynamoDB. It doesn't need to return anything.

I keep getting errors in CloudWatch saying "SyntaxError: await is only valid in async function". Is there another way to run these async functions within the handler?

exports.handler = async (event) => {
  console.log(JSON.stringify(event, null, 2));
  event.Records.forEach(record => {
    console.log(record.eventID);
    console.log(record.eventName);
    console.log('DynamoDB Record: %j', record.dynamodb);

    if (record.eventName == "INSERT") {
      try {
        res = await axios.get("https://google.com", {});
        console.log(res);
      }
      catch(e){
        console.log(e);
      }
    }
  });
};

1 Answer 1

2

you are performing an await inside of a forEach loop which is it's own function state. You could try an inline async await function inside the if statement

(async function process() {
    try {
        res = await axios.get("https://google.com", {});
        console.log(res);
    }
    catch(e){
        console.log(e);
    }
}())
Sign up to request clarification or add additional context in comments.

2 Comments

Doh! That was definitely the issue. However, I noticed a new bug get introduced by changing to this. I can no longer get values from the dns.resolve function - any variable it modifies is not seen by my function now. It worked in foreach, but not anymore
I would have to see the code to troubleshoot where the issue is.

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.