-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
162 lines (135 loc) · 4.35 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hamstah/awstools/common"
)
var (
filter = kingpin.Flag("filter", "The filter to use for the identifiers. eg tag:Name").String()
identifiers = kingpin.Arg("identifiers", "If omitted the instance is fetched from the EC2 metadata.").Strings()
)
type SecurityGroup struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Result struct {
InstanceID string `json:"instance_id"`
IAMInstanceProfile string `json:"iam_instance_profile"`
PrivateDNSName string `json:"private_dns_name"`
PrivateIPAddress string `json:"private_ip_address"`
PublicDNSName string `json:"public_dns_name"`
PublicIPAddress string `json:"public_ip_address"`
ImageID string `json:"image_id"`
InstanceType string `json:"instance_type"`
KeyName string `json:"key_name"`
LaunchTime time.Time `json:"launch_time"`
SubnetID string `json:"subnet_id"`
Tags map[string]string `json:"tags"`
SecurityGroups []*SecurityGroup `json:"security_groups"`
State string `json:"state"`
VPCID string `json:"vpc_id"`
AutoScalingGroup string `json:"auto_scaling_group"`
}
func main() {
kingpin.CommandLine.Name = "ec2-describe-instances"
kingpin.CommandLine.Help = "Returns metadata of one or more EC2 instances"
flags := common.HandleFlags()
session, conf := common.OpenSession(flags)
client := ec2.New(session, conf)
input := &ec2.DescribeInstancesInput{}
if len(*identifiers) == 0 {
resp, err := http.Get("http://169.254.169.254/latest/meta-data/instance-id")
common.FatalOnError(err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
common.FatalOnError(err)
input.InstanceIds = []*string{aws.String(string(body))}
} else {
values := []*string{}
for _, identifier := range *identifiers {
values = append(values, aws.String(identifier))
}
if *filter == "" {
input.InstanceIds = values
} else {
input.Filters = []*ec2.Filter{
{
Name: filter,
Values: values,
},
}
}
}
results := []*Result{}
err := client.DescribeInstancesPages(input,
func(page *ec2.DescribeInstancesOutput, lastPage bool) bool {
for _, reservation := range page.Reservations {
result := DescribeInstance(reservation)
if result != nil {
results = append(results, result)
}
}
return true
})
common.FatalOnError(err)
data, err := json.MarshalIndent(results, "", " ")
common.FatalOnError(err)
fmt.Println(string(data))
}
func MaybeString(value *string) string {
if value != nil {
return *value
}
return ""
}
func DescribeInstance(reservation *ec2.Reservation) *Result {
result := &Result{
Tags: map[string]string{},
SecurityGroups: []*SecurityGroup{},
}
if len(reservation.Instances) > 0 {
for _, instance := range reservation.Instances {
if instance == nil {
return nil
}
result.InstanceID = *instance.InstanceId
if instance.PrivateIpAddress != nil {
result.PrivateIPAddress = *instance.PrivateIpAddress
result.PrivateDNSName = *instance.PrivateDnsName
}
if instance.PublicIpAddress != nil {
result.PublicIPAddress = *instance.PublicIpAddress
result.PublicDNSName = *instance.PublicDnsName
}
if instance.IamInstanceProfile != nil {
result.IAMInstanceProfile = *instance.IamInstanceProfile.Arn
}
result.LaunchTime = *instance.LaunchTime
result.InstanceType = *instance.InstanceType
result.ImageID = *instance.ImageId
result.KeyName = *instance.KeyName
result.SubnetID = MaybeString(instance.SubnetId)
result.VPCID = MaybeString(instance.VpcId)
result.State = *instance.State.Name
for _, securityGroup := range instance.SecurityGroups {
result.SecurityGroups = append(result.SecurityGroups, &SecurityGroup{
ID: *securityGroup.GroupId,
Name: *securityGroup.GroupName,
})
}
for _, tag := range instance.Tags {
result.Tags[*tag.Key] = *tag.Value
if *tag.Key == "aws:autoscaling:groupName" {
result.AutoScalingGroup = *tag.Value
}
}
}
}
return result
}