Rajiv Seelam • December 12, 2019
AWS Basics - Setup
awsYou need an AWS Account
- Go to aws website (Google for the link)
- Register and Login
- Go to "My Security Credentials" from top right navigation
- Go to "Users"
- Click "Add an User"
- Select programmatic access and go to next screen.
- Select "Attach existing policies" and select "Administrator Access" policy
- Go to final screen till "Create User"
- Note down: Access key ID and Secret access key
Setup your command line
Before we delve into the world of AWS. Let's make sure our system is fit and ready for development.
Do you have these installed?
- Node (check:
node -v
) - AWS CLI (check:
aws --version
)- If you don't have aws installed, Go Here
Is AWS Configured?
We have copied Access Key and Secret Key in above steps, we need to use them after all.
- Run
aws configure
-> It will take you through few steps, provide access and secret keys where necessary. - To test if our installed is working fine, let's try to get list of users using command:
aws iam list-users --output table
- It should show list of users.
We can configure multiple AWS Accounts (If you want)
- Run:
aws configure --profile <profile_name>
- Export it using:
export AWS_PROFILE=<profile_name>
Where are AWS credentials stored in my system?
- Go to your home directory:
cd ~
- Go to aws directory:
cd .aws
- You should see two files:
config
andcredentials
- If you check contents of these files, they both should have
[default]
configured.
For AWS NodeJS SDK to work properly, it's important for above to be setup properly.
Setup NodeJS SDK
Let's install aws-sdk and see if it works now.
- Create a directory:
mkdir setup-nodejs
- Change into that directory:
cd setup-nodejs
- Install aws-sdk:
npm init -y && npm install aws-sdk -S
- Create an empty file:
touch index.js
- Open setup-nodejs directory using a text editor like code or sublime.
Let's write our first piece of code, we will try to see which profile is being used, paste the following contents into index.js file:
//index.js
const AWS = require("aws-sdk");
AWS.config.getCredentials((err) => {
if (err) console.log(err.stack);
else console.log("Access Key:", AWS.config.credentials);
});
Run: node index.js
You would see response something similar to:
// response in command line
Access Key: SharedIniFileCredentials {
expired: false,
expireTime: null,
refreshCallbacks: [],
accessKeyId: 'XXXXXXXXXXXX',
sessionToken: undefined,
filename: undefined,
profile: 'xxxxxxxxx',
disableAssumeRole: true,
preferStaticCredentials: false,
tokenCodeFn: null,
httpOptions: null
}
Let's try to get list of users, replace index.js with following code:
//index.js
const AWS = require("aws-sdk");
var iam = new AWS.IAM();
iam.listUsers({}, (err, data) => {
if (err) {
console.log(err, err.stack);
} else {
console.log(data);
}
});
And you should see some response like following:
{
ResponseMetadata: { RequestId: 'ddddddxxx-81d6-445b-a953-6111ac85454f' },
Users: [
{
Path: '/',
UserName: 'access-xxx',
UserId: 'AIDAXTRL34HMEET6MW74W',
Arn: 'arn:aws:iam::45656546:user/access-xxx',
CreateDate: 2019-11-26T10:37:22.000Z,
Tags: []
}
]
}
If you got above responses, you are done with setup.