Skip to content

Instantly share code, notes, and snippets.

@dishbreak
Created April 2, 2022 06:09
Show Gist options
  • Save dishbreak/5a5d351e1e969225a132d4730c0c7c19 to your computer and use it in GitHub Desktop.
Save dishbreak/5a5d351e1e969225a132d4730c0c7c19 to your computer and use it in GitHub Desktop.
Chunking a slice for an AWS API Call
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
)
func DescribeAllInstances(instanceIDs []string, ec2Client *ec2.Client) ([]ec2types.Instance, error) {
result := make([]ec2types.Instance, 0)
chunkSize := 10
for i := 0; i <= len(instanceIDs); i += chunkSize {
end := i + chunkSize
if end > len(instanceIDs) {
end = len(instanceIDs)
}
if resp, err := ec2Client.DescribeInstances(context.TODO(), &ec2.DescribeInstancesInput{
InstanceIds: instanceIDs[i:end],
}); err != nil {
return result, err
} else {
for _, res := range resp.Reservations {
result = append(result, res.Instances...)
}
}
}
return result, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment