-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosts.ps1
126 lines (111 loc) · 6.1 KB
/
costs.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#####################################################################################################
#
# AWS Cost Query System
# ---------------------
# Description:
# Derives monthly costs from AWS API Mechanisms.
#
# Accepts parameters or will prompt:
# -profileName ( your AWS profile name )
# -startDate ( not setting will default to the start of the current month)
# -endDate ( not setting will default to the current date )
# -prompt y/n ( this will prompt you for the date ranges )
# -outputType table/json/yaml/text ( this will default to table if left out )
#
# Version: 1.0.1
# Author: Rick Trotter
#
#####################################################################################################
# Load Default Settings from command prompt if available
param (
[string]$profileName,
[string]$startDate,
[string]$endDate,
[string]$outputType = "table",
[string]$prompt = "n"
)
## set globals
$global:startDate=$startDate
$global:endDate=$endDate
## Clear terminal and write banner
Clear-Host
Write-Host "#################################################################################" -ForegroundColor Green
Write-Host " █████╗ ██╗ ██╗███████╗ ██████╗ ██╗ ██╗██████╗ ██████╗ ███████╗████████╗" -ForegroundColor Green
Write-Host "██╔══██╗██║ ██║██╔════╝ ██╔══██╗██║ ██║██╔══██╗██╔════╝ ██╔════╝╚══██╔══╝" -ForegroundColor Green
Write-Host "███████║██║ █╗ ██║███████╗ ██████╔╝██║ ██║██║ ██║██║ ███╗█████╗ ██║ " -ForegroundColor Green
Write-Host "██╔══██║██║███╗██║╚════██║ ██╔══██╗██║ ██║██║ ██║██║ ██║██╔══╝ ██║ " -ForegroundColor Green
Write-Host "██║ ██║╚███╔███╔╝███████║ ██████╔╝╚██████╔╝██████╔╝╚██████╔╝███████╗ ██║ " -ForegroundColor Green
Write-Host "╚═╝ ╚═╝ ╚══╝╚══╝ ╚══════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚══════╝ ╚═╝ " -ForegroundColor Green
Write-Host " WORKING OUT HOW MUCH AWS HAS COST YOU IN YOUR ACCOUNT" -ForegroundColor Red
Write-Host "#################################################################################" -ForegroundColor Green
############ Verify if there is a profile name specified ###############
if([string]::IsNullOrEmpty($profileName)){
# Verify The AWS Profile to use as none has been supplied
$profileName = "default"
$myprompt = 'Which AWS profile? ( default is: ' + $profileName + ' )'
# Check which profile to use
$profileChoice = Read-Host -Prompt $myprompt
# Check for what profile - if an entry given update the default
if(-not ([string]::IsNullOrEmpty($profileChoice))){
$profileName = $profileChoice
}
}
##### Get the current costs for the month ##########
function getCostsForMonth{
# Get the current cost information from AWS
aws ce get-cost-and-usage --time-period Start=$global:startDate,End=$global:endDate --granularity MONTHLY --metrics "BlendedCost" --output $outputType --profile $profileName
}
###### VERIFY SSO IS LOGGED IN ELSE PROMPT FOR IT ######
function verifyAuth{
# Run an SSO call to work out if you are currently signed-in and if not, authenticate yourself
$areYouAuthenticated = aws sts get-caller-identity --query "Account" --profile $profileName
# If the returned string call is greater than zero or null it is an account ID returned
if( $areYouAuthenticated.Length -gt 1 ){
# Session valid
getCostsForMonth
}
else{
# Run Session Auth
Write-Host "AWS Session timed-out, re-authenticate please..."
aws sso login --profile $profileName
# Once signed-in then run the cost analysis
getCostsForMonth
}
}
##### Get date ranges ########
# If there are items set then they will be used else the system will prompt or use automatic dates
function getDateRanges{
# If the date ranges have been passed into the script, use those, otherwise request dates
if([string]::IsNullOrEmpty($global:startDate)){
# Verify The AWS Profile to use as none has been supplied
$startProcessedDate = Get-Date -Format "yyyy-MM"
$global:startDate = $startProcessedDate + "-01"
if($prompt -eq "y"){
$myprompt = 'What start date? ( YYYY-MM-DD format, default is: ' + $startDate + ' )'
# Check which profile to use
$startDateChoice = Read-Host -Prompt $myprompt
# Check for what profile - if an entry given update the default
if(-not ([string]::IsNullOrEmpty($startDateChoice))){
$global:startDate = $startDateChoice
}
}
}
if([string]::IsNullOrEmpty($global:endDate)){
# Verify The AWS Profile to use as none has been supplied
$global:endDate = Get-Date -Format "yyyy-MM-dd"
if($prompt -eq "y"){
$myprompt = 'What end date? ( YYYY-MM-DD format, default is: ' + $endDate + ' )'
# Check which profile to use
$endDateChoice = Read-Host -Prompt $myprompt
# Check for what profile - if an entry given update the default
if(-not ([string]::IsNullOrEmpty($endDateChoice))){
$global:endDate = $endDateChoice
}
}
}
}
######################################################################
######## Clear the screen and run the application hooks ##############
### Script Launch:
getDateRanges
verifyAuth