@@ -218,7 +218,7 @@ func (e *Service) GetASGTargetLifecycleState() (state string, err error) {
218
218
}
219
219
220
220
// 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 ) {
222
222
metadataInfo := ""
223
223
resp , err := e .Request (path )
224
224
if err != nil {
@@ -232,8 +232,12 @@ func (e *Service) GetMetadataInfo(path string) (info string, err error) {
232
232
}
233
233
metadataInfo = string (body )
234
234
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
+ }
237
241
}
238
242
}
239
243
return metadataInfo , nil
@@ -351,26 +355,26 @@ func retry(attempts int, sleep time.Duration, httpReq func() (*http.Response, er
351
355
// GetNodeMetadata attempts to gather additional ec2 instance information from the metadata service
352
356
func (e * Service ) GetNodeMetadata () NodeMetadata {
353
357
metadata := NodeMetadata {}
354
- identityDoc , err := e .GetMetadataInfo (IdentityDocPath )
358
+ identityDoc , err := e .GetMetadataInfo (IdentityDocPath , false )
355
359
if err != nil {
356
360
log .Err (err ).Msg ("Unable to fetch metadata from IMDS" )
357
361
return metadata
358
362
}
359
363
err = json .NewDecoder (strings .NewReader (identityDoc )).Decode (& metadata )
360
364
if err != nil {
361
365
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 )
366
370
if len (metadata .AvailabilityZone ) > 1 {
367
371
metadata .Region = metadata .AvailabilityZone [0 : len (metadata .AvailabilityZone )- 1 ]
368
372
}
369
373
}
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 )
374
378
375
379
log .Info ().Interface ("metadata" , metadata ).Msg ("Startup Metadata Retrieved" )
376
380
0 commit comments