10 "github.com/aws/aws-sdk-go/aws/awserr"
11 "github.com/aws/aws-sdk-go/aws/request"
12 "github.com/aws/aws-sdk-go/internal/sdkuri"
15 // GetMetadata uses the path provided to request information from the EC2
16 // instance metdata service. The content will be returned as a string, or
17 // error if the request failed.
18 func (c *EC2Metadata) GetMetadata(p string) (string, error) {
19 op := &request.Operation{
22 HTTPPath: sdkuri.PathJoin("/meta-data", p),
25 output := &metadataOutput{}
26 req := c.NewRequest(op, nil, output)
29 return output.Content, err
32 // GetUserData returns the userdata that was configured for the service. If
33 // there is no user-data setup for the EC2 instance a "NotFoundError" error
34 // code will be returned.
35 func (c *EC2Metadata) GetUserData() (string, error) {
36 op := &request.Operation{
39 HTTPPath: "/user-data",
42 output := &metadataOutput{}
43 req := c.NewRequest(op, nil, output)
44 req.Handlers.UnmarshalError.PushBack(func(r *request.Request) {
45 if r.HTTPResponse.StatusCode == http.StatusNotFound {
46 r.Error = awserr.New("NotFoundError", "user-data not found", r.Error)
51 return output.Content, err
54 // GetDynamicData uses the path provided to request information from the EC2
55 // instance metadata service for dynamic data. The content will be returned
56 // as a string, or error if the request failed.
57 func (c *EC2Metadata) GetDynamicData(p string) (string, error) {
58 op := &request.Operation{
59 Name: "GetDynamicData",
61 HTTPPath: sdkuri.PathJoin("/dynamic", p),
64 output := &metadataOutput{}
65 req := c.NewRequest(op, nil, output)
68 return output.Content, err
71 // GetInstanceIdentityDocument retrieves an identity document describing an
72 // instance. Error is returned if the request fails or is unable to parse
74 func (c *EC2Metadata) GetInstanceIdentityDocument() (EC2InstanceIdentityDocument, error) {
75 resp, err := c.GetDynamicData("instance-identity/document")
77 return EC2InstanceIdentityDocument{},
78 awserr.New("EC2MetadataRequestError",
79 "failed to get EC2 instance identity document", err)
82 doc := EC2InstanceIdentityDocument{}
83 if err := json.NewDecoder(strings.NewReader(resp)).Decode(&doc); err != nil {
84 return EC2InstanceIdentityDocument{},
85 awserr.New("SerializationError",
86 "failed to decode EC2 instance identity document", err)
92 // IAMInfo retrieves IAM info from the metadata API
93 func (c *EC2Metadata) IAMInfo() (EC2IAMInfo, error) {
94 resp, err := c.GetMetadata("iam/info")
97 awserr.New("EC2MetadataRequestError",
98 "failed to get EC2 IAM info", err)
102 if err := json.NewDecoder(strings.NewReader(resp)).Decode(&info); err != nil {
104 awserr.New("SerializationError",
105 "failed to decode EC2 IAM info", err)
108 if info.Code != "Success" {
109 errMsg := fmt.Sprintf("failed to get EC2 IAM Info (%s)", info.Code)
111 awserr.New("EC2MetadataError", errMsg, nil)
117 // Region returns the region the instance is running in.
118 func (c *EC2Metadata) Region() (string, error) {
119 resp, err := c.GetMetadata("placement/availability-zone")
125 return "", awserr.New("EC2MetadataError", "invalid Region response", nil)
128 // returns region without the suffix. Eg: us-west-2a becomes us-west-2
129 return resp[:len(resp)-1], nil
132 // Available returns if the application has access to the EC2 Metadata service.
133 // Can be used to determine if application is running within an EC2 Instance and
134 // the metadata service is available.
135 func (c *EC2Metadata) Available() bool {
136 if _, err := c.GetMetadata("instance-id"); err != nil {
143 // An EC2IAMInfo provides the shape for unmarshaling
144 // an IAM info from the metadata API
145 type EC2IAMInfo struct {
147 LastUpdated time.Time
148 InstanceProfileArn string
149 InstanceProfileID string
152 // An EC2InstanceIdentityDocument provides the shape for unmarshaling
153 // an instance identity document
154 type EC2InstanceIdentityDocument struct {
155 DevpayProductCodes []string `json:"devpayProductCodes"`
156 AvailabilityZone string `json:"availabilityZone"`
157 PrivateIP string `json:"privateIp"`
158 Version string `json:"version"`
159 Region string `json:"region"`
160 InstanceID string `json:"instanceId"`
161 BillingProducts []string `json:"billingProducts"`
162 InstanceType string `json:"instanceType"`
163 AccountID string `json:"accountId"`
164 PendingTime time.Time `json:"pendingTime"`
165 ImageID string `json:"imageId"`
166 KernelID string `json:"kernelId"`
167 RamdiskID string `json:"ramdiskId"`
168 Architecture string `json:"architecture"`