-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (45 loc) · 1.01 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
package main
import (
"flag"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
var (
profile string
region string
)
func init() {
flag.StringVar(&profile, "p", "default", "aws shared credential profile name")
flag.StringVar(®ion, "r", "", "aws region")
flag.Parse()
}
func main() {
sess := session.Must(session.NewSession())
cred := credentials.NewSharedCredentials("", profile)
conf := &aws.Config{Credentials: cred}
if region != "" {
conf.Region = aws.String(region)
}
svc := ec2.New(sess, conf)
res, _ := svc.DescribeInstances(nil)
for _, r := range res.Reservations {
for _, i := range r.Instances {
var instanceName string
for _, t := range i.Tags {
if *t.Key == "Name" {
instanceName = *t.Value
}
}
fmt.Printf("%s,%s,%s,%s,%s\n",
*i.InstanceId,
instanceName,
*i.InstanceType,
*i.Placement.AvailabilityZone,
*i.State.Name,
)
}
}
}