This repository has been archived by the owner on Aug 20, 2022. It is now read-only.
/
vpc_eps.go
107 lines (77 loc) · 2.32 KB
/
vpc_eps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package worker
import (
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/pipetail/cloudlint/pkg/awsregions"
"github.com/pipetail/cloudlint/pkg/check"
"github.com/pipetail/cloudlint/pkg/checkcompleted"
log "github.com/sirupsen/logrus"
)
func getVpcEndpointsWithinRegion(ec2client *ec2.EC2) []*ec2.VpcEndpoint {
epsParams := &ec2.DescribeVpcEndpointsInput{
Filters: []*ec2.Filter{
{
Name: aws.String("attachment.status"),
Values: []*string{
aws.String("detached"),
},
},
},
}
// Call to get detailed information on each volume
eps, _ := ec2client.DescribeVpcEndpoints(epsParams)
return eps.VpcEndpoints
}
// GetS3VpcEndpointsCount return a count of S3 VPC Endpoints
func GetS3VpcEndpointsCount(endpoints []*ec2.VpcEndpoint) int {
count := 0
if endpoints == nil {
return count
}
if len(endpoints) == 0 {
return count
}
for _, endpoint := range endpoints {
if strings.HasSuffix(*endpoint.ServiceName, ".s3") {
count++
}
}
return count
}
// https://medium.com/nubego/how-to-save-money-with-aws-vpc-endpoints-9bac8ae1319c
func vpcendpoints(event check.Event) (*checkcompleted.Event, error) {
// prepare the empty report
outputReport := checkcompleted.New(event.Payload.CheckID)
auth := event.Payload.AWSAuth
s3endpointscount := 0
log.WithFields(log.Fields{
"event": event,
}).Info("checking vpc_eps")
//var countDisks int64 = 0
var totalMonthlyPrice float64 = 0
regions := awsregions.GetRegions()
// see https://godoc.org/github.com/aws/aws-sdk-go/service/ec2#Region
for _, region := range regions {
log.WithFields(log.Fields{
"awsRegion": region,
}).Debug("checking ebs_unused in aws region")
ec2Svc := NewEC2Client(auth, region)
vpcendpoints := getVpcEndpointsWithinRegion(ec2Svc)
// TODO: check if volumes.nextToken is nil
s3endpointscount += GetS3VpcEndpointsCount(vpcendpoints)
}
// TODO: make this relative to total spend
severity := checkcompleted.INFO
if s3endpointscount == 0 {
severity = checkcompleted.WARNING
totalMonthlyPrice = 0
}
// set check details
outputReport.Payload.Check.Severity = severity
outputReport.Payload.Check.Impact = int(totalMonthlyPrice)
log.WithFields(log.Fields{
"checkCompleted": outputReport,
}).Info("vpc_eps_notused check finished")
return &outputReport, nil
}