diff --git a/aws/client_logger.go b/aws/client_logger.go index f3e2e86a016..5f1c90d5bcc 100644 --- a/aws/client_logger.go +++ b/aws/client_logger.go @@ -71,6 +71,12 @@ const logRespErrMsg = `DEBUG ERROR: Response %s/%s: func logResponse(r *Request) { lw := &logWriter{r.Config.Logger, bytes.NewBuffer(nil)} + if r.HTTPResponse.Body == nil { + lw.Logger.Log(fmt.Sprintf(logRespErrMsg, + r.Metadata.ServiceName, r.Operation.Name, "request's HTTPResponse is nil")) + return + } + r.HTTPResponse.Body = &teeReaderCloser{ Reader: io.TeeReader(r.HTTPResponse.Body, lw), Source: r.HTTPResponse.Body, diff --git a/aws/ec2metadata/service.go b/aws/ec2metadata/service.go index 5c0a7e69cde..fb9aba2e338 100644 --- a/aws/ec2metadata/service.go +++ b/aws/ec2metadata/service.go @@ -58,6 +58,9 @@ func New(config aws.Config) *EC2Metadata { svc.Handlers.Send.SwapNamed(aws.NamedHandler{ Name: defaults.SendHandler.Name, Fn: func(r *aws.Request) { + r.HTTPResponse = &http.Response{ + Header: http.Header{}, + } r.Error = awserr.New( aws.ErrCodeRequestCanceled, "EC2 IMDS access disabled via "+disableServiceEnvVar+" env var", diff --git a/aws/ec2metadata/service_test.go b/aws/ec2metadata/service_test.go index d5615309e3b..8cc05945923 100644 --- a/aws/ec2metadata/service_test.go +++ b/aws/ec2metadata/service_test.go @@ -18,7 +18,10 @@ func TestClientDisableIMDS(t *testing.T) { os.Setenv("AWS_EC2_METADATA_DISABLED", "true") - svc := ec2metadata.New(unit.Config()) + cfg := unit.Config() + cfg.LogLevel = aws.LogDebugWithHTTPBody + + svc := ec2metadata.New(cfg) resp, err := svc.Region() if err == nil { t.Fatalf("expect error, got none")