Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
caws_part_4
func main() {
var resources []*SingleResource
for _, region := range TraceableRegions {
// We need to create a new CFG for each region. We
// could actually update the region after the fact
// but let's focus on the purpose, here :)
cfg := aws.Config{Region: aws.String(region)}
s := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: cfg,
}))
// Creating the actual AWS client from the SDK
r := resourcegroupstaggingapi.New(s)
// The results will come paginated, so we create an empty
// one outside the next for loop so we can keep updating
// it and check if there are still more results to come or
// not. We could isolate this function and call it recursively
// if we wanted to tidy up our code.
var paginationToken string = ""
var in *resourcegroupstaggingapi.GetResourcesInput
var out *resourcegroupstaggingapi.GetResourcesOutput
var err error
// Let's start an infinite for loop until there are no
for {
if len(paginationToken) == 0 {
in = &resourcegroupstaggingapi.GetResourcesInput{
ResourcesPerPage: aws.Int64(50),
}
out, err = r.GetResources(in)
if err != nil {
fmt.Println(err)
}
} else {
in = &resourcegroupstaggingapi.GetResourcesInput{
ResourcesPerPage: aws.Int64(50),
PaginationToken: &paginationToken,
}
}
out, err = r.GetResources(in)
if err != nil {
fmt.Println(err)
}
for _, resource := range out.ResourceTagMappingList {
svc := ServiceNameFromARN(resource.ResourceARN)
rgn := region
resources = append(resources, ConvertArnToSingleResource(resource.ResourceARN, svc, &rgn))
}
paginationToken = *out.PaginationToken
if *out.PaginationToken == "" {
break
}
}
}
// Finally print the results
PrettyPrintResources(resources)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.