/
ec2GetMetadata.go
117 lines (100 loc) · 3.43 KB
/
ec2GetMetadata.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
// Copyright © 2018 Amit Saha <amitsaha.in@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"fmt"
"log"
"reflect"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/spf13/cobra"
)
type ec2Metadata struct {
AWSRegion string `env:"AWS_REGION"`
AWSAz string `env:"AWS_AZ"`
InstanceID string `env:"INSTANCE_ID"`
InstanceType string `env:"INSTANCE_TYPE"`
Name string `env:"INSTANCE_NAME"`
PrivateIP string `env:"PRIVATE_IP"`
AMI string `env:"AMI"`
}
// FIXME: have a refresh loop
// getMetadataCmd represents the ec2 get metadata commond
var ec2GetMetadataCmd = &cobra.Command{
Use: "get-metadata",
Short: "Get EC2 Metadata of the current instance",
Long: `Get EC2 metadata`,
Run: func(cmd *cobra.Command, args []string) {
// To get the instance details (for tags), we need to make a call to the
// AWS API for which we need to set a region which we can get from the
// metadata. However, to get the metadata, we have to set a AWS region
// so, here's what we will do. Set `us-east-1` in the session to get the
// metadata, get the AWS region from the metadata and then use the region
// in the API call to get the instance details
sess := createSession()
ec2MetadataSvc := ec2metadata.New(sess)
if !ec2MetadataSvc.Available() {
log.Fatal("Could not access EC2 metadata service")
}
md := ec2Metadata{}
idDoc, err := ec2MetadataSvc.GetInstanceIdentityDocument()
if err != nil {
log.Fatal("Error retrieving instance Identity document", err.Error())
}
md.AWSRegion = idDoc.Region
md.AWSAz = idDoc.AvailabilityZone
md.InstanceID = idDoc.InstanceID
md.InstanceType = idDoc.InstanceType
md.PrivateIP = idDoc.PrivateIP
md.AMI = idDoc.ImageID
// To get the name tag we have to use the AWS API
ec2Svc := ec2.New(createSession(md.AWSRegion))
input := &ec2.DescribeInstancesInput{
InstanceIds: []*string{
aws.String(md.InstanceID),
},
}
result, err := ec2Svc.DescribeInstances(input)
if err != nil {
log.Fatal("Error getting instance details", err.Error())
}
for _, r := range result.Reservations {
for _, instance := range r.Instances {
for _, tag := range instance.Tags {
if *tag.Key == "Name" {
md.Name = *tag.Value
}
}
}
}
if exportEnvironmentVariablesBash {
val := reflect.ValueOf(&md).Elem()
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
tag := typeField.Tag
envVar := tag.Get("env")
fmt.Printf("export %s=%s\n", envVar, valueField.Interface())
}
} else {
fmt.Printf("%#v\n", md)
}
},
}
var exportEnvironmentVariablesBash bool
func init() {
ec2Cmd.AddCommand(ec2GetMetadataCmd)
ec2GetMetadataCmd.Flags().BoolVar(&exportEnvironmentVariablesBash, "export-env-vars-bash", false, "Print Bash statements for exporting the metadata")
}