Ignore terminated instance from enumeration

main
Elie 2021-07-27 10:27:34 +02:00
parent 5bedc7d0e0
commit 1663f0a5bd
No known key found for this signature in database
GPG Key ID: 399AF69092C727B6
2 changed files with 30 additions and 3 deletions

View File

@ -132,9 +132,23 @@ func (r *ec2Repository) ListAllInstances() ([]*ec2.Instance, error) {
if v := r.cache.Get("ec2ListAllInstances"); v != nil {
return v.([]*ec2.Instance), nil
}
var instances []*ec2.Instance
input := &ec2.DescribeInstancesInput{}
input := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
// Ignore terminated state from enumeration since terminated means that instance
// has been removed
Name: aws.String("instance-state-name"),
Values: aws.StringSlice([]string{
"pending",
"running",
"stopping",
"shutting-down",
"stopped",
}),
},
},
}
err := r.client.DescribeInstancesPages(input, func(res *ec2.DescribeInstancesOutput, lastPage bool) bool {
for _, reservation := range res.Reservations {
instances = append(instances, reservation.Instances...)

View File

@ -365,7 +365,20 @@ func Test_ec2Repository_ListAllInstances(t *testing.T) {
{name: "List with 2 pages",
mocks: func(client *awstest.MockFakeEC2) {
client.On("DescribeInstancesPages",
&ec2.DescribeInstancesInput{},
&ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("instance-state-name"),
Values: aws.StringSlice([]string{
"pending",
"running",
"stopping",
"shutting-down",
"stopped",
}),
},
},
},
mock.MatchedBy(func(callback func(res *ec2.DescribeInstancesOutput, lastPage bool) bool) bool {
callback(&ec2.DescribeInstancesOutput{
Reservations: []*ec2.Reservation{