1

I'm trying to implement DynamoDB in a Javascript file. I tried out multiple tutorials in succession to no avail. All I'm trying to do is make a query from within my Javascript code (which is being run in an HTML page). Unfortunately, my code doesn't even get up to that. It throws me errors when I try to require AWS-SDK. I installed aws-sdk with Node.js. After being confused by multiple tutorials, I ended up with the following code:

HTML:

<!DOCTYPE html>
<html>

<head>
<script src="http://requirejs.org/docs/release/2.2.0/r.js"></script>
<script src="boom.js"></script>
</head>

<body></body>

</html>

JavaScript (boom.js):

require(['aws-sdk'], function (foo) {

    var CONF = require("./super_secure_conf.json");
    var AWS = require("aws-sdk");

    function init(){
        AWS.config = new AWS.Config({
            access_key_id: CONF.AWS_ACCESS_KEY_ID,
            secretAccessKey: CONF.AWS_SECRET_ACCESS_KEY,
            region: "us-east-1"
        });
        DynamoDB = new AWS.DynamoDB();

    }
});    

The file "super_secret_conf.json" is a JSON file containing my AWS Credentials. I tried storing the credentials at ~/.aws/credentials previously, but that wasn't working. (Should credentials be a folder or file? I had tried saving my credentials in a blank file – without any extension. Just thought I'd mention.) So I followed another tutorial, which said to use the JSON method (and I am aware that it is very insecure) which is what you see here. I still get an error though:

Error: Module name "super_secure_conf.json" has not been loaded yet for context: _. Use require([])

All and any help is greatly appreciated.

1 Answer 1

1

You are requiring AWS twice, calling it foo the first time. And you aren't waiting for the callback from your config.json require call (or even passing it a callback function). I believe your code needs to change to look like this:

require(['aws-sdk', "./super_secure_conf.json"], function (AWS, CONF) {    
    function init(){
        AWS.config = new AWS.Config({
            access_key_id: CONF.AWS_ACCESS_KEY_ID,
            secretAccessKey: CONF.AWS_SECRET_ACCESS_KEY,
            region: "us-east-1"
        });
        DynamoDB = new AWS.DynamoDB();    
    }
});    

However I'm not sure if the path "./super_secure_conf.json" is actually going to work. That looks like a path you would use for loading a file in a server-side NodeJS application, not a browser-side JavaScript application.

Note that the reason you have to load your config this way is because the ~/.aws/credentials method of loading an AWS config is not going to work for a JavaScript app running in a browser. I think you've been reading NodeJS tutorials which aren't going to translate perfectly to JavaScript in the browser. I would highly recommend you start by looking through the documentation for AWS SDK for JavaScript in the Browser, and in particular read the page on Configuring the SDK in the Browser.

Sign up to request clarification or add additional context in comments.

2 Comments

I can't thank you enough. Your answer solved half the problem, but it still required me to configure the IdentityPoolId. Other than that, thank you so much for your help!!!
@Mark B, Probably I should post a new question.While a prepare to post a proper new question, I ask you here: I Implemented a sample AWS Serverless architecture flow with Dynamodb-Lamda(for DAO )-API Gateway-S3 as website. I want to secure the API. I read the related documentation. Am wanting to Implement IAM Authentication. Presently I am using Javascript SDK. I tried to use ~/.aws/credentials file but could not, your answer above informs me that I cannot use this method. Please tell how to externalize IAM User credentials, and how would the Javascript SDK know to identify my AWS Account.

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.