Created
September 15, 2021 18:45
-
-
Save judell/fac0156f5ae62808a0230cc1cf1de0d4 to your computer and use it in GitHub Desktop.
PATTERN: UNION similar things to combine them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with lambda_sgs as ( | |
select | |
'Lambda' as service, | |
f.arn, | |
sg_id as security_group_id | |
from | |
aws_lambda_function as f, | |
jsonb_array_elements_text(f.vpc_security_group_ids) as sg_id | |
), | |
ec2_instance_sgs as ( | |
select | |
'EC2' as service, | |
i.arn, | |
sg ->> 'GroupId' as security_group_id | |
from | |
aws_ec2_instance as i, | |
jsonb_array_elements(i.security_groups) as sg | |
), | |
all_sg_attachments as ( | |
select * from lambda_sgs | |
union | |
select * from ec2_instance_sgs | |
) | |
select | |
sg.group_name, | |
sg.group_id, | |
count(sga.security_group_id) | |
from | |
aws_vpc_security_group as sg | |
full outer join | |
all_sg_attachments as sga on sg.group_id = sga.security_group_id | |
group by | |
sg.group_name, | |
sg.group_id | |
order by | |
count desc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment