Package: ec2metadata

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

Overview

Package ec2metadata provides the client for making API calls to the EC2 Metadata service.

This package's client can be disabled completely by setting the environment variable “AWS_EC2_METADATA_DISABLED=true”. This environment variable set to true instructs the SDK to disable the EC2 Metadata client. The client cannot be used while the environment variable is set to true, (case insensitive).

The endpoint of the EC2 IMDS client can be configured via the environment variable, AWS_EC2_METADATA_SERVICE_ENDPOINT when creating the client with a Session. See aws/session#Options.EC2IMDSEndpoint for more details.

Constants

const ServiceName = readonly

ServiceName is the name of the service.

Value:

"ec2metadata"

Type Summary collapse

Function Summary collapse

Type Details

EC2IAMInfo struct

An EC2IAMInfo provides the shape for unmarshaling an IAM info from the metadata API

Structure Fields:

Code string
LastUpdated time.Time
InstanceProfileArn string
InstanceProfileID string

EC2InstanceIdentityDocument struct

An EC2InstanceIdentityDocument provides the shape for unmarshaling an instance identity document

Structure Fields:

DevpayProductCodes []string
MarketplaceProductCodes []string
AvailabilityZone string
PrivateIP string
Version string
Region string
InstanceID string
BillingProducts []string
InstanceType string
AccountID string
PendingTime time.Time
ImageID string
KernelID string
RamdiskID string
Architecture string

Function Details

func NewClient(cfg aws.Config, handlers request.Handlers, endpoint, signingRegion string, opts ...func(*client.Client)) *EC2Metadata

NewClient returns a new EC2Metadata client. Should be used to create a client when not using a session. Generally using just New with a session is preferred.

Will remove the URL path from the endpoint provided to ensure the EC2 IMDS client is able to communicate with the EC2 IMDS API.

If an unmodified HTTP client is provided from the stdlib default, or no client the EC2RoleProvider's EC2Metadata HTTP client's timeout will be shortened. To disable this set Config.EC2MetadataDisableTimeoutOverride to false. Enabled by default.



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
// File 'aws/ec2metadata/service.go', line 81

func NewClient(cfg aws.Config, handlers request.Handlers, endpoint, signingRegion string, opts ...func(*client.Client)) *EC2Metadata { if !aws.BoolValue(cfg.EC2MetadataDisableTimeoutOverride) && httpClientZero(cfg.HTTPClient) { // If the http client is unmodified and this feature is not disabled // set custom timeouts for EC2Metadata requests. cfg.HTTPClient = &http.Client{ // use a shorter timeout than default because the metadata // service is local if it is running, and to fail faster // if not running on an ec2 instance. Timeout: 1 * time.Second, } // max number of retries on the client operation cfg.MaxRetries = aws.Int(2) } if u, err := url.Parse(endpoint); err == nil { // Remove path from the endpoint since it will be added by requests. // This is an artifact of the SDK adding `/latest` to the endpoint for // EC2 IMDS, but this is now moved to the operation definition. u.Path = "" u.RawPath = "" endpoint = u.String() } svc := &EC2Metadata{ Client: client.New( cfg, metadata.ClientInfo{ ServiceName: ServiceName, ServiceID: ServiceName, Endpoint: endpoint, APIVersion: "latest", }, handlers, ), } // token provider instance tp := newTokenProvider(svc, defaultTTL) // NamedHandler for fetching token svc.Handlers.Sign.PushBackNamed(request.NamedHandler{ Name: fetchTokenHandlerName, Fn: tp.fetchTokenHandler, }) // NamedHandler for enabling token provider svc.Handlers.Complete.PushBackNamed(request.NamedHandler{ Name: enableTokenProviderHandlerName, Fn: tp.enableTokenProviderHandler, }) svc.Handlers.Unmarshal.PushBackNamed(unmarshalHandler) svc.Handlers.UnmarshalError.PushBack(unmarshalError) svc.Handlers.Validate.Clear() svc.Handlers.Validate.PushBack(validateEndpointHandler) // Disable the EC2 Metadata service if the environment variable is set. // This short-circuits the service's functionality to always fail to send // requests. if strings.ToLower(os.Getenv(disableServiceEnvVar)) == "true" { svc.Handlers.Send.SwapNamed(request.NamedHandler{ Name: corehandlers.SendHandler.Name, Fn: func(r *request.Request) { r.HTTPResponse = &http.Response{ Header: http.Header{}, } r.Error = awserr.New( request.CanceledErrorCode, "EC2 IMDS access disabled via "+disableServiceEnvVar+" env var", nil) }, }) } // Add additional options to the service config for _, option := range opts { option(svc.Client) } return svc }