@@ -218,7 +218,7 @@ func (e *Service) GetASGTargetLifecycleState() (state string, err error) {
218218}
219219
220220// GetMetadataInfo generic function for retrieving ec2 metadata
221- func (e * Service ) GetMetadataInfo (path string ) (info string , err error ) {
221+ func (e * Service ) GetMetadataInfo (path string , allowMissing bool ) (info string , err error ) {
222222 metadataInfo := ""
223223 resp , err := e .Request (path )
224224 if err != nil {
@@ -232,8 +232,12 @@ func (e *Service) GetMetadataInfo(path string) (info string, err error) {
232232 }
233233 metadataInfo = string (body )
234234 if resp .StatusCode < 200 || resp .StatusCode >= 300 {
235- log .Info ().Msgf ("Metadata response status code: %d. Body: %s" , resp .StatusCode , metadataInfo )
236- return "" , fmt .Errorf ("Metadata request received http status code: %d" , resp .StatusCode )
235+ if resp .StatusCode != 404 || ! allowMissing {
236+ log .Info ().Msgf ("Metadata response status code: %d. Body: %s" , resp .StatusCode , metadataInfo )
237+ return "" , fmt .Errorf ("Metadata request received http status code: %d" , resp .StatusCode )
238+ } else {
239+ return "" , nil
240+ }
237241 }
238242 }
239243 return metadataInfo , nil
@@ -351,26 +355,26 @@ func retry(attempts int, sleep time.Duration, httpReq func() (*http.Response, er
351355// GetNodeMetadata attempts to gather additional ec2 instance information from the metadata service
352356func (e * Service ) GetNodeMetadata () NodeMetadata {
353357 metadata := NodeMetadata {}
354- identityDoc , err := e .GetMetadataInfo (IdentityDocPath )
358+ identityDoc , err := e .GetMetadataInfo (IdentityDocPath , false )
355359 if err != nil {
356360 log .Err (err ).Msg ("Unable to fetch metadata from IMDS" )
357361 return metadata
358362 }
359363 err = json .NewDecoder (strings .NewReader (identityDoc )).Decode (& metadata )
360364 if err != nil {
361365 log .Warn ().Msg ("Unable to fetch instance identity document from ec2 metadata" )
362- metadata .InstanceID , _ = e .GetMetadataInfo (InstanceIDPath )
363- metadata .InstanceType , _ = e .GetMetadataInfo (InstanceTypePath )
364- metadata .LocalIP , _ = e .GetMetadataInfo (LocalIPPath )
365- metadata .AvailabilityZone , _ = e .GetMetadataInfo (AZPlacementPath )
366+ metadata .InstanceID , _ = e .GetMetadataInfo (InstanceIDPath , false )
367+ metadata .InstanceType , _ = e .GetMetadataInfo (InstanceTypePath , false )
368+ metadata .LocalIP , _ = e .GetMetadataInfo (LocalIPPath , false )
369+ metadata .AvailabilityZone , _ = e .GetMetadataInfo (AZPlacementPath , false )
366370 if len (metadata .AvailabilityZone ) > 1 {
367371 metadata .Region = metadata .AvailabilityZone [0 : len (metadata .AvailabilityZone )- 1 ]
368372 }
369373 }
370- metadata .InstanceLifeCycle , _ = e .GetMetadataInfo (InstanceLifeCycle )
371- metadata .LocalHostname , _ = e .GetMetadataInfo (LocalHostnamePath )
372- metadata .PublicHostname , _ = e .GetMetadataInfo (PublicHostnamePath )
373- metadata .PublicIP , _ = e .GetMetadataInfo (PublicIPPath )
374+ metadata .InstanceLifeCycle , _ = e .GetMetadataInfo (InstanceLifeCycle , false )
375+ metadata .LocalHostname , _ = e .GetMetadataInfo (LocalHostnamePath , false )
376+ metadata .PublicHostname , _ = e .GetMetadataInfo (PublicHostnamePath , true )
377+ metadata .PublicIP , _ = e .GetMetadataInfo (PublicIPPath , true )
374378
375379 log .Info ().Interface ("metadata" , metadata ).Msg ("Startup Metadata Retrieved" )
376380
0 commit comments