드래곤스카이 인프라 비용 62% 절감 설계 및 구현
매출 지표와 인프라 비용을 연동해 모니터링하고 Python으로 불필요 리소스를 매일 자동 탐지해 인프라 비용을 최대 62% 줄였습니다.
미사용 자원 찾기 (관련 공개 저장소 pf-05)
이 코드가 하는 일: AWS 계정과 리전을 하나씩 돌며 미사용 자원을 찾습니다. 공인 IP는 어떤 인스턴스에도 붙어 있지 않고 연결 정보도 없으면 미사용으로 보고, 이름·할당 ID·IP를 묶어 기록합니다. 보안그룹은 제외 규칙에 걸리는 이름을 건너뛴 뒤, 그 그룹을 쓰는 네트워크 인터페이스가 하나도 없는지 조회해 미사용 여부를 판단합니다. 이렇게 모인 결과가 공통 형식(UnusedResource)으로 쌓여, 정리할 후보를 한 목록에서 볼 수 있습니다.
for account, region, client in provider.regional("ec2"):
base = {"cloud": "AWS", "project": account.name, "project_id": account.account_id, "region": region}
for address in client.describe_addresses()["Addresses"]:
if not address.get("InstanceId") and not address.get("AssociationId"):
label = (
f"{_name(address)} ({address.get('AllocationId')} / {address.get('PublicIp')})".strip()
)
records.append(UnusedResource(resource_type="IP", resource=label, **base))
for group in client.describe_security_groups()["SecurityGroups"]:
if _excluded(group["GroupName"], rules):
continue
used = client.describe_network_interfaces(
Filters=[{"Name": "group-id", "Values": [group["GroupId"]]}]
)["NetworkInterfaces"]
if not used: