Last active
December 2, 2018 22:10
-
-
Save ac360/22ed25f8073961b73fec to your computer and use it in GitHub Desktop.
AWS Wrapper: Good or Bad?
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
// In the new ServerlessProviderAws class ----------------------------------- | |
// A class method wraps all aws-sdk requests | |
request(service, method, params, stage, region) { | |
let awsService = new AWS[service](_this.getCredentials(stage, region)); // Easily set stage/region-specific credentials | |
let req = awsService[method](params); // Form an aws-sdk "request" so we can listen to all AWS SDK events throughout the framework | |
// Listen to any events we want (e.g., VALIDATE_CREDENTIALS) | |
req.on('validate', function() { | |
console.log("validating credentials...") | |
}); | |
// Promisify the response | |
return new BbPromise(function(resolve, reject) { | |
req.send(function(err, data) { | |
if (err) return reject(err); // Can auto-retry 429 errors | |
return resolve(data); | |
}); | |
}); | |
} | |
// In an Action ----------------------------------- | |
// Providers are classes attached to root Serverless instance. | |
let aws = this.S.getProvider('aws'); // Get provider easily, no more aws config in Actions | |
aws.request('S3', 'getObject', params, stage, region).then().catch(); // Perform request | |
aws.sdk // Can still make aws-sdk accessible, but wouldn't recommend using that or you will lose above sugar | |
// Wrapping the aws-sdk could provide nice UX, centralized listening of ALL aws-sdk events and make it easy to handle multiple credentials behind the scenes. | |
// Worth it? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment