Skip to content

Commit

Permalink
feat: Added queries for unused resources cost (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronsh12 authored Dec 18, 2023
1 parent 019d950 commit 5c27d5a
Show file tree
Hide file tree
Showing 44 changed files with 485 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_route53_histed_zones()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_backup_vaults()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_cloudfront_distributions()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_directconntect_connections()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_dynamodb_tables()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_ec2_ebs_volumes()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_ec2_eips()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_ec2_hosts()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_ec2_images()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- depends_on: {{ ref('aws_cost__by_resources') }}

{{unused_ec2_internet_gateways()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- depends_on: {{ ref('aws_cost__by_resources') }}

{{unused_ec2_network_acls()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_ec2_transit_gateways()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_ecr_repositories()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_efs_filesystems()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_lightsail_container_services()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_lightsail_disks()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_lightsail_distributions()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_lightsail_load_balancers()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_lightsail_static_ips()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_load_balancers()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_route53_histed_zones()}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{unused_sns_topics()}}
18 changes: 18 additions & 0 deletions transformations/aws/macros/unused/unused_acm_certs.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% macro unused_acm_certs(framework, check_id) %}
{{ return(adapter.dispatch('unused_acm_certs')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_acm_certs(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_acm_certs(framework, check_id) %}
select
c.account_id,
c.arn as resource_id,
rbc.cost
from aws_acm_certificates c
JOIN {{ ref('aws_cost__by_resources') }} rbc ON c.arn = rbc.line_item_resource_id
where array_length(c.in_use_by, 1) = 0
{% endmacro %}

{% macro snowflake__unused_acm_certs(framework, check_id) %}
{% endmacro %}
29 changes: 29 additions & 0 deletions transformations/aws/macros/unused/unused_backup_vaults.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% macro unused_backup_vaults(framework, check_id) %}
{{ return(adapter.dispatch('unused_backup_vaults')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_backup_vaults(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_backup_vaults(framework, check_id) %}
with point as (
select distinct vault_arn
from aws_backup_vault_recovery_points
),
unused_vaults as (
select
vault.account_id,
vault.arn as resource_id
from aws_backup_vaults vault
left join point on point.vault_arn = vault.arn
where point.vault_arn is null)
SELECT
uv.account_id,
uv.resource_id,
rbc.cost
FROM unused_vaults uv
JOIN {{ ref('aws_cost__by_resources') }} rbc ON uv.resource_id = rbc.line_item_resource_id
{% endmacro %}

{% macro snowflake__unused_backup_vaults(framework, check_id) %}

{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% macro unused_cloudfront_distributions(framework, check_id) %}
{{ return(adapter.dispatch('unused_cloudfront_distributions')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_cloudfront_distributions(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_cloudfront_distributions(framework, check_id) %}
select
d.account_id,
d.arn as resource_id,
rbc.cost
from aws_cloudfront_distributions d
JOIN {{ ref('aws_cost__by_resources') }} rbc ON d.arn = rbc.line_item_resource_id
where (d.distribution_config->>'Enabled')::boolean is distinct from true
{% endmacro %}

{% macro snowflake__unused_cloudfront_distributions(framework, check_id) %}

{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% macro unused_directconntect_connections(framework, check_id) %}
{{ return(adapter.dispatch('unused_directconntect_connections')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_directconntect_connections(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_directconntect_connections(framework, check_id) %}
select
dc.account_id,
dc.arn as resource_id,
rbc.cost
from aws_directconnect_connections dc
JOIN {{ ref('aws_cost__by_resources') }} rbc ON dc.arn = rbc.line_item_resource_id
where dc.connection_state = 'down'
{% endmacro %}

{% macro snowflake__unused_directconntect_connections(framework, check_id) %}

{% endmacro %}
19 changes: 19 additions & 0 deletions transformations/aws/macros/unused/unused_dynamodb_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% macro unused_dynamodb_tables(framework, check_id) %}
{{ return(adapter.dispatch('unused_dynamodb_tables')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_dynamodb_tables(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_dynamodb_tables(framework, check_id) %}
select
ddb.account_id,
ddb.arn as resource_id,
rbc.cost
from aws_dynamodb_tables ddb
JOIN {{ ref('aws_cost__by_resources') }} rbc ON ddb.arn = rbc.line_item_resource_id
where ddb.item_count = 0
{% endmacro %}

{% macro snowflake__unused_dynamodb_tables(framework, check_id) %}

{% endmacro %}
19 changes: 19 additions & 0 deletions transformations/aws/macros/unused/unused_ec2_ebs_volumes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% macro unused_ec2_ebs_volumes(framework, check_id) %}
{{ return(adapter.dispatch('unused_ec2_ebs_volumes')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_ec2_ebs_volumes(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_ec2_ebs_volumes(framework, check_id) %}
select
ev.account_id,
ev.arn as resource_id,
rbc.cost
from aws_ec2_ebs_volumes ev
JOIN {{ ref('aws_cost__by_resources') }} rbc ON ev.arn = rbc.line_item_resource_id
where coalesce(jsonb_array_length(ev.attachments), 0) = 0
{% endmacro %}

{% macro snowflake__unused_ec2_ebs_volumes(framework, check_id) %}

{% endmacro %}
19 changes: 19 additions & 0 deletions transformations/aws/macros/unused/unused_ec2_eips.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% macro unused_ec2_eips(framework, check_id) %}
{{ return(adapter.dispatch('unused_ec2_eips')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_ec2_eips(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_ec2_eips(framework, check_id) %}
select
eips.account_id,
eips.allocation_id as resource_id,
rbc.cost
from aws_ec2_eips eips
JOIN {{ ref('aws_cost__by_resources') }} rbc ON eips.allocation_id = rbc.line_item_resource_id
where eips.association_id is null
{% endmacro %}

{% macro snowflake__unused_ec2_eips(framework, check_id) %}

{% endmacro %}
19 changes: 19 additions & 0 deletions transformations/aws/macros/unused/unused_ec2_hosts.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% macro unused_ec2_hosts(framework, check_id) %}
{{ return(adapter.dispatch('unused_ec2_hosts')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_ec2_hosts(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_ec2_hosts(framework, check_id) %}
select
h.account_id,
h.arn as resource_id,
rbc.cost
from aws_ec2_hosts h
JOIN {{ ref('aws_cost__by_resources') }} rbc ON h.arn = rbc.line_item_resource_id
where coalesce(jsonb_array_length(h.instances), 0) = 0
{% endmacro %}

{% macro snowflake__unused_ec2_hosts(framework, check_id) %}

{% endmacro %}
19 changes: 19 additions & 0 deletions transformations/aws/macros/unused/unused_ec2_images.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% macro unused_ec2_images(framework, check_id) %}
{{ return(adapter.dispatch('unused_ec2_images')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_ec2_images(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_ec2_images(framework, check_id) %}
select
i.account_id,
i.arn as resource_id,
rbc.cost
from aws_ec2_images i
JOIN {{ ref('aws_cost__by_resources') }} rbc ON i.arn = rbc.line_item_resource_id
where coalesce(jsonb_array_length(i.block_device_mappings), 0) = 0
{% endmacro %}

{% macro snowflake__unused_ec2_images(framework, check_id) %}

{% endmacro %}
19 changes: 19 additions & 0 deletions transformations/aws/macros/unused/unused_ec2_internet_gateways.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% macro unused_ec2_internet_gateways(framework, check_id) %}
{{ return(adapter.dispatch('unused_ec2_internet_gateways')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_ec2_internet_gateways(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_ec2_internet_gateways(framework, check_id) %}
select
ig.account_id,
ig.arn as resource_id,
rbc.cost
from aws_ec2_internet_gateways ig
JOIN {{ ref('aws_cost__by_resources') }} rbc ON ig.arn = rbc.line_item_resource_id
where coalesce(jsonb_array_length(ig.attachments), 0) = 0
{% endmacro %}

{% macro snowflake__unused_ec2_internet_gateways(framework, check_id) %}

{% endmacro %}
18 changes: 18 additions & 0 deletions transformations/aws/macros/unused/unused_ec2_network_acls.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% macro unused_ec2_network_acls(framework, check_id) %}
{{ return(adapter.dispatch('unused_ec2_network_acls')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_ec2_network_acls(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_ec2_network_acls(framework, check_id) %}
select
a.account_id,
a.arn as resource_id,
rbc.cost
from aws_ec2_network_acls a
JOIN {{ ref('aws_cost__by_resources') }} rbc ON a.arn = rbc.line_item_resource_id
where coalesce(jsonb_array_length(a.associations), 0) = 0
{% endmacro %}

{% macro snowflake__unused_ec2_network_acls(framework, check_id) %}
{% endmacro %}
26 changes: 26 additions & 0 deletions transformations/aws/macros/unused/unused_ec2_transit_gateways.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% macro unused_ec2_transit_gateways(framework, check_id) %}
{{ return(adapter.dispatch('unused_ec2_transit_gateways')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_ec2_transit_gateways(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_ec2_transit_gateways(framework, check_id) %}
with attachment as (select distinct transit_gateway_arn from aws_ec2_transit_gateway_attachments),
unused_transit_gateways as (
select
gateway.account_id,
gateway.arn as resource_id
from aws_ec2_transit_gateways gateway
left join attachment on attachment.transit_gateway_arn = gateway.arn
where attachment.transit_gateway_arn is null)
SELECT
ug.account_id,
ug.resource_id,
rbc.cost
FROM unused_transit_gateways ug
JOIN {{ ref('aws_cost__by_resources') }} rbc ON ug.resource_id = rbc.line_item_resource_id
{% endmacro %}

{% macro snowflake__unused_ec2_transit_gateways(framework, check_id) %}

{% endmacro %}
26 changes: 26 additions & 0 deletions transformations/aws/macros/unused/unused_ecr_repositories.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% macro unused_ecr_repositories(framework, check_id) %}
{{ return(adapter.dispatch('unused_ecr_repositories')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_ecr_repositories(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_ecr_repositories(framework, check_id) %}
with image as (select distinct account_id, repository_name from aws_ecr_repository_images),
unused_repo as (
select
repository.account_id,
repository.arn as resource_id
from aws_ecr_repositories repository
left join image on image.account_id = repository.account_id and image.repository_name = repository.repository_name
where image.repository_name is null)
SELECT
ur.account_id,
ur.resource_id,
rbc.cost
FROM unused_repo ur
JOIN {{ ref('aws_cost__by_resources') }} rbc ON ur.resource_id = rbc.line_item_resource_id
{% endmacro %}

{% macro snowflake__unused_ecr_repositories(framework, check_id) %}

{% endmacro %}
19 changes: 19 additions & 0 deletions transformations/aws/macros/unused/unused_efs_filesystems.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% macro unused_efs_filesystems(framework, check_id) %}
{{ return(adapter.dispatch('unused_efs_filesystems')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_efs_filesystems(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_efs_filesystems(framework, check_id) %}
select
fs.account_id,
fs.arn as resource_id,
rbc.cost
from aws_efs_filesystems fs
JOIN {{ ref('aws_cost__by_resources') }} rbc ON fs.arn = rbc.line_item_resource_id
where fs.number_of_mount_targets = 0
{% endmacro %}

{% macro snowflake__unused_efs_filesystems(framework, check_id) %}

{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% macro unused_lightsail_container_services(framework, check_id) %}
{{ return(adapter.dispatch('unused_lightsail_container_services')(framework, check_id)) }}
{% endmacro %}

{% macro default__unused_lightsail_container_services(framework, check_id) %}{% endmacro %}

{% macro postgres__unused_lightsail_container_services(framework, check_id) %}
with deployment as (select distinct container_service_arn from aws_lightsail_container_service_deployments),
unused_cs AS (
select
cs.account_id,
cs.arn as resource_id
from aws_lightsail_container_services cs
left join deployment on deployment.container_service_arn = cs.arn
where deployment.container_service_arn is null)
SELECT
cs.account_id,
cs.resource_id,
rbc.cost
FROM unused_cs cs
JOIN {{ ref('aws_cost__by_resources') }} rbc ON cs.resource_id = rbc.line_item_resource_id
{% endmacro %}

{% macro snowflake__unused_lightsail_container_services(framework, check_id) %}

{% endmacro %}
Loading

0 comments on commit 5c27d5a

Please sign in to comment.