This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ec2.go
85 lines (70 loc) · 2.44 KB
/
ec2.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
/******************************************************************************
Cloud Resource Counter
File: ec2.go
Summary: Provides a count of all (non-spot) EC2 instances.
******************************************************************************/
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
color "github.com/logrusorgru/aurora"
)
// EC2Counts retrieves the count of all EC2 instances either for all
// regions (allRegions is true) or the region associated with the
// session. This method gives status back to the user via the supplied
// ActivityMonitor instance.
func EC2Counts(sf ServiceFactory, am ActivityMonitor, allRegions bool) int {
// Indicate activity
am.StartAction("Retrieving EC2 counts")
// Should we get the counts for all regions?
instanceCount := 0
if allRegions {
// Get the list of all enabled regions for this account
regionsSlice := GetEC2Regions(sf.GetEC2InstanceService(""), am)
// Loop through all of the regions
for _, regionName := range regionsSlice {
// Get the EC2 counts for a specific region
instanceCount += ec2CountForSingleRegion(sf.GetEC2InstanceService(regionName), am)
}
} else {
// Get the EC2 counts for the region selected by this session
instanceCount = ec2CountForSingleRegion(sf.GetEC2InstanceService(""), am)
}
// Indicate end of activity
am.EndAction("OK (%d)", color.Bold(instanceCount))
return instanceCount
}
// Get the EC2 Instance count for a single region
func ec2CountForSingleRegion(ec2is *EC2InstanceService, am ActivityMonitor) int {
// Indicate activity
am.Message(".")
// Construct our input to find only RUNNING EC2 instances
input := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("instance-state-name"),
Values: []*string{
aws.String("running"),
},
},
},
}
// Invoke our service
instanceCount := 0
err := ec2is.InspectInstances(input, func(dio *ec2.DescribeInstancesOutput, lastPage bool) bool {
// Loop through each reservation, instance
for _, reservation := range dio.Reservations {
for _, instance := range reservation.Instances {
// Is this a valid instance? Spot instances have an InstanceLifecycle of "spot".
// Similarly, Scheduled instances have an InstanceLifecycle of "scheduled".
if instance.InstanceLifecycle == nil {
instanceCount++
}
}
}
return true
})
// Check for error
am.CheckError(err)
return instanceCount
}