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
1,629 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
});

I hope it helps!

For more details, join AWS course and learn from the industry experts.

answered Aug 10, 2018 by Priyaj
• 58,090 points

Related Questions In AWS

0 votes
2 answers

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

If you get lucky (it depends what ...READ MORE

answered Nov 3, 2020 in AWS by anonymous
15,131 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, 2020 in AWS by MD
• 95,440 points
2,215 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 26, 2018 in AWS by Cloud gunner
• 4,670 points

edited Jun 26, 2018 by Cloud gunner 881 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,640 points
2,908 views
0 votes
1 answer
0 votes
1 answer
+2 votes
3 answers
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP