How to get AWS monthly invoice PDF using AWS API?

断了今生、忘了曾经 提交于 2020-07-05 07:51:07

问题


Is there a way to programmatically download the PDF monthly invoice the accounting department ask me every month?

I can get them from AWS Console (eg. https://console.aws.amazon.com/billing/home?region=eu-west-3#/bills?year=2019&month=3) Where there is a link to the invoice.

The moment I click to download the invoice, I can see HTTP requests to the following URL: https://console.aws.amazon.com/billing/rest/v1.0/bill/invoice/generate?generatenew=true&invoiceGroupId=_SOME_ID_&invoicenumber=_SOME_ID_

Then a final request to the URL that actually serves the PDF file: https://console.aws.amazon.com/billing/rest/v1.0/bill/invoice/download?invoiceGroupId=_SOME_ID_&invoicenumber=_SOME_ID_

I cannot find documentation on the AWS API to fetch such invoice document (there is some for billing reports and other stuff, but none for the "official" document) so I start to ask myself if it is even available?

Before going scrapping the AWS Console (via Scrapy, Selenium, Puppeteer) I ask the community ;)

NB: I know AWS can send the invoice PDF via e-mail but I rather fetch it directly from AWS instead of fetching from an IMAP/POP e-mail server.


回答1:


You can use aws cli or aws sdk to get the data in json format. And then convert the json into pdf (not covered in this answer).

AWS cli

aws cli provides get-cost-and-usage command. By fiddling with parameters you can get the output that matches the one that is produced by billing invoice.

Example usage of this command:

    aws ce get-cost-and-usage \
        --time-period Start=2019-03-01,End=2019-04-01 \
        --granularity MONTHLY \
        --metrics "BlendedCost" "UnblendedCost" "UsageQuantity" \
        --group-by Type=DIMENSION,Key=SERVICE

Which produces the following output

{
  "GroupDefinitions": [
    {
      "Type": "DIMENSION",
      "Key": "SERVICE"
    }
  ],
  "ResultsByTime": [
    {
      "TimePeriod": {
        "Start": "2019-03-01",
        "End": "2019-04-01"
      },
      "Total": {},
      "Groups": [
        {
          "Keys": [
            "AWS Budgets"
          ],
          "Metrics": {
            "BlendedCost": {
              "Amount": "3.0392156805",
              "Unit": "USD"
            },
            "UnblendedCost": {
              "Amount": "3",
              "Unit": "USD"
            },
            "UsageQuantity": {
              "Amount": "155",
              "Unit": "N/A"
            }
          }
        },
        {
          "Keys": [
            "AWS CloudTrail"
          ],
          "Metrics": {
            "BlendedCost": {
              "Amount": "0",
              "Unit": "USD"
            },
            "UnblendedCost": {
              "Amount": "0",
              "Unit": "USD"
            },
            "UsageQuantity": {
              "Amount": "720042",
              "Unit": "N/A"
            }
          }
        },
...

AWS SDK

You can also get the same kind of data programmatically. The easiest way to do it is to use aws sdk. Refer to the documentation of the sdk you want to use. For example information on this functionality for python sdk can be found here.



来源:https://stackoverflow.com/questions/55849494/how-to-get-aws-monthly-invoice-pdf-using-aws-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!