Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass aws credential and config to aws sdk in nodejs programmatically?

I am building an environment which let users to run their nodejs code. It is pretty much like what Code Pen or runit does. If users need to run aws sdk code in the environment, I don't know how to handle their credentials and configs. I know aws nodejs sdk has a method config() which I can pass all configuration in. But usually developers aws credentials and config are saved in ~/.aws/credential and ~/.aws/config files. If I ask users to upload these files into the environment, how can I convert them into a parameter can be read by aws sdk? Is there a easy way to do or I have to manually parse these files?

like image 733
Joey Yi Zhao Avatar asked Sep 09 '25 14:09

Joey Yi Zhao


2 Answers

You can do it like this:

const AWS = require('aws-sdk');

// config.json
{"accessKeyId": <YOUR_ACCESS_KEY_ID>, "secretAccessKey": <YOUR_SECRET_ACCESS_KEY>, "region": "us-east-1" }


AWS.config.loadFromPath('./config.json');

You can also do it like this:

var AWS = require("aws-sdk");

AWS.config.update({
  region: "us-west-2",
  "accessKeyId": <YOUR_ACCESS_KEY_ID>, 
  "secretAccessKey": <YOUR_SECRET_ACCESS_KEY>
});
like image 62
Carlos Jafet Neto Avatar answered Sep 12 '25 04:09

Carlos Jafet Neto


Here's an example of hard-coding credentials for the Simple Email Service (SES) in AWS SDK v3:

let { SES } = require("@aws-sdk/client-ses");

const ses = new SES({
  apiVersion: "2010-12-01",
  region: "us-west-1",
  credentials: {
    accessKeyId: ".....", 
    secretAccessKey: ".....",
  },
});

I'm guessing it's basically the same for other AWS constructors - i.e. just add that credentials property with the access keys in it.

(Note that hard-coding your credentials like this convenient, but can be a bad idea for professional/production applications where security is important. See the links at the bottom of this page for several other approaches that are more secure.)

like image 26
joe Avatar answered Sep 12 '25 04:09

joe