Is CORS configuration needed for invoking Hello Lambda function from aws sdk javascript on static web page?

Can someone guide me. I am new to AWS Lambda. I am trying to access a Lambda function from a plain html page using aws sdk and I am getting CORS error. Is Cors configuration needed as my html is my desktop only and I am not accessing anything from server or s3 bucket.

Error in browser: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource athttps://lambda.us-west-2.amazonaws.com/2015-03-31/functions/TestHandler/invocations. (Reason: CORS request failed).

My Lambda Function: - created using Hello world template using Java 8.

public class TestLambda {
    public String myHandler(int myCount, Context context) {
        LambdaLogger logger = context.getLogger();
        logger.log("received:" + myCount);
        return String.valueOf(myCount);
    }
}

My Javascript

var lambda = new AWS.Lambda({apiVersion: '2015-03-31', region:'us-west-2', accessKeyId: "xxx", 
    secretAccessKey: 'xxx/xxx'} ;

 lambda.invoke({      
      FunctionName: 'TestHandler',
      Payload: JSON.stringify('111111')
    }, function(err, data){
    alert("function returned");
      if(err) {
      alert("It was an error" + err.stack);

      } else {
      alert("it was success");        
        alert("result is " + data);
      }
    });

I get the alert It was an error.

thanks in advance.

I don't think the javascript SDK was meant for client side(in the browser). I think it was intended to use with node.js applications - where you wouldn't get CORS errors. You definitely wouldn't want your accessKey and secretAccessKey to be exposed in HTML. Anyway, the typical way to expose Lambda to a browser is to use API Gateway. The basic setup is done here:http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html

You will also need to setup CORS for your api once its setup by following these steps:http://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html

I created a blog post which shows how to setup Lambda with API Gateway to create a Slack-Bot style integration:http://www.ryanray.me/serverless-slack-integrations

What Others Are Reading