/
search_ec2.go
executable file
·66 lines (61 loc) · 1.37 KB
/
search_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
package main
import (
"code.google.com/p/gcfg"
"fmt"
"github.com/stripe/aws-go/aws"
"github.com/stripe/aws-go/gen/ec2"
"os"
)
// conf takes an AWS configuration from a file in ~/.awsgo
// example:
//
// [credentials]
// accesskey = "AKI...."
// secretkey = "mw0...."
//
type conf struct {
Credentials struct {
AccessKey string
SecretKey string
}
}
func main() {
var (
err error
conf conf
)
// obtain credentials from ~/.awsgo
credfile := os.Getenv("HOME") + "/.awsgo"
_, err = os.Stat(credfile)
if err != nil {
fmt.Println("Error: missing credentials file in ~/.awsgo")
os.Exit(1)
}
err = gcfg.ReadFileInto(&conf, credfile)
if err != nil {
panic(err)
}
// create a new client to EC2 api
creds := aws.Creds(conf.Credentials.AccessKey, conf.Credentials.SecretKey, "")
cli := ec2.New(creds, "us-east-1", nil)
fireq := ec2.Filter{
Name: aws.String("private-ip-address"),
Values: []string{"172.30.200.13"},
}
direq := ec2.DescribeInstancesRequest{
Filters: []ec2.Filter{fireq},
}
resp, err := cli.DescribeInstances(&direq)
if err != nil {
panic(err)
}
for _, reservation := range resp.Reservations {
for _, instance := range reservation.Instances {
fmt.Printf("%s\t%s", *instance.InstanceID, *instance.PrivateIPAddress)
for _, tag := range instance.Tags {
fmt.Printf("\t%s:%s", *tag.Key, *tag.Value)
}
fmt.Printf("\n")
}
}
}