13

How can we use AWS Kinesis in a web browser?

I'm interested in AWS Kinesis Stream and wondering if I can use it to send users' activity logs directly from their browsers to AWS.

AWS provides a JavaScript SDK which is executable in a web browser but, according to its document, the SDK requires credential information because of which, I think, it's not secure to use it in my use case.

Should I put proxy servers between their browsers and AWS Kinesis? Or is there any secure way to use AWS Kinesis in such a case?

k-kawa
  • 1,289
  • 2
  • 11
  • 18

1 Answers1

12

You have couple of options to use the JS SDK directly from the browsers of your users without embedding credentials in your code or force your users to log in into a service:

The first one is to use AWS Cognito. You can embed couple of line of code in your JS code that will identify the identity pool you want to use. On the service side you define the role for unauthenticated users to be able to write to Kinesis. You can see more details in this blog post: https://blogs.aws.amazon.com/javascript/post/Tx1F7FO6GDAIXD3/Authentication-with-Amazon-Cognito-in-the-Browser

The second option is to put API-Gateway between your users and the Kinesis stream. The gateway is a managed service that you can define as "open" with no authentication and the gateway can be the one with the permission to write to your Kinesis stream. The simplest way is to use a Lambda function that will be able also to transform/clean the events before putting them to the stream. See more details in the service documentations: http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html

Guy
  • 12,388
  • 3
  • 45
  • 67
  • 2
    Isn't the API Gateway extremly expensive for such use case ? Imagine you send 1 000 000 event weighting 1ko each : API Gateway will cost 3.5$ and Kinesis Firehose will cost $0.0038 (EU-west tarification). @Guy am I incorrectly calculating the costs? – Hugo Sep 28 '16 at 08:27
  • You should consider the alternatives. API-GW gives quite a few features that if you try to manage yourself, can cost you even more. For example, you can use it for billing if you are running a service that is creating analytics to other sites, as well as its edge locations integration. – Guy Oct 09 '16 at 04:26
  • Here is the example where SDK is used in browsr to write events to kinesis https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/kinesis-examples-capturing-page-scrolling.html But what concerns me is the size of the DSK: 1.5Mb. It will bloat the size of my app significantly, all of this just to send a simple http request – Dienow Jan 04 '18 at 18:37
  • You can remove parts that you don't need in the AWS SDK using the following instructions: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/building-sdk-for-browsers.html – Guy Jan 04 '18 at 23:46
  • @PetrusTheron, I hope that you mean reading it using server-side node application and not from a browser. You can use the AWS SDK for it: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Kinesis.html#getRecords-property – Guy Oct 12 '18 at 10:11