Skip to content

Instantly share code, notes, and snippets.

@mkol5222
Forked from janaz/get-aws-console-url.js
Created March 27, 2024 12:05
Show Gist options
  • Save mkol5222/0aff4361ce6ca0cdbd0b83460cb5744e to your computer and use it in GitHub Desktop.
Save mkol5222/0aff4361ce6ca0cdbd0b83460cb5744e to your computer and use it in GitHub Desktop.
Generate AWS console URL from the credentials stored in environment variables
/**
* This little program prints out the url to the AWS console
* generated from the local AWS credentials stored in environment variables:
* AWS_ACCESS_KEY_ID
* AWS_SECRET_ACCESS_KEY
* AWS_SESSION_TOKEN
*
* Steps:
* 1. Create a JSON object
* session = JSON.stringify({
* sessionId: <access key>,
* sessionKey: <secret access key},
* sessionToken: <session token>
* })
* 2. Send GET request to the following URL
* https://signin.aws.amazon.com/federation?Action=getSigninToken&Session=<session JSON object from the step above>
* 3. Use the "SigninToken" value returned by the above url to generate the console login URL
* https://signin.aws.amazon.com/federation?Action=login&Destination=<https://console.aws.amazon.com/>&SigninToken=<SigninToken retrieved in step 2>
*/
const https = require('https');
const session = JSON.stringify({
sessionId: process.env.AWS_ACCESS_KEY_ID,
sessionKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN
});
const baseUrl = 'https://signin.aws.amazon.com/federation'
const getSigninToken = (cb) => {
https.get(`${baseUrl}?Action=getSigninToken&Session=${encodeURIComponent(session)}`, (res) => {
res.setEncoding('utf8');
let response = '';
res.on('data', (chunk) => {
response = response + chunk;
});
res.on('end', () => {
cb(JSON.parse(response).SigninToken);
});
});
}
getSigninToken((signinToken) => {
const destination = 'https://console.aws.amazon.com/';
const url = `${baseUrl}?Action=login&Destination=${encodeURIComponent(destination)}&SigninToken=${encodeURIComponent(signinToken)}`;
console.log(url);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment