-
Notifications
You must be signed in to change notification settings - Fork 50
/
resource-count-aws.sh
executable file
·523 lines (452 loc) · 19.2 KB
/
resource-count-aws.sh
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#!/bin/bash
# shellcheck disable=SC2102,SC2181,SC2207
##########################################################################################
# Instructions:
#
# - Make this script executable:
# chmod +x resource-count-aws.sh
#
# - Run this script:
# resource-count-aws.sh
# resource-count-aws.sh org (see below)
#
# API/CLI used:
#
# - aws organizations describe-organization (optional)
# - aws organizations list-accounts (optional)
# - aws sts assume-role (optional)
#
# - aws ec2 describe-instances
# - aws rds describe-db-instances
# - aws ec2 describe-nat-gateways
# - aws redshift describe-clusters
# - aws elb describe-load-balancer
# - aws lambda get-account-settings (optional)
# - aws s3api list-buckets (optional)
# - aws s3api list-objects (optional)
##########################################################################################
##########################################################################################
## Use of jq is required by this script.
##########################################################################################
if ! type "jq" > /dev/null; then
echo "Error: jq not installed or not in execution path, jq is required for script execution."
exit 1
fi
##########################################################################################
## Optionally query the AWS Organization by passing "org" as an argument.
##########################################################################################
if [ "${1}X" == "orgX" ] || [ "${2}X" == "orgX" ] || [ "${3}X" == "orgX" ]; then
USE_AWS_ORG="true"
else
USE_AWS_ORG="false"
fi
##########################################################################################
## Optionally report on Compute by passing "cwp" as an argument.
##########################################################################################
if [ "${1}X" == "cwpX" ] || [ "${2}X" = "cwpX" ] || [ "${3}X" == "cwpX" ]; then
WITH_CWP="true"
else
WITH_CWP="false"
fi
##########################################################################################
## Optionally report on S3 Bucket Size by passing "data" as an argument.
## NOTE: This can take a long time depending on the size of your buckets
##########################################################################################
if [ "${1}X" == "dataX" ] || [ "${2}X" = "dataX" ] || [ "${3}X" == "dataX" ]; then
WITH_DATA="true"
else
WITH_DATA="false"
fi
##########################################################################################
## Utility functions.
##########################################################################################
error_and_exit() {
echo
echo "ERROR: ${1}"
echo
exit 1
}
##########################################################################################
## AWS Utility functions.
##########################################################################################
aws_ec2_describe_regions() {
RESULT=$(aws ec2 describe-regions --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
fi
}
####
aws_organizations_describe_organization() {
RESULT=$(aws organizations describe-organization --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
fi
}
aws_organizations_list_accounts() {
RESULT=$(aws organizations list-accounts --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
fi
}
aws_sts_assume_role() {
RESULT=$(aws sts assume-role --role-arn="${1}" --role-session-name=prisma-cloud-sizing-resources --duration-seconds=999 --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
fi
}
####
aws_ec2_describe_instances() {
RESULT=$(aws ec2 describe-instances --max-items 99999 --region="${1}" --filters "Name=instance-state-name,Values=running" --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
else
echo '{"Error": [] }'
fi
}
aws_ec2_describe_db_instances() {
RESULT=$(aws rds describe-db-instances --max-items 99999 --region="${1}" --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
else
echo '{"Error": [] }'
fi
}
aws_ec2_describe_nat_gateways() {
RESULT=$(aws ec2 describe-nat-gateways --max-items 99999 --region="${1}" --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
else
echo '{"Error": [] }'
fi
}
aws_redshift_describe_clusters() {
RESULT=$(aws redshift describe-clusters --max-items 99999 --region="${1}" --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
else
echo '{"Error": [] }'
fi
}
aws_elb_describe_load_balancers() {
RESULT=$(aws elb describe-load-balancers --max-items 99999 --region="${1}" --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
else
echo '{"Error": [] }'
fi
}
aws_lambda_get_account_settings() {
RESULT=$(aws lambda get-account-settings --region="${1}" --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
else
echo '{"Error": [] }'
fi
}
aws_s3api_list_buckets() {
RESULT=$(aws s3api list-buckets --query "Buckets[].Name[]" --output json 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
else
echo '{"Error": [] }'
fi
}
aws_s3_ls_bucket_size() {
RESULT=$(aws s3api list-objects --bucket "${1}" --output json --query "sum(Contents[].Size)" 2>/dev/null)
if [ $? -eq 0 ]; then
echo "${RESULT}"
else
echo '-1'
fi
}
####
get_region_list() {
echo "###################################################################################"
echo "Querying AWS Regions"
REGIONS=$(aws_ec2_describe_regions | jq -r '.Regions[] | .RegionName' 2>/dev/null | sort)
XIFS=$IFS
# shellcheck disable=SC2206
IFS=$'\n' REGION_LIST=($REGIONS)
IFS=$XIFS
if [ ${#REGION_LIST[@]} -eq 0 ]; then
echo " Warning: Using default region list"
REGION_LIST=(us-east-1 us-east-2 us-west-1 us-west-2 ap-south-1 ap-northeast-1 ap-northeast-2 ap-southeast-1 ap-southeast-2 eu-north-1 eu-central-1 eu-west-1 sa-east-1 eu-west-2 eu-west-3 ca-central-1)
fi
echo " Total number of regions: ${#REGION_LIST[@]}"
echo "###################################################################################"
echo ""
}
get_bucket_list() {
echo "###################################################################################"
echo "Querying S3 buckets"
BUCKETS=$(aws_s3api_list_buckets | jq -r '.[]' 2>/dev/null)
XIFS=$IFS
# shellcheck disable=SC2206
IFS=$'\n' BUCKET_LIST=($BUCKETS)
IFS=$XIFS
if [ ${#BUCKET_LIST[@]} -eq 0 ]; then
echo " Warning: Could not query S3 buckets"
fi
echo " Total number of buckets: ${#BUCKET_LIST[@]}"
echo "###################################################################################"
echo ""
}
get_account_list() {
if [ "${USE_AWS_ORG}" = "true" ]; then
echo "###################################################################################"
echo "Querying AWS Organization"
MASTER_ACCOUNT_ID=$(aws_organizations_describe_organization | jq -r '.Organization.MasterAccountId' 2>/dev/null)
if [ $? -ne 0 ] || [ -z "${MASTER_ACCOUNT_ID}" ]; then
error_and_exit "Error: Failed to describe AWS Organization, check aws cli setup, and access to the AWS Organizations API."
fi
ACCOUNT_LIST=$(aws_organizations_list_accounts)
if [ $? -ne 0 ] || [ -z "${ACCOUNT_LIST}" ]; then
error_and_exit "Error: Failed to list AWS Organization accounts, check aws cli setup, and access to the AWS Organizations API."
fi
TOTAL_ACCOUNTS=$(echo "${ACCOUNT_LIST}" | jq '.Accounts | length' 2>/dev/null)
echo " Total number of member accounts: ${TOTAL_ACCOUNTS}"
echo "###################################################################################"
echo ""
else
MASTER_ACCOUNT_ID=""
ACCOUNT_LIST=""
TOTAL_ACCOUNTS=1
fi
}
assume_role() {
ACCOUNT_NAME="${1}"
ACCOUNT_ID="${2}"
echo "###################################################################################"
echo "Processing Account: ${ACCOUNT_NAME} (${ACCOUNT_ID})"
if [ "${ACCOUNT_ID}" = "${MASTER_ACCOUNT_ID}" ]; then
echo " Account is the master account, skipping assume role ..."
else
ACCOUNT_ASSUME_ROLE_ARN="arn:aws:iam::${ACCOUNT_ID}:role/OrganizationAccountAccessRole"
SESSION_JSON=$(aws_sts_assume_role "${ACCOUNT_ASSUME_ROLE_ARN}")
if [ $? -ne 0 ] || [ -z "${SESSION_JSON}" ]; then
ASSUME_ROLE_ERROR="true"
echo " Warning: Failed to assume role into Member Account ${ACCOUNT_NAME} (${ACCOUNT_ID}), skipping ..."
else
# Export environment variables used to connect to this member account.
AWS_ACCESS_KEY_ID=$(echo "${SESSION_JSON}" | jq .Credentials.AccessKeyId 2>/dev/null | sed -e 's/^"//' -e 's/"$//')
AWS_SECRET_ACCESS_KEY=$(echo "${SESSION_JSON}" | jq .Credentials.SecretAccessKey 2>/dev/null | sed -e 's/^"//' -e 's/"$//')
AWS_SESSION_TOKEN=$(echo "${SESSION_JSON}" | jq .Credentials.SessionToken 2>/dev/null | sed -e 's/^"//' -e 's/"$//')
export AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN
fi
fi
echo "###################################################################################"
echo ""
}
##########################################################################################
# Unset environment variables used to assume role into the last member account.
##########################################################################################
unassume_role() {
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
}
##########################################################################################
## Set or reset counters.
##########################################################################################
reset_account_counters() {
EC2_INSTANCE_COUNT=0
RDS_INSTANCE_COUNT=0
NATGW_COUNT=0
REDSHIFT_COUNT=0
ELB_COUNT=0
LAMBDA_COUNT=0
# shellcheck disable=SC2178
BUCKET_LIST=[]
BUCKETS_SIZE=0
WORKLOAD_COUNT=0
}
reset_global_counters() {
EC2_INSTANCE_COUNT_GLOBAL=0
RDS_INSTANCE_COUNT_GLOBAL=0
REDSHIFT_COUNT_GLOBAL=0
NATGW_COUNT_GLOBAL=0
ELB_COUNT_GLOBAL=0
LAMBDA_COUNT_GLOBAL=0
BUCKETS_SIZE_GLOBAL=0
WORKLOAD_COUNT_GLOBAL=0
WORKLOAD_COUNT_GLOBAL_WITH_IAM_MODULE=0
LAMBDA_CREDIT_USAGE_GLOBAL=0
COMPUTE_CREDIT_USAGE_GLOBAL=0
}
##########################################################################################
## Iterate through the (or each member) account, region, and billable resource type.
##########################################################################################
count_account_resources() {
for ((ACCOUNT_INDEX=0; ACCOUNT_INDEX<=(TOTAL_ACCOUNTS-1); ACCOUNT_INDEX++))
do
if [ "${USE_AWS_ORG}" = "true" ]; then
ACCOUNT_NAME=$(echo "${ACCOUNT_LIST}" | jq -r .Accounts["${ACCOUNT_INDEX}"].Name 2>/dev/null)
ACCOUNT_ID=$(echo "${ACCOUNT_LIST}" | jq -r .Accounts["${ACCOUNT_INDEX}"].Id 2>/dev/null)
ASSUME_ROLE_ERROR=""
assume_role "${ACCOUNT_NAME}" "${ACCOUNT_ID}"
if [ -n "${ASSUME_ROLE_ERROR}" ]; then
continue
fi
fi
echo "###################################################################################"
echo "Running EC2 Instances"
for i in "${REGION_LIST[@]}"
do
RESOURCE_COUNT=$(aws_ec2_describe_instances "${i}" | jq '[ .Reservations[].Instances[] ] | length' 2>/dev/null)
echo " Count of Running EC2 Instances in Region ${i}: ${RESOURCE_COUNT}"
EC2_INSTANCE_COUNT=$((EC2_INSTANCE_COUNT + RESOURCE_COUNT))
done
echo "Total EC2 Instances across all regions: ${EC2_INSTANCE_COUNT}"
echo "###################################################################################"
echo ""
echo "###################################################################################"
echo "RDS Instances"
for i in "${REGION_LIST[@]}"
do
RESOURCE_COUNT=$(aws_ec2_describe_db_instances "${i}" | jq '.[] | length' 2>/dev/null)
echo " Count of RDS Instances in Region ${i}: ${RESOURCE_COUNT}"
RDS_INSTANCE_COUNT=$((RDS_INSTANCE_COUNT + RESOURCE_COUNT))
done
echo "Total RDS Instances across all regions: ${RDS_INSTANCE_COUNT}"
echo "###################################################################################"
echo ""
echo "###################################################################################"
echo "NAT Gateways"
for i in "${REGION_LIST[@]}"
do
RESOURCE_COUNT=$(aws_ec2_describe_nat_gateways "${i}" | jq '.[] | length' 2>/dev/null)
echo " Count of NAT Gateways in Region ${i}: ${RESOURCE_COUNT}"
NATGW_COUNT=$((NATGW_COUNT + RESOURCE_COUNT))
done
echo "Total NAT Gateways across all regions: ${NATGW_COUNT}"
echo "###################################################################################"
echo ""
echo "###################################################################################"
echo "RedShift Clusters"
for i in "${REGION_LIST[@]}"
do
RESOURCE_COUNT=$(aws_redshift_describe_clusters "${i}" | jq '.[] | length' 2>/dev/null)
echo " Count of RedShift Clusters in Region: ${i}: ${RESOURCE_COUNT}"
REDSHIFT_COUNT=$((REDSHIFT_COUNT + RESOURCE_COUNT))
done
echo "Total RedShift Clusters across all regions: ${REDSHIFT_COUNT}"
echo "###################################################################################"
echo ""
echo "###################################################################################"
echo "ELBs"
for i in "${REGION_LIST[@]}"
do
RESOURCE_COUNT=$(aws_elb_describe_load_balancers "${i}" | jq '.[] | length' 2>/dev/null)
echo " Count of ELBs in Region ${i}: ${RESOURCE_COUNT}"
ELB_COUNT=$((ELB_COUNT + RESOURCE_COUNT))
done
echo "Total ELBs across all regions: ${ELB_COUNT}"
echo "###################################################################################"
echo ""
if [ "${WITH_CWP}" = "true" ]; then
echo "###################################################################################"
echo "Lambda Functions"
for i in "${REGION_LIST[@]}"
do
RESOURCE_COUNT=$(aws_lambda_get_account_settings "${i}" | jq '.AccountUsage.FunctionCount' 2>/dev/null)
echo " Count of Lambda Functions in Region ${i}: ${RESOURCE_COUNT}"
LAMBDA_COUNT=$((LAMBDA_COUNT + RESOURCE_COUNT))
done
echo "Total Lambda Functions across all regions: ${LAMBDA_COUNT}"
echo "###################################################################################"
echo ""
fi
if [ "${WITH_DATA}" = "true" ]; then
echo "###################################################################################"
echo "S3 Bucket sizes"
get_bucket_list
for i in "${BUCKET_LIST[@]}"
do
BUCKET_SIZE=$(aws_s3_ls_bucket_size "${i}" 2>/dev/null)
echo "Total size of bucket ${i}: ${BUCKET_SIZE}"
BUCKETS_SIZE=$((BUCKETS_SIZE + BUCKET_SIZE))
done
echo "Total S3 Buckets size: ${BUCKETS_SIZE}"
echo "###################################################################################"
echo ""
fi
if [ "${USE_AWS_ORG}" = "true" ]; then
WORKLOAD_COUNT=$((EC2_INSTANCE_COUNT + RDS_INSTANCE_COUNT + REDSHIFT_COUNT + NATGW_COUNT + ELB_COUNT))
echo "###################################################################################"
echo "Totals for Member Account: ${ACCOUNT_NAME} ($ACCOUNT_ID)"
echo "CSPM: Total Billable Resources: ${WORKLOAD_COUNT}"
if [ "${WITH_CWP}" = "true" ]; then
LAMBDA_CREDIT_USAGE=$((LAMBDA_COUNT/6))
echo "CWP: Total Credit Consumption by ${LAMBDA_COUNT} Lambda Functions: ${LAMBDA_CREDIT_USAGE}"
fi
echo "###################################################################################"
echo ""
fi
EC2_INSTANCE_COUNT_GLOBAL=$((EC2_INSTANCE_COUNT_GLOBAL + EC2_INSTANCE_COUNT))
RDS_INSTANCE_COUNT_GLOBAL=$((RDS_INSTANCE_COUNT_GLOBAL + RDS_INSTANCE_COUNT))
NATGW_COUNT_GLOBAL=$((NATGW_COUNT_GLOBAL + NATGW_COUNT))
REDSHIFT_COUNT_GLOBAL=$((REDSHIFT_COUNT_GLOBAL + REDSHIFT_COUNT))
ELB_COUNT_GLOBAL=$((ELB_COUNT_GLOBAL + ELB_COUNT))
LAMBDA_COUNT_GLOBAL=$((LAMBDA_COUNT_GLOBAL + LAMBDA_COUNT))
BUCKETS_SIZE_GLOBAL=$((BUCKETS_SIZE_GLOBAL + BUCKETS_SIZE))
reset_account_counters
if [ "${USE_AWS_ORG}" = "true" ]; then
unassume_role
fi
done
WORKLOAD_COUNT_GLOBAL=$((EC2_INSTANCE_COUNT_GLOBAL + RDS_INSTANCE_COUNT_GLOBAL + NATGW_COUNT_GLOBAL + REDSHIFT_COUNT_GLOBAL + ELB_COUNT_GLOBAL))
WORKLOAD_COUNT_GLOBAL_WITH_IAM_MODULE=$((WORKLOAD_COUNT_GLOBAL*125/100))
echo "###################################################################################"
echo "CSPM: Total Billable Resources:"
echo " Count of EC2 Instances: ${EC2_INSTANCE_COUNT_GLOBAL}"
echo " Count of RDS Instances: ${RDS_INSTANCE_COUNT_GLOBAL}"
echo " Count of NAT Gateways: ${NATGW_COUNT_GLOBAL}"
echo " Count of RedShift Clusters: ${REDSHIFT_COUNT_GLOBAL}"
echo " Count of ELBs: ${ELB_COUNT_GLOBAL}"
echo ""
echo "CSPM: Total Credit Consumption: ${WORKLOAD_COUNT_GLOBAL}"
echo "(If using the IAM Security Module: ${WORKLOAD_COUNT_GLOBAL_WITH_IAM_MODULE})"
echo ""
echo "Totals are based upon resource counts at the time that this script is executed."
echo "###################################################################################"
if [ "${WITH_CWP}" = "true" ]; then
LAMBDA_CREDIT_USAGE_GLOBAL=$((LAMBDA_COUNT_GLOBAL/6))
COMPUTE_CREDIT_USAGE_GLOBAL=$((LAMBDA_CREDIT_USAGE_GLOBAL))
echo ""
echo "###################################################################################"
echo "CWP Total Credit Consumption:"
echo " By ${LAMBDA_COUNT_GLOBAL} Lambda Functions: ${LAMBDA_CREDIT_USAGE_GLOBAL}"
echo ""
echo "CWP Total Credit Consumption: ${COMPUTE_CREDIT_USAGE_GLOBAL}"
echo ""
echo "###################################################################################"
fi
if [ "${WITH_DATA}" = "true" ]; then
BUCKETS_USAGE_GIG_GLOBAL=$((BUCKETS_SIZE_GLOBAL/1000/1000/1000))
BUCKETS_CREDIT_FULL_USAGE_GLOBAL=$((BUCKETS_USAGE_GIG_GLOBAL/33))
BUCKETS_CREDIT_EXPOSURE_USAGE_GLOBAL=$((BUCKETS_USAGE_GIG_GLOBAL/200))
echo ""
echo "###################################################################################"
echo "Data Security Total Credit Consumption:"
echo " If choosing Full Scan: ${BUCKETS_CREDIT_FULL_USAGE_GLOBAL}"
echo " If choosing Exposure Scan: ${BUCKETS_CREDIT_EXPOSURE_USAGE_GLOBAL}"
echo ""
echo "###################################################################################"
fi
echo "If you have any questions/concerns, please see the following licensing guide:"
echo "https://www.paloaltonetworks.com/resources/guides/prisma-cloud-enterprise-edition-licensing-guide"
}
##########################################################################################
# Allow shellspec to source this script.
##########################################################################################
${__SOURCED__:+return}
##########################################################################################
# Main.
##########################################################################################
get_account_list
get_region_list
reset_account_counters
reset_global_counters
count_account_resources