Skip to content

Commit 48b0be6

Browse files
authored
Merge pull request #253 from awslabs/ami_update
updated SQl query
2 parents 5662d58 + 8496dd6 commit 48b0be6

File tree

1 file changed

+65
-74
lines changed

1 file changed

+65
-74
lines changed

data-collection/deploy/module-inventory.yaml

Lines changed: 65 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,82 +1420,73 @@ Resources:
14201420
Description: Identifies snapshots connected to AMI's with CUR data
14211421
Name: inventory_snapshot_connected_to_ami_with_CUR
14221422
QueryString: |
1423-
SELECT DISTINCT
1424-
snapshotid
1425-
, volume
1426-
, volumesize
1427-
, starttime
1428-
, snapdescription
1429-
, ownerid
1430-
, snap_ami_id
1431-
, imageid
1432-
, name
1433-
, description
1434-
, state
1435-
, rootdevicetype
1436-
, virtualizationtype
1437-
, snapshots.year
1438-
, snapshots.month
1439-
, (CASE WHEN (snap_ami_id = imageid) THEN 'AMI Available' WHEN (snap_ami_id LIKE 'ami%') THEN 'AMI Removed' ELSE 'Not AMI' END) status
1440-
,sum_line_item_usage_amount
1441-
,sum_line_item_unblended_cost
1442-
FROM
1443-
(SELECT
1444-
snapshotid
1445-
, volumeid volume
1446-
, volumesize
1447-
, starttime
1448-
, Description snapdescription
1449-
, inventory_snapshot_data.year
1450-
, inventory_snapshot_data.month
1451-
, ownerid
1452-
, (CASE WHEN ("substr"(Description, 1, 22) = 'Created by CreateImage') THEN "split_part"(Description, ' ', 5) WHEN ("substr"(Description, 2, 11) = 'Copied snap') THEN "split_part"(Description, ' ', 9) WHEN ("substr"(Description, 1, 22) = 'Copied for Destination') THEN "split_part"(Description, ' ', 4) ELSE '' END) snap_ami_id
1453-
FROM
1454-
${database}.inventory_snapshot_data)
1455-
as snapshots
1456-
LEFT JOIN (
1423+
1424+
with latest_snapshot as (
1425+
select
1426+
max(date_parse(collection_date, '%Y-%m-%d %T')) as snapshot_collection_date
1427+
FROM optimization_data.inventory_snapshot_data
1428+
),
1429+
recent_snapshots as (
1430+
SELECT
1431+
Description,
1432+
ownerid,
1433+
snapshotid,
1434+
latest_snapshot.snapshot_collection_date,
1435+
1436+
CASE
1437+
WHEN substr(Description, 1, 22) = 'Created by CreateImage' THEN split_part(Description,' ', 5)
1438+
WHEN substr(Description, 2, 11) = 'Copied snap' THEN split_part(Description,' ', 9)
1439+
WHEN substr(Description, 1, 22) = 'Copied for Destination' THEN split_part(Description,' ', 4)
1440+
ELSE NULL
1441+
END AS snapshot_ami_id
1442+
FROM optimization_data.inventory_snapshot_data
1443+
INNER JOIN latest_snapshot ON latest_snapshot.snapshot_collection_date = date_parse(collection_date, '%Y-%m-%d %T')
1444+
),
1445+
1446+
1447+
snapshot_costs as(
14571448
SELECT
1458-
imageid
1459-
, name
1460-
, description
1461-
, state
1462-
, rootdevicetype
1463-
, virtualizationtype
1449+
SPLIT(line_item_resource_id,'/')[2] as snapshot_cur_id,
1450+
SUM(CAST(line_item_unblended_cost AS DECIMAL(16,8))) AS sum_line_item_unblended_cost
14641451
FROM
1465-
${database}.inventory_ami_data
1466-
) ami ON (snapshots.snap_ami_id = ami.imageid)
1467-
left join
1468-
(SELECT
1469-
bill_payer_account_id,
1470-
line_item_usage_account_id,
1471-
product_region,
1472-
split("line_item_resource_id", '/')[2]resource_id,
1473-
${table_name}.month,
1474-
${table_name}.year,
1475-
SUM(CAST(line_item_usage_amount AS DOUBLE)) AS sum_line_item_usage_amount,
1476-
SUM(CAST(line_item_unblended_cost AS DECIMAL(16,8))) AS sum_line_item_unblended_cost
1477-
FROM
1478-
${database}.${table_name}
1479-
WHERE
1480-
${date_filter}
1481-
AND product_product_name = 'Amazon Elastic Compute Cloud'
1482-
AND line_item_usage_type LIKE '%%EBS%%Snapshot%%'
1483-
AND product_product_family LIKE 'Storage Snapshot'
1484-
AND line_item_line_item_type IN ('DiscountedUsage', 'Usage', 'SavingsPlanCoveredUsage')
1485-
GROUP BY
1486-
bill_payer_account_id,
1487-
line_item_usage_account_id,
1488-
product_region,
1489-
"line_item_resource_id",month, year
1490-
ORDER BY
1491-
sum_line_item_unblended_cost DESC,
1492-
sum_line_item_usage_amount DESC
1493-
) as cur
1494-
on snapshots.snapshotid = cur.resource_id
1495-
and snapshots.ownerid=cur.line_item_usage_account_id
1496-
and snapshots.month=cur.month
1497-
and snapshots.year=cur.year
1498-
where snapshots.month = '11' and snapshots.year= '2022'
1452+
${DatabaseName}.cur2
1453+
WHERE
1454+
product['product_name'] = 'Amazon Elastic Compute Cloud'
1455+
AND line_item_usage_type LIKE '%%EBS%%Snapshot%%'
1456+
AND line_item_line_item_type = 'Usage'
1457+
GROUP BY
1458+
line_item_resource_id),
1459+
1460+
latest_ami as (
1461+
select
1462+
max(date_parse(collection_date, '%Y-%m-%d %T')) as ami_last_collection_date
1463+
FROM optimization_data.inventory_ami_data
1464+
),
1465+
recent_amis as (
1466+
SELECT
1467+
imageid,
1468+
description as ami_description,
1469+
latest_ami.ami_last_collection_date
1470+
FROM optimization_data.inventory_ami_data
1471+
-- only see things that overlap
1472+
INNER JOIN latest_ami ON latest_ami.ami_last_collection_date = date_parse(collection_date, '%Y-%m-%d %T')
1473+
)
1474+
1475+
SELECT
1476+
recent_snapshots.*,
1477+
recent_amis.*,
1478+
CASE
1479+
WHEN snapshot_ami_id = imageid THEN 'AMI Available'
1480+
WHEN snapshot_ami_id LIKE 'ami%' THEN 'AMI Removed'
1481+
ELSE 'Not AMI'
1482+
END AS status,
1483+
snapshot_costs.sum_line_item_unblended_cost
1484+
FROM recent_snapshots
1485+
LEFT JOIN recent_amis ON recent_snapshots.snapshot_ami_id = recent_amis.imageid
1486+
LEFT JOIN snapshot_costs on recent_snapshots.snapshotid = snapshot_costs.snapshot_cur_id
1487+
WHERE snapshot_ami_id is not NULL
1488+
order by snapshotid
1489+
14991490
15001491
AthenaSnaphotAMIPricing:
15011492
Type: AWS::Athena::NamedQuery

0 commit comments

Comments
 (0)