Package: awscost
awscostCommand Line utility for Provides aws cost very easy to see.Supports python 3.7 or later.Install$pipinstallawscostExamplesshow costshow cost latest 5 months.$awscost
key2019-072019-082019-092019-102019-112019-12
--------------------------------------------------------------------------------------------
Total348.3497.498.46106.4767.257.84
EC2-Other34.2834.2833.234.2833.633.01
AmazonElasticComputeCloud-Compute17.1117.1116.5617.1117.131.47
AWSCostExplorer0.561.44
Tax25.87.227.279.726.120.69
AWSCloudTrail3.293.214.155.574.440.61
AWSKeyManagementService002.8440.35
AmazonCloudWatch1.50.01000.880.11
AmazonRoute530.10.10.10.10.10.1
AmazonSimpleStorageService0.250.240.270.450.40.05
AWSLambda000000AmazonDynamoDB00000AmazonElasticFileSystem000000AmazonSageMaker26635.2234.1135.220AmazonSimpleNotificationService000AmazonPolly0AmazonSimpleQueueService0show cost latest 3 days, group by SERVICE and operation.$awscost-gDAILY-p3-dSERVICE-dOPERATION
key2019-12-012019-12-022019-12-03
------------------------------------------------------------------------------------------------
Total2.872.12.87
GetCostAndUsage,AWSCostExplorer0.021.42
NatGateway,EC2-Other1.081.080.77
RunInstances,AmazonElasticComputeCloud-Compute0.550.550.37
None,AWSCloudTrail0.220.220.17
Unknown,AWSKeyManagementService0.130.130.09
Unknown,AmazonCloudWatch0.040.040.03
CreateVolume-Gp2,EC2-Other0.030.030.01
PutObject,AmazonSimpleStorageService0.020.020.01
NoOperation,Tax0.25
...support dimensionsAZINSTANCE_TYPELINKED_ACCOUNTOPERATIONPURCHASE_TYPESERVICEUSAGE_TYPEPLATFORMTENANCYRECORD_TYPELEGAL_ENTITY_NAMEDEPLOYMENT_OPTIONDATABASE_ENGINECACHE_ENGINEINSTANCE_TYPE_FAMILYREGIONBILLING_ENTITYRESERVATION_IDSAVINGS_PLANS_TYPESAVINGS_PLAN_ARNOPERATING_SYSTEMsee.https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetDimensionValues.htmlsupport filterawscost--filter'{"Not": {"Dimensions": {"Key": "RECORD_TYPE","Values": ["Credit", "Refund", "Upfront"]}}}'config file($HOME/.awscost)default:metrics:AmortizedCostfilter:Dimensions:Key:RECORD_TYPEValues:-Usage-Taxdiscount:filter:Dimensions:Key:RECORD_TYPEValues:-Credit-Refund-Upfrontec2:filter:Dimensions:Key:SERVICEValues:-"EC2-Other"-"AmazonElasticComputeCloud-Compute"You can exec command as below.$awscost--profileec2Various format(-t option)example, use -t tsv.default is simple.$awscost-dSERVICE-dOPERATION-ttsv
key01-2401-2501-2601-2701-2801-2901-3001-3102-0102-02
Total22222.022.012.012.012.51.67
EC2-Other,NatGateway1.081.081.081.081.081.081.081.081.080.9
AmazonElasticComputeCloud-Compute,RunInstances0.550.550.550.550.550.550.550.550.550.46
...support formatsimple(default)plaingithubgridfancy_gridpipeorgtbljiraprestopsqlrstmediawikimoinmoinyoutrackhtmllatexlatex_rawlatex_booktabstextiletsvSettingsaws auth support following.environment variablesprofile(use --aws-profile option.)instance profilePython APIfromawscost.cost_explorerimportCostExplorerfromdateutil.relativedeltaimportrelativedeltafromdatetimeimportdatetimestart=(datetime.today()-relativedelta(months=3)).strftime("%Y-%m-01")end=datetime.today().strftime("%Y-%m-01")cost_explorer=CostExplorer(granularity="MONTHLY",start=start,end=end,dimensions=["SERVICE"],metrics="UnblendedCost",threshold=1.0,).get_cost_and_usage_total_and_group_by()# return dict data.cost_explorer# =># {   'AWS CloudTrail': {'2019-11': 4.44, '2019-12': 6.17, '2020-01': 4.38},#     'AWS Lambda': {'2019-11': 0.0, '2019-12': 0.0, '2020-01': 0.0},# ...#     'Total': {'2019-11': 67.15, '2019-12': 72.22, '2020-01': 68.11}}matplotlib...importmatplotlib.pyplotaspltplt.figure(figsize=(25,15),dpi=100)plt.xlabel('month',fontsize=16)plt.ylabel('$',fontsize=16)plt.grid(True)fori,(service_name,v)inenumerate(cost_explorer.items()):left=list(v.keys())height=list(v.values())plt.plot(left,height,linewidth=2,label=service_name,marker='o')plt.legend(loc='best',fontsize=15,numpoints=5)plt.show()iam policy{"Version":"2012-10-17","Statement":[{"Sid":"xxxxxxxxx","Effect":"Allow","Action":["ce:GetCostAndUsage"],"Resource":"*"}]}CIinstall test package$./scripts/ci.shinstall-testtest$./scripts/ci.shrun-testflake8 and black and pytest.release pypi$./scripts/ci.shreleasegit tag and pypi release.