Is there a way we can list all load balancers in aws by tag name?
I could not find anything in their SDK documentation.
Can it be done? How?
I'm not familiar with the Golang AWS SDK but if it were me and I could not find it in the SDK documentation I would look up the AWS CLI documentation and find commands there since they may translate over to the SDK or worst case run system commands in Golang to execute the CLI and get the same result.
https://docs.aws.amazon.com/cli/latest/reference/resourcegroupstaggingapi/get-resources.html
Returns all the tagged resources that are associated with the specified tags (keys and values) located in the specified region for the AWS account.
So you could use this to get all resources with a specific tag and then in Golang iterate over the results to only select the ELBs
Looks like you can filter both by tag and resource in one command:
Looks like the command does exist for golang sdk
and I would think the options I highlighted above are available to you.
You would use something such as exec. https://golang.org/pkg/os/exec/
This tutorial may contain how to also get results back from exec https://nathanleclaire.com/blog/2014/12/29/shelled-out-commands-in-golang/
package main
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"
)
const (
// ProviderName is the cloud provider providing loadbalancing functionality
ProviderName = "aws"
)
// ELB is the struct implementing the lbprovider interface
type ELB struct {
client *elb.ELB
elbResourceName string
resourceapiClient *resourcegroupstaggingapi.ResourceGroupsTaggingAPI
}
// NewELB is the factory method for ELB
func NewELB(id string, secret string, region string) (*ELB, error) {
awsConfig := &aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(id, secret, ""),
}
awsConfig = awsConfig.WithCredentialsChainVerboseErrors(true)
sess, err := session.NewSession(awsConfig)
if err != nil {
return nil, fmt.Errorf("Unable to initialize AWS session: %v", err)
}
return &ELB{
client: elb.New(sess),
resourceapiClient: resourcegroupstaggingapi.New(sess),
elbResourceName: "elasticloadbalancing",
}, nil
}
// GetLoadbalancersByTag gets the loadbalancers by tag
func (e *ELB) GetLoadbalancersByTag(tagKey string, tagValue string) ([]string, error) {
tagFilters := &resourcegroupstaggingapi.TagFilter{}
tagFilters.Key = aws.String(tagKey)
tagFilters.Values = append(tagFilters.Values, aws.String(tagValue))
getResourcesInput := &resourcegroupstaggingapi.GetResourcesInput{}
getResourcesInput.TagFilters = append(getResourcesInput.TagFilters, tagFilters)
getResourcesInput.ResourceTypeFilters = append(
getResourcesInput.ResourceTypeFilters,
aws.String(e.elbResourceName),
)
resources, err := e.resourceapiClient.GetResources(getResourcesInput)
if err != nil {
return nil, err
}
elbs := []string{}
for _, resource := range resources.ResourceTagMappingList {
elbs = append(elbs, strings.Split(*resource.ResourceARN, "/")[1])
}
return elbs, nil
}
You could simply iterate over the list of ELBs and filter by Tags.
svc := elbv2.New(...)
input := &elbv2.DescribeLoadBalancersInput{
LoadBalancerArns: []*string{},
}
result, _ := svc.DescribeLoadBalancers(input)
var list_of_arns []*string
for _, lb := range result.LoadBalancers{
list_of_arns = append(list_of_arns, lb.LoadBalancerArn)
}
input2 := &elbv2.DescribeTagsInput{
ResourceArns: list_of_arns,
}
result2, _ := svc.DescribeTags(input2)
fmt.Println(result2.TagDescriptions)