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)
28 return output.Content, req.Send()
31 // GetUserData returns the userdata that was configured for the service. If
32 // there is no user-data setup for the EC2 instance a "NotFoundError" error
33 // code will be returned.
34 func (c *EC2Metadata) GetUserData() (string, error) {
35 op := &request.Operation{
38 HTTPPath: "/user-data",
41 output := &metadataOutput{}
42 req := c.NewRequest(op, nil, output)
43 req.Handlers.UnmarshalError.PushBack(func(r *request.Request) {
44 if r.HTTPResponse.StatusCode == http.StatusNotFound {
45 r.Error = awserr.New("NotFoundError", "user-data not found", r.Error)
49 return output.Content, req.Send()
52 // GetDynamicData uses the path provided to request information from the EC2
53 // instance metadata service for dynamic data. The content will be returned
54 // as a string, or error if the request failed.
55 func (c *EC2Metadata) GetDynamicData(p string) (string, error) {
56 op := &request.Operation{
57 Name: "GetDynamicData",
59 HTTPPath: sdkuri.PathJoin("/dynamic", p),
62 output := &metadataOutput{}
63 req := c.NewRequest(op, nil, output)
65 return output.Content, req.Send()
68 // GetInstanceIdentityDocument retrieves an identity document describing an
69 // instance. Error is returned if the request fails or is unable to parse
71 func (c *EC2Metadata) GetInstanceIdentityDocument() (EC2InstanceIdentityDocument, error) {
72 resp, err := c.GetDynamicData("instance-identity/document")
74 return EC2InstanceIdentityDocument{},
75 awserr.New("EC2MetadataRequestError",
76 "failed to get EC2 instance identity document", err)
79 doc := EC2InstanceIdentityDocument{}
80 if err := json.NewDecoder(strings.NewReader(resp)).Decode(&doc); err != nil {
81 return EC2InstanceIdentityDocument{},
82 awserr.New("SerializationError",
83 "failed to decode EC2 instance identity document", err)
89 // IAMInfo retrieves IAM info from the metadata API
90 func (c *EC2Metadata) IAMInfo() (EC2IAMInfo, error) {
91 resp, err := c.GetMetadata("iam/info")
94 awserr.New("EC2MetadataRequestError",
95 "failed to get EC2 IAM info", err)
99 if err := json.NewDecoder(strings.NewReader(resp)).Decode(&info); err != nil {
101 awserr.New("SerializationError",
102 "failed to decode EC2 IAM info", err)
105 if info.Code != "Success" {
106 errMsg := fmt.Sprintf("failed to get EC2 IAM Info (%s)", info.Code)
108 awserr.New("EC2MetadataError", errMsg, nil)
114 // Region returns the region the instance is running in.
115 func (c *EC2Metadata) Region() (string, error) {
116 resp, err := c.GetMetadata("placement/availability-zone")
121 // returns region without the suffix. Eg: us-west-2a becomes us-west-2
122 return resp[:len(resp)-1], nil
125 // Available returns if the application has access to the EC2 Metadata service.
126 // Can be used to determine if application is running within an EC2 Instance and
127 // the metadata service is available.
128 func (c *EC2Metadata) Available() bool {
129 if _, err := c.GetMetadata("instance-id"); err != nil {
136 // An EC2IAMInfo provides the shape for unmarshaling
137 // an IAM info from the metadata API
138 type EC2IAMInfo struct {
140 LastUpdated time.Time
141 InstanceProfileArn string
142 InstanceProfileID string
145 // An EC2InstanceIdentityDocument provides the shape for unmarshaling
146 // an instance identity document
147 type EC2InstanceIdentityDocument struct {
148 DevpayProductCodes []string `json:"devpayProductCodes"`
149 AvailabilityZone string `json:"availabilityZone"`
150 PrivateIP string `json:"privateIp"`
151 Version string `json:"version"`
152 Region string `json:"region"`
153 InstanceID string `json:"instanceId"`
154 BillingProducts []string `json:"billingProducts"`
155 InstanceType string `json:"instanceType"`
156 AccountID string `json:"accountId"`
157 PendingTime time.Time `json:"pendingTime"`
158 ImageID string `json:"imageId"`
159 KernelID string `json:"kernelId"`
160 RamdiskID string `json:"ramdiskId"`
161 Architecture string `json:"architecture"`