Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var aws = require('aws-sdk');
- var ses = new aws.SES({region: 'us-east-1'});
- var iam = new aws.IAM({apiVersion: '2010-05-08'});
- exports.handler = function (event, context) {
- console.log("Incoming: ", event);
- var message = "";
- function AwsListGroupsFunction(err, data)
- {
- if (err) console.log(err, err.stack); // an error occurred
- else
- {
- //Get AWS Groups from AWS Account
- var dataCount = data.Groups.length-1;
- for (var key in data.Groups)
- {
- message+= data.Groups[key].GroupName
- // Then get all users associated to the GroupName
- var groupParams = { GroupName: data.Groups[key].GroupName};
- iam.getGroup(groupParams, AwsGetGroupFunction); // end of else for if iam.getGroup
- if (key != dataCount)
- message += ', ';
- }
- }
- return message;
- };
- function AwsGetGroupFunction(err, data) {
- if (err) console.log(err, err.stack); // an error occurred
- else{
- // successful response
- message+= "<br/>";
- for (var userKey in data.Users) {
- message += data.Users[userKey].UserName + ' and PasswordLastUsed: ' + data.Users[userKey].PasswordLastUsed + ' , ';
- }
- }
- return message;
- };
- function SendEmail(message){
- // Do this
- var destinationAddress = "myEmail@email.com";
- let subject = "My Subject";
- let emailBody =
- "My Body" +
- "<br/><br /><b>Message:</b><br/>" + message;
- console.log(emailBody);
- var eParams = {
- Destination: {
- ToAddresses: [destinationAddress]
- },
- Message: {
- Body: {
- Html: {
- Data: emailBody
- }
- },
- Subject: {
- Data: subject
- }
- },
- Source: "sourceemail@email.com"
- };
- console.log('===SENDING EMAIL===');
- var email = ses.sendEmail(eParams, function(err, data)
- {
- if(err) console.log(err);
- else {
- console.log("===EMAIL SENT===");
- console.log(data);
- console.log("EMAIL CODE END");
- console.log('EMAIL: ', email);
- context.succeed(event);
- }
- });
- }
- // This is the only function called which calls other functions
- var params = {};
- iam.listGroups(params, AwsListGroupsFunction);
- };
Add Comment
Please, Sign In to add comment