Skip to content

Instantly share code, notes, and snippets.

@dishbreak
Last active April 1, 2022 23:01
Show Gist options
  • Save dishbreak/ec36d9f85fd98f67ff42aa67df0d6219 to your computer and use it in GitHub Desktop.
Save dishbreak/ec36d9f85fd98f67ff42aa67df0d6219 to your computer and use it in GitHub Desktop.
Dealing with AWS API pagination
package ecsiterator
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs"
)
func GetContainerInstanceArnsForEcsCluster(e *ecs.Client, ecsClusterName string) ([]string) {
var nextToken *string
containerInstanceArns := make([]string, 0)
for keepGoing := true; keepGoing; {
if resp, err := e.ListContainerInstances(context.TODO(), &ecs.ListContainerInstancesInput{
Cluster: aws.String(ecsClusterName),
NextToken: nextToken,
}); err != nil {
return nil, err
} else {
nextToken = resp.NextToken
containerInstanceArns = append(containerInstanceArns, resp.ContainerInstanceArns...)
}
keepGoing = nextToken != nil
}
return containerInstanceArns, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment