Skip to content

Commit

Permalink
POL-957 AWS Rightsize EC2 CloudWatch Fix (#1577)
Browse files Browse the repository at this point in the history
* fix

* tweak
  • Loading branch information
XOmniverse authored Oct 27, 2023
1 parent 8771128 commit eb761c7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
4 changes: 4 additions & 0 deletions cost/aws/rightsize_ec2_instances/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v4.3

- Fixed issue with gathering and interpreting CloudWatch metrics

## v4.2

- Updated description of `Account Number` parameter
Expand Down
26 changes: 19 additions & 7 deletions cost/aws/rightsize_ec2_instances/aws_rightsize_ec2_instances.pt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ severity "low"
category "Cost"
default_frequency "weekly"
info(
version: "4.2",
version: "4.3",
provider: "AWS",
service: "Compute",
policy_set: "Rightsize Compute Instances",
Expand Down Expand Up @@ -593,7 +593,7 @@ script "js_cloudwatch_queries", type: "javascript" do
"MetricName": mem_metricname,
"Dimensions": dimensions
},
"Period": 2592000,
"Period": lookback,
"Stat": stat
},
"ReturnData": true
Expand All @@ -608,20 +608,28 @@ end

# Combine queries into 500 item blocks so we can make bulk requests to Cloudwatch
datasource "ds_instances_requests" do
run_script $js_instances_requests, $ds_cloudwatch_queries
run_script $js_instances_requests, $ds_cloudwatch_queries, $param_stats_lookback
end

script "js_instances_requests", type: "javascript" do
parameters "queries"
parameters "queries", "param_stats_lookback"
result "result"
code <<-EOS
// Organize the queries into discrete requests to send in.
// Queries are first sorted by region and then split into 500 item blocks.
result = []
query_block_size = 500
end_date = parseInt(new Date().getTime() / 1000)
start_date = parseInt(new Date(new Date().setDate(new Date().getDate() - 30)).getTime() / 1000)
// Round down to beginning of the hour to avoid getting multiple values
// from CloudWatch due to how the data is sliced
end_date = new Date()
end_date.setMinutes(0, 0, 0)
end_date = parseInt(end_date.getTime() / 1000)
start_date = new Date()
start_date.setDate(start_date.getDate() - param_stats_lookback)
start_date.setMinutes(0, 0, 0)
start_date = parseInt(start_date.getTime() / 1000)
_.each(Object.keys(queries), function(region) {
for (i = 0; i < queries[region].length; i += query_block_size) {
Expand Down Expand Up @@ -703,7 +711,11 @@ script "js_cloudwatch_data_sorted", type: "javascript" do
region = item['region']
instance_name = item['id'].split('_')[0] + '-' + item['id'].split('_')[1]
metric = item['id'].split('_')[2]
value = item['values'][0]
// Grabbing index 0 SHOULD be safe because we should only get one result.
// Just in case AWS slices the data weirdly and returns 2 results, we make
// sure we grab the last item every time, which contains the actual data we need.
value = item['values'][item['values'].length - 1]
if (result[region] == undefined) { result[region] = {} }
if (result[region][instance_name] == undefined) { result[region][instance_name] = {} }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tenancy "single"
default_frequency "15 minutes"
info(
provider: "AWS",
version: "4.2", # This version of the Meta Parent Policy Template should match the version of the Child Policy Template as it appears in the Catalog for best reliability
version: "4.3", # This version of the Meta Parent Policy Template should match the version of the Child Policy Template as it appears in the Catalog for best reliability
publish: "false"
)

Expand Down

0 comments on commit eb761c7

Please sign in to comment.