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
• 360 points
587 views

1 answer to this question.

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
• 57,700 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,150 points
6,726 views
0 votes
1 answer

how to get the list of aws services i am used in aws my account by using the lambda function

Hi@shalk, You can create a session in your ...READ MORE

answered Sep 24 in AWS by MD
• 66,980 points
45 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,650 points

edited Jun 26, 2018 by Cloud gunner 299 views
0 votes
1 answer

How can I just increase the size of my root disk on AWS EC2 for use with Elastic Beanstalk?

This can be done using the following ...READ MORE

answered Nov 12, 2018 in AWS by Archana
• 5,600 points
878 views
0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers