Struct: ec2metadata.EC2Metadata

import "../ibm-cos-sdk-go/aws/ec2metadata"

Overview

A EC2Metadata is an EC2 Metadata service Client.

Implemented Interfaces

s3crypto.Cipher, s3manager.ReadSeekerWriteTo, s3manager.WriterReadFrom

Fields included from client.Client

client.Client.Config, client.Client.Handlers

Constructor Functions collapse

Method Summary collapse

Methods included from client.Client

client.Client.AddDebugHandlers(), client.Client.NewRequest()

Function Details

func New(p client.ConfigProvider, cfgs ...*aws.Config) *EC2Metadata

New creates a new instance of the EC2Metadata client with a session. This client is safe to use across multiple goroutines.

Example:

// Create a EC2Metadata client from just a session. svc := ec2metadata.New(mySession) // Create a EC2Metadata client with additional configuration svc := ec2metadata.New(mySession, aws.NewConfig().WithLogLevel(aws.LogDebugHTTPBody))


66
67
68
69
// File 'aws/ec2metadata/service.go', line 66

func New(p client.ConfigProvider, cfgs ...*aws.Config) *EC2Metadata { c := p.ClientConfig(ServiceName, cfgs...) return NewClient(*c.Config, c.Handlers, c.Endpoint, c.SigningRegion) }

Method Details

func (c *EC2Metadata) Available() bool

Available returns if the application has access to the EC2 Metadata service. Can be used to determine if application is running within an EC2 Instance and the metadata service is available.



207
208
209
// File 'aws/ec2metadata/api.go', line 207

func (c *EC2Metadata) Available() bool { return c.AvailableWithContext(aws.BackgroundContext()) }

func (c *EC2Metadata) AvailableWithContext(ctx aws.Context) bool

AvailableWithContext returns if the application has access to the EC2 Metadata service. Can be used to determine if application is running within an EC2 Instance and the metadata service is available.



214
215
216
217
218
219
220
// File 'aws/ec2metadata/api.go', line 214

func (c *EC2Metadata) AvailableWithContext(ctx aws.Context) bool { if _, err := c.GetMetadataWithContext(ctx, "instance-id"); err != nil { return false } return true }

func (c *EC2Metadata) GetDynamicData(p string) (string, error)

GetDynamicData uses the path provided to request information from the EC2 instance metadata service for dynamic data. The content will be returned as a string, or error if the request failed.



104
105
106
// File 'aws/ec2metadata/api.go', line 104

func (c *EC2Metadata) GetDynamicData(p string) (string, error) { return c.GetDynamicDataWithContext(aws.BackgroundContext(), p) }

func (c *EC2Metadata) GetDynamicDataWithContext(ctx aws.Context, p string) (string, error)

GetDynamicDataWithContext uses the path provided to request information from the EC2 instance metadata service for dynamic data. The content will be returned as a string, or error if the request failed.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
// File 'aws/ec2metadata/api.go', line 111

func (c *EC2Metadata) GetDynamicDataWithContext(ctx aws.Context, p string) (string, error) { op := &request.Operation{ Name: "GetDynamicData", HTTPMethod: "GET", HTTPPath: sdkuri.PathJoin("/latest/dynamic", p), } output := &metadataOutput{} req := c.NewRequest(op, nil, output) req.SetContext(ctx) err := req.Send() return output.Content, err }

func (c *EC2Metadata) GetInstanceIdentityDocument() (EC2InstanceIdentityDocument, error)

GetInstanceIdentityDocument retrieves an identity document describing an instance. Error is returned if the request fails or is unable to parse the response.



129
130
131
// File 'aws/ec2metadata/api.go', line 129

func (c *EC2Metadata) GetInstanceIdentityDocument() (EC2InstanceIdentityDocument, error) { return c.GetInstanceIdentityDocumentWithContext(aws.BackgroundContext()) }

func (c *EC2Metadata) GetInstanceIdentityDocumentWithContext(ctx aws.Context) (EC2InstanceIdentityDocument, error)

GetInstanceIdentityDocumentWithContext retrieves an identity document describing an instance. Error is returned if the request fails or is unable to parse the response.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// File 'aws/ec2metadata/api.go', line 136

func (c *EC2Metadata) GetInstanceIdentityDocumentWithContext(ctx aws.Context) (EC2InstanceIdentityDocument, error) { resp, err := c.GetDynamicDataWithContext(ctx, "instance-identity/document") if err != nil { return EC2InstanceIdentityDocument{}, awserr.New("EC2MetadataRequestError", "failed to get EC2 instance identity document", err) } doc := EC2InstanceIdentityDocument{} if err := json.NewDecoder(strings.NewReader(resp)).Decode(&doc); err != nil { return EC2InstanceIdentityDocument{}, awserr.New(request.ErrCodeSerialization, "failed to decode EC2 instance identity document", err) } return doc, nil }

func (c *EC2Metadata) GetMetadata(p string) (string, error)

GetMetadata uses the path provided to request information from the EC2 instance metadata service. The content will be returned as a string, or error if the request failed.



53
54
55
// File 'aws/ec2metadata/api.go', line 53

func (c *EC2Metadata) GetMetadata(p string) (string, error) { return c.GetMetadataWithContext(aws.BackgroundContext(), p) }

func (c *EC2Metadata) GetMetadataWithContext(ctx aws.Context, p string) (string, error)

GetMetadataWithContext uses the path provided to request information from the EC2 instance metadata service. The content will be returned as a string, or error if the request failed.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// File 'aws/ec2metadata/api.go', line 60

func (c *EC2Metadata) GetMetadataWithContext(ctx aws.Context, p string) (string, error) { op := &request.Operation{ Name: "GetMetadata", HTTPMethod: "GET", HTTPPath: sdkuri.PathJoin("/latest/meta-data", p), } output := &metadataOutput{} req := c.NewRequest(op, nil, output) req.SetContext(ctx) err := req.Send() return output.Content, err }

func (c *EC2Metadata) GetUserData() (string, error)

GetUserData returns the userdata that was configured for the service. If there is no user-data setup for the EC2 instance a “NotFoundError” error code will be returned.



79
80
81
// File 'aws/ec2metadata/api.go', line 79

func (c *EC2Metadata) GetUserData() (string, error) { return c.GetUserDataWithContext(aws.BackgroundContext()) }

func (c *EC2Metadata) GetUserDataWithContext(ctx aws.Context) (string, error)

GetUserDataWithContext returns the userdata that was configured for the service. If there is no user-data setup for the EC2 instance a “NotFoundError” error code will be returned.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
// File 'aws/ec2metadata/api.go', line 86

func (c *EC2Metadata) GetUserDataWithContext(ctx aws.Context) (string, error) { op := &request.Operation{ Name: "GetUserData", HTTPMethod: "GET", HTTPPath: "/latest/user-data", } output := &metadataOutput{} req := c.NewRequest(op, nil, output) req.SetContext(ctx) err := req.Send() return output.Content, err }

func (c *EC2Metadata) IAMInfo() (EC2IAMInfo, error)

IAMInfo retrieves IAM info from the metadata API



155
156
157
// File 'aws/ec2metadata/api.go', line 155

func (c *EC2Metadata) IAMInfo() (EC2IAMInfo, error) { return c.IAMInfoWithContext(aws.BackgroundContext()) }

func (c *EC2Metadata) IAMInfoWithContext(ctx aws.Context) (EC2IAMInfo, error)

IAMInfoWithContext retrieves IAM info from the metadata API



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// File 'aws/ec2metadata/api.go', line 160

func (c *EC2Metadata) IAMInfoWithContext(ctx aws.Context) (EC2IAMInfo, error) { resp, err := c.GetMetadataWithContext(ctx, "iam/info") if err != nil { return EC2IAMInfo{}, awserr.New("EC2MetadataRequestError", "failed to get EC2 IAM info", err) } info := EC2IAMInfo{} if err := json.NewDecoder(strings.NewReader(resp)).Decode(&info); err != nil { return EC2IAMInfo{}, awserr.New(request.ErrCodeSerialization, "failed to decode EC2 IAM info", err) } if info.Code != "Success" { errMsg := fmt.Sprintf("failed to get EC2 IAM Info (%s)", info.Code) return EC2IAMInfo{}, awserr.New("EC2MetadataError", errMsg, nil) } return info, nil }

func (c *EC2Metadata) Region() (string, error)

Region returns the region the instance is running in.



185
186
187
// File 'aws/ec2metadata/api.go', line 185

func (c *EC2Metadata) Region() (string, error) { return c.RegionWithContext(aws.BackgroundContext()) }

func (c *EC2Metadata) RegionWithContext(ctx aws.Context) (string, error)

RegionWithContext returns the region the instance is running in.



190
191
192
193
194
195
196
197
198
199
200
201
202
// File 'aws/ec2metadata/api.go', line 190

func (c *EC2Metadata) RegionWithContext(ctx aws.Context) (string, error) { ec2InstanceIdentityDocument, err := c.GetInstanceIdentityDocumentWithContext(ctx) if err != nil { return "", err } // extract region from the ec2InstanceIdentityDocument region := ec2InstanceIdentityDocument.Region if len(region) == 0 { return "", awserr.New("EC2MetadataError", "invalid region received for ec2metadata instance", nil) } // returns region return region, nil }