Skip to content

Commit

Permalink
Fix bug where an instance with a specific id gets retrieved from a gi…
Browse files Browse the repository at this point in the history
…ven region but propagate an error when we attempt to find it from another region it is not in.

Signed-off-by: Vasil Sirakov <sirakov97@gmail.com>
  • Loading branch information
VasilSirakov committed Sep 26, 2024
1 parent adbfee4 commit 8a9f783
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions providers/aws/resources/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ func Is400AccessDeniedError(err error) bool {
return false
}

func Is400InstanceNotFoundError(err error) bool {
var respErr *http.ResponseError
if errors.As(err, &respErr) {
if respErr.HTTPStatusCode() == 400 && (strings.Contains(respErr.Error(), "InvalidInstanceID.NotFound") || strings.Contains(respErr.Error(), "InvalidInstanceID.Malformed")) {
return true
}
}
return false
}

func strMapToInterface(m map[string]string) map[string]interface{} {
res := map[string]interface{}{}
for k, v := range m {
Expand Down
7 changes: 7 additions & 0 deletions providers/aws/resources/aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,13 @@ func (a *mqlAwsEc2) getInstances(conn *connection.AwsConnection) []*jobpool.Job
log.Warn().Str("region", regionVal).Msg("error accessing region for AWS API")
return res, nil
}
// AWS returns an error response when trying to find an instance with a specific identifier if it cannot find it in some region.
// we do not propagate this error upward because an instance can be found in one region and return an error for all others which
// would be the expected behavior.
if Is400InstanceNotFoundError(err) {
log.Debug().Str("region", regionVal).Msg("could not find instance in region")
return res, nil
}
return nil, err
}
res, err = a.gatherInstanceInfo(instances, regionVal)
Expand Down

0 comments on commit 8a9f783

Please sign in to comment.