Please advise how to call lambda function from frontend and what to set to avoid cors errors
Which Category is your question related to? Amplify function & API Gateway
Amplify CLI Version not relevant
What AWS Services are you utilizing? Functions and REST API
Provide additional details e.g. code snippets I have added a function to my amplify project and added a path to it by updating my REST api.
Using the following code snippet: const apiName = "myrestapi"; const path = "/testlambda"; const init = { body: { data: input }, headers: {}, response: true, };
await API.post(apiName, path, init);
The code in the function runs but it never returns my response. Instead I keep getting a cors error in the concole:
Access to XMLHttpRequest at 'https://xxxxx.execute-api.ap-southeast-2.amazonaws.com/develop/testlambda' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Can someone please advise the correct code / settings I need in order to not get the cors errors? I have tried enabling CORS through API Gateway as per the documentation but still get the error.
So after some debugging and testing, I discovered that the issue is not CORS related whatsoever. I discovered that it is due to the function I am calling through the API Gateway running for longer than 30 seconds which is the timeout set for API calls which then gives a false CORS error (hint: it should give a timeout error not a false and misleading CORS error!!!)
So the question becomes: how do you run potentially long running lambda functions from within the amplify framework / invoke them from the front-end app???? Is there a parameter that can be set to extend the timeout on the API Gateway call?
Can you guys also please advise / or add a way to invoke lambda function in amplify without having to use a rest api or provide a way to extend the timeout on api calls? The current functionality available does not cater for the requirement to invoke potentially long running functions that process a lot of data. The only option I see for this is to trigger the lambda function somehow which isn't always an option when a system needs to process a lot of data before storing it. 29 seconds for a timeout isn't always enough
@frankclaassen You can modify the generated CloudFormation file located in amplify/backend/function/<resourc-name> directory and increase the default timeout from 30 seconds to a max of 15 mins. Also, we released Fargate task support a couple of weeks ago to account for long running tasks - please go through our docs here - https://docs.amplify.aws/cli/usage/containers
@kaustavghosh06 hi, increasing the lambda timeout doesn't help when API gateway has a 29 second timeout limit. The API gateway call is the problem with timing out so I need a way to directly invoke the lambda function from within amplify
From my understanding, amplify API is a wrapper over aws-sdk but it does not have the Invoke function wrapped for use???
Any comment on how to access lambda directly without API gateway???
For anyone coming across this and want to know how to call Lambda from Amplify:
Install the aws-sdk, follow the docs on AWS for calling lambda from nodejs, update iam roles in AWS to give required permissions for invoking lambda functions
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.