How do you add multiple tags to filter resources using AWS golang SDK?
Hello, I have the input variable to be use for the GetCostAndUsage function from AWS SDK
input := &costexplorer.GetCostAndUsageInput{
TimePeriod: &costexplorer.DateInterval{
Start: aws.String(startdate.Format("2006-01-02")),
End: aws.String(enddate.Format("2006-01-02")),
},
Granularity: aws.String("MONTHLY"),
Metrics: []*string{aws.String("BlendedCost")},
GroupBy: []*costexplorer.GroupDefinition{
{
Type: aws.String("DIMENSION"),
Key: aws.String("SERVICE"),
},
},
Filter: &costexplorer.Expression{
Tags: &costexplorer.TagValues{
Key: aws.String("project"),
Values: []*string{aws.String("Project1")},
MatchOptions: []*string{
aws.String("EQUALS"),
},
},
},
}
This does works. However, I would like to filter resources using another tag which is stage
with the values of dev
or prod
.
Thus I tried adding more tags in the Filter and it looked something like this
Filter: &costexplorer.Expression{
Tags: &costexplorer.TagValues{
Key: aws.String("project"),
Values: []*string{aws.String("Project1")},
MatchOptions: []*string{
aws.String("EQUALS"),
},
},
Tags: &costexplorer.TagValues{
Key: aws.String("stage"),
Values: []*string{aws.String("dev")},
MatchOptions: []*string{
aws.String("EQUALS"),
},
},
},
Of course Go doesn't like that and gives duplicate field name tags on struct literal. Can you give me an idea on how should I approach this problem?
Use the And
expression:
Filter: &costexplorer.Expression{
And: []*costexplorer.Expression{
{
Tags: &costexplorer.TagValues{
Key: aws.String("project"),
Values: []*string{aws.String("Project1")},
MatchOptions: []*string{
aws.String("EQUALS"),
},
},
},
{
Tags: &costexplorer.TagValues{
Key: aws.String("stage"),
Values: []*string{aws.String("dev")},
MatchOptions: []*string{
aws.String("EQUALS"),
},
},
},
},
},
Reference: https://pkg.go.dev/github.com/aws/aws-sdk-go/service/costexplorer#Expression