-
Notifications
You must be signed in to change notification settings - Fork 10
/
ec2.go
110 lines (93 loc) · 3.13 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* Copyright (C) 2023 Cristian Magherusan-Stanciu. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Open Software License version 3.0 as published
* by the Open Source Initiative.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Open Software License version 3.0 for more details.
*
* You should have received a copy of the Open Software License version 3.0
* along with this program. If not, see <https://opensource.org/licenses/OSL-3.0>.
*/
package main
import (
"context"
"fmt"
"log"
"sync"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
)
type EC2InstanceInfo struct {
Region string
NameTag string
InstanceState string
InstanceID string
PublicIP string
VPCID string
SubnetID string
Cost float64
}
func fetchInstancesInRegion(conf aws.Config, regionName string) ([]types.Instance, error) {
// Create a regional client
regionalClient := ec2.NewFromConfig(conf, func(o *ec2.Options) {
o.Region = regionName
})
// Fetch instances in the region
resp, err := regionalClient.DescribeInstances(context.TODO(), &ec2.DescribeInstancesInput{})
if err != nil {
return nil, fmt.Errorf("failed to describe instances in region %s: %v", regionName, err)
}
var filteredInstances []types.Instance
for _, reservation := range resp.Reservations {
for _, instance := range reservation.Instances {
if instance.PublicIpAddress != nil && *instance.PublicIpAddress != "" {
filteredInstances = append(filteredInstances, instance)
}
}
}
return filteredInstances, nil
}
func fetchAllInstances(config aws.Config, regions []types.Region) ([]EC2InstanceInfo, error) {
var allInstances []EC2InstanceInfo
var mu sync.Mutex
var wg sync.WaitGroup
debug.Println("Starting fetchAllInstances...")
for _, region := range regions {
wg.Add(1)
go func(region types.Region) {
defer wg.Done()
debug.Printf("Fetching instances for region: %s", *region.RegionName)
instances, err := fetchInstancesInRegion(config, *region.RegionName)
if err != nil {
log.Printf("Failed to fetch instances in region %s: %v", *region.RegionName, err)
return
}
debug.Printf("Fetched %d instances for region %s", len(instances), *region.RegionName)
for _, instance := range instances {
nameTag := getNameTagValue(instance.Tags)
inst := EC2InstanceInfo{
Region: *region.RegionName,
NameTag: nameTag,
InstanceState: string(instance.State.Name),
InstanceID: *instance.InstanceId,
PublicIP: *instance.PublicIpAddress,
VPCID: *instance.VpcId,
SubnetID: *instance.SubnetId,
Cost: 3.65,
}
mu.Lock()
allInstances = append(allInstances, inst)
mu.Unlock()
}
}(region)
}
wg.Wait()
debug.Printf("Finished fetchAllInstances. Total instances fetched: %d", len(allInstances))
return allInstances, nil
}