Can someone explain how to fix a missing config error with Node.js? I\'ve followed all the examples from the aws doc page but I still get this error no matter what.
I have gone through your code and here you are connecting to AWS services before setting the region, so i suggest you to update the region first and then connect to services or create instance of those as below -
var express = require('express');
var router = express.Router();
var AWS = require('aws-sdk');
AWS.config.update({region:'us-east-1'});
var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();
var bucketName = 'my-bucket';
If you work with AWS CLI, you probably have a default region defined in ~/.aws/config. Unfortunately AWS SDK for JavaScript does not load it by default. To load it define env var
AWS_SDK_LOAD_CONFIG=1
See https://github.com/aws/aws-sdk-js/pull/1391
This may not be the right way to do it, but I have all my configs in a separate JSON file. And this does fix the issue for me
To load the AWS config, i do this:
var awsConfig = config.aws;
AWS.config.region = awsConfig.region;
AWS.config.credentials = {
accessKeyId: awsConfig.accessKeyId,
secretAccessKey: awsConfig.secretAccessKey
}
config.aws is just a JSON file.
You could create a common module and use it based on the region you want to
var AWS = require('aws-sdk')
module.exports = {
getClient: function(region) {
AWS.config.update({ region: region })
return new AWS.S3()
}
}
and consume it as,
var s3Client = s3.getClient(config.region)
the idea is to Update AWS config before instantiating s3
I'm impressed this hasn't been posted here yet.
Instead of setting the region with AWS.config.update()
, you can use
const s3 = new AWS.S3({
region: "eu-central-1",
});
to make it instance specific.
You can resolve this issue right in your project directory.
npm i -D dotenv
..env
file in root of our project.AWS_SDK_LOAD_CONFIG=1
in that .env
file.const {config} = require("dotenv");
in the same file where you configure connection to DynamoDB.config()
before you new AWS.DynamoDB()
.P.S. As someone have mentioned before, problem is that Node doesn't get data from your aws.config file