forked from FlorianValery/aws-organization-ec2-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
214 lines (193 loc) · 6.52 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Main file
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/spirrello/aws-organization-ec2-list/config"
)
func main() {
// Retrieve config file
config := config.InitVariables()
// Get all accounts names & ID from the organization
listAccounts := getOrganizationAccounts(config)
// Create list variable to store every ec2 instances
var listEc2 = make(map[string][]string)
// Loop over each account and get its instances via a function
fmt.Println("Retrieving the instances...")
for accountName, accountID := range listAccounts {
listEc2 = getAccountEc2(config, accountName, accountID, listEc2)
}
fmt.Println("All the instances from the Organization were retrieved.")
// Write results to a CSV file
writeToCSV(listEc2)
}
// Retrieve all accounts within organization
func getOrganizationAccounts(config config.Config) map[string]string {
// Create organization service client
var c Clients
svc := c.Organization(config.Region, config.MasterAccountID, config.OrganizationRole)
// Create variable for the list of accounts and initialize input
organizationAccounts := make(map[string]string)
input := &organizations.ListAccountsInput{}
// Start a do-while loop
for {
// Retrieve the accounts with a limit of 20 per call
organizationAccountsPaginated, err := svc.ListAccounts(input)
// Append the accounts from the current call to the total list
for _, account := range organizationAccountsPaginated.Accounts {
organizationAccounts[*account.Name] = *account.Id
}
checkError("Could not retrieve account list", err)
// Check if more accounts need to be retrieved using api token, otherwise break the loop
if organizationAccountsPaginated.NextToken == nil {
break
} else {
input = &organizations.ListAccountsInput{NextToken: organizationAccountsPaginated.NextToken}
}
}
return organizationAccounts
}
// Retrieve all ec2 instances and their attributes within an account
func getAccountEc2(config config.Config, accountName string, accountID string, result map[string][]string) map[string][]string {
// Create EC2 service client
var c Clients
svc := c.EC2(config.Region, accountID, config.OrganizationRole)
// Get the EC2 list of the given account
input := &ec2.DescribeInstancesInput{}
instances, err := svc.DescribeInstances(input)
checkError("Could not retrieve the EC2s", err)
// Iterate over the EC2 instances and add elements to global list, if instances > 0
if len(instances.Reservations) != 0 {
for _, reservation := range instances.Reservations {
// Loop through every individual EC2 instance
for _, instance := range reservation.Instances {
// Set the map key using the unique instance ID
key := *instance.InstanceId
// Retrieve account information
result[key] = append(result[key], accountName)
result[key] = append(result[key], accountID)
// Check if the instance name is set using tags, otherwise use default null name
for _, tag := range instance.Tags {
if *tag.Key == "Name" {
result[key] = append(result[key], *tag.Value)
}
}
if len(result) == 2 {
result[key] = append(result[key], "N/A")
}
// Retrieve instance information, some use default values if potentially null
result[key] = append(result[key], *instance.InstanceType)
result[key] = append(result[key], *instance.InstanceId)
result[key] = append(result[key], *instance.ImageId)
if instance.Platform != nil {
result[key] = append(result[key], *instance.Platform)
} else {
result[key] = append(result[key], "linux")
}
if instance.PrivateIpAddress != nil {
result[key] = append(result[key], *instance.PrivateIpAddress)
} else {
result[key] = append(result[key], "N/A")
}
result[key] = append(result[key], *instance.State.Name)
result[key] = append(result[key], (*instance.LaunchTime).String())
}
}
}
fmt.Println("Account number " + accountID + " done")
return result
}
// Clients Struct to store the session with custom parameters
type Clients struct {
session *session.Session
configs map[string]*aws.Config
}
// Session Func to start a session
func (c Clients) Session() *session.Session {
if c.session != nil {
return c.session
}
sess := session.Must(session.NewSession())
c.session = sess
return sess
}
// Config custom func
func (c Clients) Config(
region *string,
accountID *string,
role *string) *aws.Config {
// return no config for nil inputs
if accountID == nil || region == nil || role == nil {
return nil
}
arn := fmt.Sprintf(
"arn:aws:iam::%v:role/%v",
*accountID,
*role,
)
// include region in cache key otherwise concurrency errors
key := fmt.Sprintf("%v::%v", *region, arn)
// check for cached config
if c.configs != nil && c.configs[key] != nil {
return c.configs[key]
}
// new creds
creds := stscreds.NewCredentials(c.Session(), arn)
// new config
config := aws.NewConfig().
WithCredentials(creds).
WithRegion(*region).
WithMaxRetries(10)
if c.configs == nil {
c.configs = map[string]*aws.Config{}
}
c.configs[key] = config
return config
}
// Organization Create client
func (c *Clients) Organization(
region string,
accountID string,
role string) *organizations.Organizations {
return organizations.New(c.Session(), c.Config(®ion, &accountID, &role))
}
// EC2 Create client
func (c *Clients) EC2(
region string,
accountID string,
role string) *ec2.EC2 {
return ec2.New(c.Session(), c.Config(®ion, &accountID, &role))
}
// Function that log errors if not null
func checkError(message string, err error) {
if err != nil {
log.Fatal(message, err)
}
}
// Function that writes a map of slices to a CSV File
func writeToCSV(listEc2 map[string][]string) {
// Create the csv file using the os package
fmt.Println("Creating a CSV file...")
file, err := os.Create("result.csv")
checkError("Cannot create file", err)
defer file.Close()
// Create the writer object
writer := csv.NewWriter(file)
defer writer.Flush()
// Write headers
var headers = []string{"Account Name", "Account ID", "Instance Name", "Instance Size", "Instance ID", "Image ID", "Platform", "Private IP", "State", "Timestamp"}
writer.Write(headers)
// Loop over the organization ec2 list and write them in rows in the csv file
for _, value := range listEc2 {
err := writer.Write(value)
checkError("Cannot write to file", err)
}
fmt.Println("CSV file created in " + "result.csv")
}