Created
August 22, 2023 13:59
-
-
Save emsesc/ede477d0462b50514c9abdfc8594a882 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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