Guest User

Untitled

a guest
May 23rd, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. var aws = require('aws-sdk');
  2. var ses = new aws.SES({region: 'us-east-1'});
  3. var iam = new aws.IAM({apiVersion: '2010-05-08'});
  4.  
  5. exports.handler = function (event, context) {
  6. console.log("Incoming: ", event);
  7. var message = "";
  8.  
  9. function AwsListGroupsFunction(err, data)
  10. {
  11. if (err) console.log(err, err.stack); // an error occurred
  12. else
  13. {
  14. //Get AWS Groups from AWS Account
  15. var dataCount = data.Groups.length-1;
  16. for (var key in data.Groups)
  17. {
  18. message+= data.Groups[key].GroupName
  19. // Then get all users associated to the GroupName
  20. var groupParams = { GroupName: data.Groups[key].GroupName};
  21. iam.getGroup(groupParams, AwsGetGroupFunction); // end of else for if iam.getGroup
  22.  
  23. if (key != dataCount)
  24. message += ', ';
  25. }
  26. }
  27. return message;
  28. };
  29.  
  30. function AwsGetGroupFunction(err, data) {
  31. if (err) console.log(err, err.stack); // an error occurred
  32. else{
  33. // successful response
  34. message+= "<br/>";
  35. for (var userKey in data.Users) {
  36. message += data.Users[userKey].UserName + ' and PasswordLastUsed: ' + data.Users[userKey].PasswordLastUsed + ' , ';
  37. }
  38.  
  39. }
  40. return message;
  41. };
  42.  
  43. function SendEmail(message){
  44. // Do this
  45. var destinationAddress = "myEmail@email.com";
  46. let subject = "My Subject";
  47. let emailBody =
  48. "My Body" +
  49. "<br/><br /><b>Message:</b><br/>" + message;
  50.  
  51. console.log(emailBody);
  52.  
  53. var eParams = {
  54. Destination: {
  55. ToAddresses: [destinationAddress]
  56. },
  57. Message: {
  58. Body: {
  59. Html: {
  60. Data: emailBody
  61. }
  62. },
  63. Subject: {
  64. Data: subject
  65. }
  66. },
  67. Source: "sourceemail@email.com"
  68. };
  69.  
  70. console.log('===SENDING EMAIL===');
  71. var email = ses.sendEmail(eParams, function(err, data)
  72. {
  73. if(err) console.log(err);
  74. else {
  75. console.log("===EMAIL SENT===");
  76. console.log(data);
  77. console.log("EMAIL CODE END");
  78. console.log('EMAIL: ', email);
  79. context.succeed(event);
  80. }
  81. });
  82. }
  83.  
  84.  
  85. // This is the only function called which calls other functions
  86. var params = {};
  87. iam.listGroups(params, AwsListGroupsFunction);
  88.  
  89. };
Add Comment
Please, Sign In to add comment