-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (70 loc) · 1.87 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
package main
import (
"fmt"
"os"
"sort"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/olekukonko/tablewriter"
)
func main() {
val, ok := os.LookupEnv("AWS_REGION")
region := "us-east-1"
if ok {
region = val
}
svc := ec2.New(session.New(&aws.Config{
Region: aws.String(region),
}))
input := &ec2.DescribeInstancesInput{}
result, err := svc.DescribeInstances(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Instance Name", "State", "Private IP", "Public IP"})
table.SetAutoWrapText(false)
table.SetAutoFormatHeaders(true)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetTablePadding("\t") // pad with tabs
table.SetNoWhiteSpace(true)
data := [][]string{}
for _, r := range result.Reservations {
for _, i := range r.Instances {
// get the name tag
instanceName := "Undefined"
for _, t := range i.Tags {
if strings.ToLower(aws.StringValue(t.Key)) == "name" {
instanceName = aws.StringValue(t.Value)
}
}
if instanceName != "Undefined" {
data = append(data, []string{instanceName, aws.StringValue(i.State.Name), aws.StringValue(i.PrivateIpAddress), aws.StringValue(i.PublicIpAddress)})
}
}
}
sort.Slice(data, func(i int, j int) bool {
return data[i][0] < data[j][0]
})
table.AppendBulk(data)
table.Render()
}