How can I get an AWS billing for the previous day via Lambda function?

0 votes
exports.handler = (event, context, callback) => { // TODO implement

var AWS = require('aws-sdk');
var cloudwatch = new AWS.CloudWatch();
var params = {
              MetricName: 'EstimatedCharges',
              Namespace: 'AWS/Billing',
            };
            cloudwatch.listMetrics(params, function(err, data) {
              if (err) console.log(err, err.stack); // an error occurred
              else     console.log("success",JSON.stringify(data));           // successful response
            });

callback(null, 'Hello from Lambda');
};
Jun 18, 2018 in AWS by Luke cage
• 350 points
50 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I would suggest using the Cost Explorer API. You have the JS SDK to use:https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CostExplorer.html

Example as how you can use it

var params = {
  Filter: { /* Expression */
    And: [
      /* recursive Expression */,
      /* more items */
    ],
    Dimensions: {
      Key: AZ | INSTANCE_TYPE | LINKED_ACCOUNT | OPERATION | PURCHASE_TYPE | REGION | SERVICE | USAGE_TYPE | USAGE_TYPE_GROUP | RECORD_TYPE | OPERATING_SYSTEM | TENANCY | SCOPE | PLATFORM | SUBSCRIPTION_ID | LEGAL_ENTITY_NAME | DEPLOYMENT_OPTION | DATABASE_ENGINE | CACHE_ENGINE | INSTANCE_TYPE_FAMILY,
      Values: [
        'STRING_VALUE',
        /* more items */
      ]
    },
    Not: /* recursive Expression */,
    Or: [
      /* recursive Expression */,
      /* more items */
    ],
    Tags: {
      Key: 'STRING_VALUE',
      Values: [
        'STRING_VALUE',
        /* more items */
      ]
    }
  },
  Granularity: DAILY | MONTHLY,
  GroupBy: [
    {
      Key: 'STRING_VALUE',
      Type: DIMENSION | TAG
    },
    /* more items */
  ],
  Metrics: [
    'STRING_VALUE',
    /* more items */
  ],
  NextPageToken: 'STRING_VALUE',
  TimePeriod: {
    End: 'STRING_VALUE', /* required */
    Start: 'STRING_VALUE' /* required */
  }
};
costexplorer.getCostAndUsage(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});
answered Aug 10, 2018 by Priyaj
• 56,140 points

Related Questions In AWS

0 votes
1 answer

How do I add python libraries to an AWS lambda function for Alexa?

As it is described in the Amazon ...READ MORE

answered Oct 23, 2018 in AWS by Archana
• 4,090 points
119 views
0 votes
2 answers

How do I define the principal for an AWS policy statement?

Check with AWS Policy Documentations once, rest ...READ MORE

answered Jun 25, 2018 in AWS by Cloud gunner
• 4,240 points

edited Jun 26, 2018 by Cloud gunner 60 views
0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.