Skip to content

Commit

Permalink
Added ECS Task Definition (#868)
Browse files Browse the repository at this point in the history
  • Loading branch information
aimanafzal authored Jul 10, 2023
1 parent b14e2ab commit e7083d3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
cloudfront.Distributions,
dynamodb.Tables,
ecs.Clusters,
ecs.TaskDefinitions,
ecs.ContainerInstances,
ecr.Repositories,
sns.Topics,
Expand Down
2 changes: 1 addition & 1 deletion providers/aws/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R

//log.Printf("Hourly cost EC2: %f", hourlyCost)

}
}

monthlyCost = float64(hourlyUsage) * hourlyCost

Expand Down
66 changes: 66 additions & 0 deletions providers/aws/ecs/taskDefinitions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package ecs

import (
"context"
"fmt"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs"
log "github.com/sirupsen/logrus"
. "github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/providers"
)

func extractNameAndRevisionFromArn(input string) []string {
var nameAndRevision [2]string

parts := strings.Split(input, ":")
if len(parts) >= 6 {
nameAndRevision[0] = strings.Split(parts[5], "/")[1]
nameAndRevision[1] = parts[6]
}
return nameAndRevision[:]
}

func TaskDefinitions(ctx context.Context, client ProviderClient) ([]Resource, error) {
resources := make([]Resource, 0)
var config ecs.ListTaskDefinitionsInput
ecsClient := ecs.NewFromConfig(*client.AWSClient)

for {
output, err := ecsClient.ListTaskDefinitions(context.Background(), &config)
if err != nil {
return resources, err
}
for _, taskdefinition := range output.TaskDefinitionArns {
ecsNameAndRevision := extractNameAndRevisionFromArn(taskdefinition)
resources = append(resources, Resource{
Provider: "AWS",
Account: client.Name,
Service: "ECS Task Definition",
ResourceId: taskdefinition,
Region: client.AWSClient.Region,
Name: ecsNameAndRevision[0],
Cost: 0,
FetchedAt: time.Now(),

Link: fmt.Sprintf("https://%s.console.aws.amazon.com/ecs/v2/task-definitions/%s/%s/containers?region=%s", client.AWSClient.Region, ecsNameAndRevision[0], ecsNameAndRevision[1], client.AWSClient.Region),
})
}

if aws.ToString(output.NextToken) == "" {
break
}
config.NextToken = output.NextToken
}
log.WithFields(log.Fields{
"provider": "AWS",
"account": client.Name,
"region": client.AWSClient.Region,
"service": "ECS Task Definition",
"resources": len(resources),
}).Info("Fetched resources")
return resources, nil
}

0 comments on commit e7083d3

Please sign in to comment.