-
Notifications
You must be signed in to change notification settings - Fork 2
/
ec2.go
45 lines (37 loc) · 999 Bytes
/
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
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
func getName(tags []*ec2.Tag) string {
for _, tag := range tags {
if *tag.Key == "Name" {
return *tag.Value
}
}
return ""
}
func ListEc2Instances(region string) {
conn := ec2.New(session.New(), &aws.Config{Region: aws.String(region)})
resp, err := conn.DescribeInstances(nil)
if err != nil {
panic(err)
}
table := NewTable([]string{
"ID", "NAME", "PRIVATE IP", "PUBLIC IP", "TYPE", "STATE",
})
for _, reservation := range resp.Reservations {
for _, instance := range reservation.Instances {
table.Append([]string{
stringFromPointer(instance.InstanceId, ""),
getName(instance.Tags),
stringFromPointer(instance.PrivateIpAddress, ""),
stringFromPointer(instance.PublicIpAddress, ""),
stringFromPointer(instance.InstanceType, ""),
stringFromPointer(instance.State.Name, ""),
})
}
}
table.Render()
}