Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(1255): Integrated the AWS costexplorer API #1278

Merged
merged 7 commits into from
Dec 18, 2023
8 changes: 8 additions & 0 deletions providers/aws/cloudfront/distributions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
. "github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/providers"
awsUtils "github.com/tailwarden/komiser/providers/aws/utils"
"github.com/tailwarden/komiser/utils"
)

Expand All @@ -26,6 +27,10 @@ func Distributions(ctx context.Context, client ProviderClient) ([]Resource, erro
cloudwatchClient := cloudwatch.NewFromConfig(*client.AWSClient)
client.AWSClient.Region = tempRegion

serviceCost, err := awsUtils.GetCostAndUsage(ctx, client.AWSClient.Region, "CloudFront")
tanbirali marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Warnln("Couldn't fetch CloudFront cost and usage:", err)
}
for {
output, err := cloudfrontClient.ListDistributions(ctx, &config)
if err != nil {
Expand Down Expand Up @@ -145,6 +150,9 @@ func Distributions(ctx context.Context, client ProviderClient) ([]Resource, erro
Region: client.AWSClient.Region,
Name: *distribution.DomainName,
Cost: monthlyCost,
Metadata: map[string]string{
"serviceCost": fmt.Sprint(serviceCost),
},
Tags: tags,
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://%s.console.aws.amazon.com/cloudfront/v3/home?region=%s#/distributions/%s", client.AWSClient.Region, client.AWSClient.Region, *distribution.Id),
Expand Down
7 changes: 7 additions & 0 deletions providers/aws/ec2/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
awsUtils "github.com/tailwarden/komiser/providers/aws/utils"
"github.com/tailwarden/komiser/utils"
)

Expand All @@ -33,6 +34,11 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R

accountId := stsOutput.Account

serviceCost, err := awsUtils.GetCostAndUsage(ctx, client.AWSClient.Region, "EC2")
if err != nil {
log.Warnln("Couldn't fetch EC2 cost and usage:", err)
}

oldRegion := client.AWSClient.Region
client.AWSClient.Region = "us-east-1"
pricingClient := pricing.NewFromConfig(*client.AWSClient)
Expand Down Expand Up @@ -151,6 +157,7 @@ func Instances(ctx context.Context, client providers.ProviderClient) ([]models.R
Metadata: map[string]string{
"instanceType": string(instance.InstanceType),
"state": string(instance.State.Name),
"serviceCost": fmt.Sprint(serviceCost),
},
Link: fmt.Sprintf("https://%s.console.aws.amazon.com/ec2/home?region=%s#InstanceDetails:instanceId=%s", client.AWSClient.Region, client.AWSClient.Region, *instance.InstanceId),
})
Expand Down
8 changes: 8 additions & 0 deletions providers/aws/ecs/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/sts"
. "github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/providers"
awsUtils "github.com/tailwarden/komiser/providers/aws/utils"
)

func ContainerInstances(ctx context.Context, client ProviderClient) ([]Resource, error) {
Expand All @@ -26,6 +27,10 @@ func ContainerInstances(ctx context.Context, client ProviderClient) ([]Resource,
return resources, err
}

serviceCost, err := awsUtils.GetCostAndUsage(ctx, client.AWSClient.Region, "ECS")
if err != nil {
log.Warnln("Couldn't fetch ECS cost and usage:", err)
}
accountId := stsOutput.Account
for {
output, err := ecsContainer.ListContainerInstances(context.Background(), &config)
Expand All @@ -44,6 +49,9 @@ func ContainerInstances(ctx context.Context, client ProviderClient) ([]Resource,
Region: client.AWSClient.Region,
Name: containerInstance,
Cost: 0,
Metadata: map[string]string{
"serviceCost": fmt.Sprint(serviceCost),
},
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://%s.console.aws.amazon.com/ecs/home?#/containers/%s", client.AWSClient.Region, containerInstance),
})
Expand Down
7 changes: 7 additions & 0 deletions providers/aws/eks/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/sts"
. "github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/providers"
awsUtils "github.com/tailwarden/komiser/providers/aws/utils"
"github.com/tailwarden/komiser/utils"
)

Expand All @@ -28,6 +29,11 @@ func KubernetesClusters(ctx context.Context, client ProviderClient) ([]Resource,

accountId := stsOutput.Account

serviceCost, err := awsUtils.GetCostAndUsage(ctx, client.AWSClient.Region, "EKS")
if err != nil {
log.Warnln("Couldn't fetch EKS cost and usage:", err)
}

for {
output, err := eksClient.ListClusters(ctx, &config)
if err != nil {
Expand Down Expand Up @@ -96,6 +102,7 @@ func KubernetesClusters(ctx context.Context, client ProviderClient) ([]Resource,
"region": client.AWSClient.Region,
"service": "EKS",
"resources": len(resources),
"serviceCost":fmt.Sprint(serviceCost),
}).Info("Fetched resources")
return resources, nil
}
7 changes: 7 additions & 0 deletions providers/aws/elasticache/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/providers"
awsUtils "github.com/tailwarden/komiser/providers/aws/utils"
"github.com/tailwarden/komiser/utils"
)

Expand All @@ -26,6 +27,11 @@ func Clusters(ctx context.Context, client ProviderClient) ([]Resource, error) {

pricingClient := pricing.NewFromConfig(*client.AWSClient)

serviceCost, err := awsUtils.GetCostAndUsage(ctx, client.AWSClient.Region, "Elasticache")
if err != nil {
log.Warnln("Couldn't fetch Elasticache cost and usage:", err)
}

for {
output, err := elasticacheClient.DescribeCacheClusters(ctx, &config)
if err != nil {
Expand Down Expand Up @@ -122,6 +128,7 @@ func Clusters(ctx context.Context, client ProviderClient) ([]Resource, error) {
"nodeType": *cluster.CacheNodeType,
"status": *cluster.CacheClusterStatus,
"clusterId": *cluster.CacheClusterId,
"serviceCost": fmt.Sprint(serviceCost),
},
FetchedAt: time.Now(),
Link: fmt.Sprintf("https:/%s.console.aws.amazon.com/elasticache/home?region=%s#/%s/%s", client.AWSClient.Region, client.AWSClient.Region, *cluster.Engine, *cluster.CacheClusterId),
Expand Down
9 changes: 9 additions & 0 deletions providers/aws/iam/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
awsUtils "github.com/tailwarden/komiser/providers/aws/utils"
)

const (
Expand All @@ -25,6 +26,11 @@ func Users(ctx context.Context, client providers.ProviderClient) ([]models.Resou

paginator := iam.NewListUsersPaginator(iamClient, &iam.ListUsersInput{})

serviceCost, err := awsUtils.GetCostAndUsage(ctx, client.AWSClient.Region, "IAM")
if err != nil {
log.Warnln("Couldn't fetch IAM cost and usage:", err)
}

for paginator.HasMorePages() {
output, err := paginator.NextPage(ctx)
if err != nil {
Expand All @@ -49,6 +55,9 @@ func Users(ctx context.Context, client providers.ProviderClient) ([]models.Resou
Region: client.AWSClient.Region,
Name: aws.ToString(o.UserName),
Cost: 0,
Metadata: map[string]string{
"serviceCost": fmt.Sprint(serviceCost),
},
CreatedAt: *o.CreateDate,
Tags: tags,
FetchedAt: time.Now(),
Expand Down
9 changes: 9 additions & 0 deletions providers/aws/redshift/eventsubscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/sts"
. "github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/providers"
awsUtils "github.com/tailwarden/komiser/providers/aws/utils"
)

func EventSubscriptions(ctx context.Context, client ProviderClient) ([]Resource, error) {
Expand All @@ -27,6 +28,11 @@ func EventSubscriptions(ctx context.Context, client ProviderClient) ([]Resource,

accountId := stsOutput.Account

serviceCost, err := awsUtils.GetCostAndUsage(ctx, client.AWSClient.Region, "Redshift")
if err != nil {
log.Warnln("Couldn't fetch Redshift cost and usage:", err)
}

for {
output, err := redshiftClient.DescribeEventSubscriptions(ctx, &config)
if err != nil {
Expand Down Expand Up @@ -60,6 +66,9 @@ func EventSubscriptions(ctx context.Context, client ProviderClient) ([]Resource,
Region: client.AWSClient.Region,
Name: *eventSubscription.CustSubscriptionId,
Cost: monthlyCost,
Metadata: map[string]string{
"serviceCost": fmt.Sprint(serviceCost),
},
Tags: tags,
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://%s.console.aws.amaxon.com/redshift/home?region=%s/event-subscriptions/%s", client.AWSClient.Region, client.AWSClient.Region, *eventSubscription.CustSubscriptionId),
Expand Down
7 changes: 7 additions & 0 deletions providers/aws/s3/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func Buckets(ctx context.Context, client ProviderClient) ([]Resource, error) {
client.AWSClient.Region = "us-east-1"
pricingClient := pricing.NewFromConfig(*client.AWSClient)
client.AWSClient.Region = tempRegion
serviceCost, err := awsUtils.GetCostAndUsage(ctx, client.AWSClient.Region, "S3")
if err != nil {
log.Warnln("Couldn't fetch S3 cost and usage:", err)
}

pricingOutput, err := pricingClient.GetProducts(ctx, &pricing.GetProductsInput{
ServiceCode: aws.String("AmazonS3"),
Expand Down Expand Up @@ -143,6 +147,9 @@ func Buckets(ctx context.Context, client ProviderClient) ([]Resource, error) {
ResourceId: resourceArn,
Name: *bucket.Name,
Cost: monthlyCost,
Metadata: map[string]string{
"serviceCost": fmt.Sprint(serviceCost),
},
CreatedAt: *bucket.CreationDate,
Tags: tags,
FetchedAt: time.Now(),
Expand Down
8 changes: 8 additions & 0 deletions providers/aws/sqs/queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/sqs"
. "github.com/tailwarden/komiser/models"
. "github.com/tailwarden/komiser/providers"
awsUtils "github.com/tailwarden/komiser/providers/aws/utils"
"github.com/tailwarden/komiser/utils"
)

Expand All @@ -24,6 +25,10 @@ func Queues(ctx context.Context, client ProviderClient) ([]Resource, error) {
var config sqs.ListQueuesInput
sqsClient := sqs.NewFromConfig(*client.AWSClient)

serviceCost, err := awsUtils.GetCostAndUsage(ctx, client.AWSClient.Region, "SQS")
if err != nil {
log.Warnln("Couldn't fetch SQS cost and usage:", err)
}
for {
output, err := sqsClient.ListQueues(context.Background(), &config)
if err != nil {
Expand Down Expand Up @@ -140,6 +145,9 @@ func Queues(ctx context.Context, client ProviderClient) ([]Resource, error) {
Region: client.AWSClient.Region,
Name: queueName,
Cost: monthlyCost,
Metadata: map[string]string{
"serviceCost": fmt.Sprint(serviceCost),
},
Tags: tags,
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://%s.console.aws.amazon.com/sqs/v2/home?region=%s#/queues/%s", client.AWSClient.Region, client.AWSClient.Region, queue),
Expand Down