I have been trying to deploy a lambda function and then make it accessible through API-gateway. My java function is in JAVA and this is the documentation I followed to create a simple AWS lambda function in JAVA:
https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-pojo.html
This is how my function handler looks:
package lambda;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import lambda.axon.Path;
public class shortestPath implements RequestHandler<RequestClass, ResponseClass>{
public ResponseClass handleRequest(RequestClass request, Context context){
String inputString = request.inputString;
int steps = Path.stepsTo(inputString);
return new ResponseClass(steps);
}
}
This is this request class:
package lambda;
public class RequestClass {
String inputString;
public String getInputString() {
return inputString;
}
public void setInputString(String inputString) {
this.inputString = inputString;
}
public RequestClass(String inputString) {
this.inputString = inputString;
}
public RequestClass() {
}
}
And this the response class:
package lambda;
public class ResponseClass {
int steps;
public ResponseClass(int steps) {
this.steps = steps;
}
public ResponseClass() {
}
public int getSteps() {
return steps;
}
public void setSteps(int steps) {
this.steps = steps;
}
}
I deploy this on aws and configure AWS api gateway to trigger it , everything works fine when I hit the end-point given by the API gateway when I use postman(https://www.getpostman.com/)
But trying the same through a browser gives me a CORS error:
Access to XMLHttpRequest at 'https://<hash>execute-api.us-east-1.amazonaws.com/dev' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.
I tried enabling CORS in the API gateway console and then redeploying it:
This Stackoverflow post(Configure CORS response headers on AWS Lambda?) says if I am using a lambda-proxy I should have the headers in the handler response itself, I am not sure what a proxy is but how I can do that with my current implementation of the Lambda function i.e. include custom headers in my Response