Skip to content

Instantly share code, notes, and snippets.

@emsesc
Created August 22, 2023 13:59
Show Gist options
  • Save emsesc/ede477d0462b50514c9abdfc8594a882 to your computer and use it in GitHub Desktop.
Save emsesc/ede477d0462b50514c9abdfc8594a882 to your computer and use it in GitHub Desktop.
const AWS = require('aws-sdk');
// Configure the AWS SDK with your IAM user's credentials
AWS.config.update({
accessKeyId: 'YOUR_ACCESS_KEY_ID',
secretAccessKey: 'YOUR_SECRET_ACCESS_KEY',
region: 'us-east-1' // Replace with your desired AWS region
});
const sts = new AWS.STS();
const params = {
RoleArn: 'arn:aws:iam::YOUR_ACCOUNT_ID:role/YOUR_ROLE_NAME',
RoleSessionName: 'YourSessionName' // Provide a session name
};
sts.assumeRole(params, (err, data) => {
if (err) {
console.error('Error assuming role:', err);
return;
}
// Temporary security credentials
const { AccessKeyId, SecretAccessKey, SessionToken } = data.Credentials;
// Use these temporary credentials to access S3
const s3 = new AWS.S3({
accessKeyId: AccessKeyId,
secretAccessKey: SecretAccessKey,
sessionToken: SessionToken
});
// Now you can use the S3 client to interact with S3
s3.listBuckets((s3Err, s3Data) => {
if (s3Err) {
console.error('Error listing S3 buckets:', s3Err);
} else {
console.log('S3 Buckets:', s3Data.Buckets);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment