Reputation: 42604
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?
Upvotes: 4
Views: 5708
Reputation: 5642
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.)
Upvotes: 3
Reputation: 3719
You definitely DON'T want to save your AWS credentials in the file. A better way to do it would be to save the values in an environment variable in the environment your app is going to be running on. If you have the AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
environment variables set, the SDK will automatically load them, and you don't have to worry about it in your code, as described here: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html Depending on which services you are using, these values might already be set. If not, it should be pretty easy to create a user or role that has permissions for whatever operations you are using the SDK for.
Upvotes: 0
Reputation: 891
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>
});
Upvotes: 3