Last active
January 4, 2024 10:23
-
-
Save oneamitj/a648b88d82aca191d9f9cfe6458da126 to your computer and use it in GitHub Desktop.
Get list of services being used in an AWS Account and their corresponding cost.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("Before running this code make sure AWS account is setup or the env var is there.") | |
print(''' | |
export AWS_ACCESS_KEY_ID="AS******* | |
export AWS_SECRET_ACCESS_KEY="u8*****" | |
export AWS_SESSION_TOKEN="****" | |
''' | |
) | |
prompt = input("***Run the code (Y/n):") | |
if prompt == 'n': | |
exit(1) | |
# import os | |
# AWS_ACCESS = os.getenv("AWS_ACCESS_KEY_ID") | |
# AWS_SECRET = os.getenv("AWS_SECRET_ACCESS_KEY") | |
# AWS_TOKEN = os.getenv("AWS_SESSION_TOKEN") | |
import importlib.util | |
import subprocess | |
import sys | |
def check_and_install_package(package_name): | |
if not package_exists(package_name): | |
install_package(package_name) | |
return importlib.import_module(package_name) | |
else: | |
return importlib.import_module(package_name) | |
def package_exists(package_name): | |
package_spec = importlib.util.find_spec(package_name) | |
return package_spec is not None | |
def install_package(package_name): | |
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package_name]) | |
package_name = 'boto3' | |
check_and_install_package(package_name) | |
import boto3 | |
bill_client = boto3.client('ce') | |
start_date = '2023-12-01' | |
end_date = '2023-12-31' | |
# Define the time period | |
time_period = { | |
'Start': start_date, | |
'End': end_date | |
} | |
response = bill_client.get_cost_and_usage( | |
TimePeriod=time_period, | |
Granularity='MONTHLY', | |
Metrics=['UnblendedCost'], | |
GroupBy=[ | |
{ | |
'Type': 'DIMENSION', | |
'Key': 'SERVICE' | |
} | |
] | |
) | |
with open("aws_services_cost.csv", "w") as f: | |
f.write('"Service","Cost"\n') | |
for result in response['ResultsByTime']: | |
for group in result['Groups']: | |
f.write(f"\"{group['Keys'][0]}\",\"{group['Metrics']['UnblendedCost']['Amount']}\"\n") | |
print('Check file "aws_services_cost.csv" in your current directory.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("Before running this code make sure AWS account is setup or the env var is there.") | |
print(''' | |
export AWS_ACCESS_KEY_ID="AS******* | |
export AWS_SECRET_ACCESS_KEY="u8*****" | |
export AWS_SESSION_TOKEN="****" | |
''' | |
) | |
prompt = input("***Run the code (Y/n):") | |
if prompt == 'n': | |
exit(1) | |
# import os | |
# AWS_ACCESS = os.getenv("AWS_ACCESS_KEY_ID") | |
# AWS_SECRET = os.getenv("AWS_SECRET_ACCESS_KEY") | |
# AWS_TOKEN = os.getenv("AWS_SESSION_TOKEN") | |
import importlib.util | |
import subprocess | |
import sys | |
def check_and_install_package(package_name): | |
if not package_exists(package_name): | |
install_package(package_name) | |
return importlib.import_module(package_name) | |
else: | |
return importlib.import_module(package_name) | |
def package_exists(package_name): | |
package_spec = importlib.util.find_spec(package_name) | |
return package_spec is not None | |
def install_package(package_name): | |
subprocess.check_call([sys.executable, '-m', 'pip', 'install', package_name]) | |
package_name = 'boto3' | |
check_and_install_package(package_name) | |
import boto3 | |
bill_client = boto3.client("ce") | |
aws_services = [] | |
next_page_token = None | |
while True: | |
dimension_values = {} | |
if next_page_token: | |
dimension_values = bill_client.get_dimension_values( | |
TimePeriod={ | |
'Start': '2023-11-01', | |
'End': '2023-12-31' | |
}, | |
Dimension='SERVICE', | |
Context='COST_AND_USAGE', | |
NextPageToken=next_page_token) | |
else: | |
dimension_values = bill_client.get_dimension_values( | |
TimePeriod={ | |
'Start': '2023-01-01', | |
'End': '2023-12-31' | |
}, | |
Dimension='SERVICE', | |
Context='COST_AND_USAGE') | |
for dimension_value in dimension_values['DimensionValues']: | |
# print(dimension_value['Value']) | |
aws_services.append(dimension_value['Value']) | |
if 'NextPageToken' not in dimension_values: | |
break | |
next_page_token = dimension_values['NextPageToken'] | |
with open("aws_services.txt", "w") as f: | |
for item in aws_services: | |
f.write("%s\n" % item) | |
print('Check file "aws_services.txt" in your current directory.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment