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.
package ec2metadata

import (
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
)

ServiceName is the name of the service.
	ServiceName          = "ec2metadata"
	disableServiceEnvVar = "AWS_EC2_METADATA_DISABLED"
Headers for Token and TTL
	ttlHeader   = "x-aws-ec2-metadata-token-ttl-seconds"
	tokenHeader = "x-aws-ec2-metadata-token"
Named Handler constants
	fetchTokenHandlerName          = "FetchTokenHandler"
	unmarshalMetadataHandlerName   = "unmarshalMetadataHandler"
	unmarshalTokenHandlerName      = "unmarshalTokenHandler"
	enableTokenProviderHandlerName = "enableTokenProviderHandler"
TTL constants
A EC2Metadata is an EC2 Metadata service Client.
type EC2Metadata struct {
	*client.Client
}
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))
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.
func ( aws.Config,  request.Handlers, ,  string,  ...func(*client.Client)) *EC2Metadata {
If the http client is unmodified and this feature is not disabled set custom timeouts for EC2Metadata requests.
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.
max number of retries on the client operation
		.MaxRetries = aws.Int(2)
	}

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.
		.Path = ""
		.RawPath = ""
		 = .String()
	}

	 := &EC2Metadata{
		Client: client.New(
			,
			metadata.ClientInfo{
				ServiceName: ServiceName,
				ServiceID:   ServiceName,
				Endpoint:    ,
				APIVersion:  "latest",
			},
			,
		),
	}
token provider instance
Disable the EC2 Metadata service if the environment variable is set. This short-circuits the service's functionality to always fail to send requests.
Add additional options to the service config
	for ,  := range  {
		(.Client)
	}
	return 
}

func ( *http.Client) bool {
	return  == nil || (.Transport == nil && .CheckRedirect == nil && .Jar == nil && .Timeout == 0)
}

type metadataOutput struct {
	Content string
}

type tokenOutput struct {
	Token string
	TTL   time.Duration
}
unmarshal token handler is used to parse the response of a getToken operation
var unmarshalTokenHandler = request.NamedHandler{
	Name: unmarshalTokenHandlerName,
	Fn: func( *request.Request) {
		defer .HTTPResponse.Body.Close()
		var  bytes.Buffer
		if ,  := io.Copy(&, .HTTPResponse.Body);  != nil {
			.Error = awserr.NewRequestFailure(awserr.New(request.ErrCodeSerialization,
				"unable to unmarshal EC2 metadata response", ), .HTTPResponse.StatusCode, .RequestID)
			return
		}

		 := .HTTPResponse.Header.Get(ttlHeader)
		,  := .Data.(*tokenOutput)
		if ! {
			return
		}

TTL is in seconds
		,  := strconv.ParseInt(, 10, 64)
		if  != nil {
			.Error = awserr.NewRequestFailure(awserr.New(request.ParamFormatErrCode,
				"unable to parse EC2 token TTL response", ), .HTTPResponse.StatusCode, .RequestID)
			return
		}
		 := time.Duration() * time.Second
		.TTL = 
	},
}

var unmarshalHandler = request.NamedHandler{
	Name: unmarshalMetadataHandlerName,
	Fn: func( *request.Request) {
		defer .HTTPResponse.Body.Close()
		var  bytes.Buffer
		if ,  := io.Copy(&, .HTTPResponse.Body);  != nil {
			.Error = awserr.NewRequestFailure(awserr.New(request.ErrCodeSerialization,
				"unable to unmarshal EC2 metadata response", ), .HTTPResponse.StatusCode, .RequestID)
			return
		}

		if ,  := .Data.(*metadataOutput);  {
			.Content = .String()
		}
	},
}

func ( *request.Request) {
	defer .HTTPResponse.Body.Close()
	var  bytes.Buffer

	if ,  := io.Copy(&, .HTTPResponse.Body);  != nil {
		.Error = awserr.NewRequestFailure(
			awserr.New(request.ErrCodeSerialization, "unable to unmarshal EC2 metadata error response", ),
			.HTTPResponse.StatusCode, .RequestID)
		return
	}
Response body format is not consistent between metadata endpoints. Grab the error message as a string and include that as the source error
	.Error = awserr.NewRequestFailure(awserr.New("EC2MetadataError", "failed to make EC2Metadata request", errors.New(.String())),
		.HTTPResponse.StatusCode, .RequestID)
}

func ( *request.Request) {
	if .ClientInfo.Endpoint == "" {
		.Error = aws.ErrMissingEndpoint
	}