From 4e9591785265501556ce6dafb086fcc1abed413e Mon Sep 17 00:00:00 2001 From: jolo-dev Date: Sun, 22 Oct 2023 12:48:11 +0200 Subject: [PATCH 1/7] (feature/0457-support-amazon-cloudfront-function): Support Amazon CloudFront function --- providers/aws/cloudfront/functions.go | 114 ++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 providers/aws/cloudfront/functions.go diff --git a/providers/aws/cloudfront/functions.go b/providers/aws/cloudfront/functions.go new file mode 100644 index 000000000..fc8a40440 --- /dev/null +++ b/providers/aws/cloudfront/functions.go @@ -0,0 +1,114 @@ +package cloudfront + +import ( + "context" + "fmt" + "time" + + log "github.com/sirupsen/logrus" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/cloudfront" + "github.com/aws/aws-sdk-go-v2/service/cloudwatch" + "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" + . "github.com/tailwarden/komiser/models" + . "github.com/tailwarden/komiser/providers" + "github.com/tailwarden/komiser/utils" +) + +const ( + freeTierInvocations = 2000000 +) + +func Functions(ctx context.Context, client ProviderClient) ([]Resource, error) { + resources := make([]Resource, 0) + var config cloudfront.ListFunctionsInput + cloudfrontClient := cloudfront.NewFromConfig(*client.AWSClient) + + tempRegion := client.AWSClient.Region + client.AWSClient.Region = "us-east-1" + cloudwatchClient := cloudwatch.NewFromConfig(*client.AWSClient) + client.AWSClient.Region = tempRegion + + for { + output, err := cloudfrontClient.ListFunctions(ctx, &config) + if err != nil { + return resources, err + } + + for _, function := range output.FunctionList.Items { + metricsInvocationsOutput, err := cloudwatchClient.GetMetricStatistics(ctx, &cloudwatch.GetMetricStatisticsInput{ + StartTime: aws.Time(utils.BeginningOfMonth(time.Now())), + EndTime: aws.Time(time.Now()), + MetricName: aws.String("Invocations"), + Namespace: aws.String("AWS/CloudFront"), + Dimensions: []types.Dimension{ + types.Dimension{ + Name: aws.String("FunctionName"), + Value: function.Name, + }, + }, + Period: aws.Int32(3600), + Statistics: []types.Statistic{ + types.StatisticSum, + }, + }) + + if err != nil { + log.Warnf("Couldn't fetch invocations metric for %s", *function.Name) + return resources, err + } + + invocations := 0.0 + if metricsInvocationsOutput != nil && len(metricsInvocationsOutput.Datapoints) > 0 { + invocations = *metricsInvocationsOutput.Datapoints[0].Sum + } + if invocations > freeTierInvocations { + invocations -= freeTierInvocations + } + + monthlyCost := invocations * 0.0000001 + + outputTags, err := cloudfrontClient.ListTagsForResource(ctx, &cloudfront.ListTagsForResourceInput{ + Resource: function.FunctionMetadata.FunctionARN, + }) + + tags := make([]Tag, 0) + + if err == nil { + for _, tag := range outputTags.Tags.Items { + tags = append(tags, Tag{ + Key: *tag.Key, + Value: *tag.Value, + }) + } + } + + resources = append(resources, Resource{ + Provider: "AWS", + Account: client.Name, + Service: "CloudFront Functions", + ResourceId: *function.FunctionMetadata.FunctionARN, + Region: client.AWSClient.Region, + Name: *function.Name, + Cost: monthlyCost, + Tags: tags, + FetchedAt: time.Now(), + Link: fmt.Sprintf("https://%s.console.aws.amazon.com/cloudfront/v3/home?region=%s#/functions/%s", client.AWSClient.Region, client.AWSClient.Region, *function.Name), + }) + } + + if aws.ToString(output.FunctionList.NextMarker) == "" { + break + } + config.Marker = output.FunctionList.NextMarker + } + log.WithFields(log.Fields{ + "provider": "AWS", + "account": client.Name, + "region": client.AWSClient.Region, + "service": "CloudFront Functions", + "resources": len(resources), + }).Info("Fetched resources") + return resources, nil +} From 65cebbdce32792a3efb5e55142b219341fa9a9cf Mon Sep 17 00:00:00 2001 From: jolo-dev Date: Sun, 22 Oct 2023 14:58:13 +0200 Subject: [PATCH 2/7] (feature/0457-support-amazon-cloudfront-function): Add policy and dependency --- docs/configuration/cloud-providers/aws.mdx | 1 + policy.json | 1 + providers/aws/aws.go | 1 + 3 files changed, 3 insertions(+) diff --git a/docs/configuration/cloud-providers/aws.mdx b/docs/configuration/cloud-providers/aws.mdx index fc1a827b9..55c6411eb 100644 --- a/docs/configuration/cloud-providers/aws.mdx +++ b/docs/configuration/cloud-providers/aws.mdx @@ -9,6 +9,7 @@ sidebar_label: Amazon Web Services - API Gateway - Access control lists - CloudFront distributions + - CloudFront functions - CloudWatch Dashboards - CloudWatch alarms - CloudWatch metrics diff --git a/policy.json b/policy.json index cd321e529..b5f6d1852 100644 --- a/policy.json +++ b/policy.json @@ -8,6 +8,7 @@ "apigateway:GET", "cloudwatch:GetMetricStatistics", "cloudfront:ListDistributions", + "cloudfront:Functions", "cloudfront:ListTagsForResource", "cloudwatch:DescribeAlarms", "cloudwatch:ListTagsForResource", diff --git a/providers/aws/aws.go b/providers/aws/aws.go index 8e2f7df02..a7104755d 100644 --- a/providers/aws/aws.go +++ b/providers/aws/aws.go @@ -58,6 +58,7 @@ func listOfSupportedServices() []providers.FetchDataFunction { ec2.Instances, eks.KubernetesClusters, cloudfront.Distributions, + cloudfront.Functions, dynamodb.Tables, ecs.Clusters, ecs.TaskDefinitions, From e85c0a2d9afead1ad45cf03617a55c47c0c820ef Mon Sep 17 00:00:00 2001 From: JoLo <54506108+jolo-dev@users.noreply.github.com> Date: Sat, 28 Oct 2023 17:05:38 +0200 Subject: [PATCH 3/7] Update providers/aws/cloudfront/functions.go Co-authored-by: Bishal Das <70086051+bishal7679@users.noreply.github.com> --- providers/aws/cloudfront/functions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/aws/cloudfront/functions.go b/providers/aws/cloudfront/functions.go index fc8a40440..f62e4da67 100644 --- a/providers/aws/cloudfront/functions.go +++ b/providers/aws/cloudfront/functions.go @@ -87,7 +87,7 @@ func Functions(ctx context.Context, client ProviderClient) ([]Resource, error) { resources = append(resources, Resource{ Provider: "AWS", Account: client.Name, - Service: "CloudFront Functions", + Service: "CloudFront", ResourceId: *function.FunctionMetadata.FunctionARN, Region: client.AWSClient.Region, Name: *function.Name, From bdc474fdb3decfb5d8e1748ef23ca10cc2244b50 Mon Sep 17 00:00:00 2001 From: JoLo <54506108+jolo-dev@users.noreply.github.com> Date: Sat, 28 Oct 2023 17:05:44 +0200 Subject: [PATCH 4/7] Update providers/aws/cloudfront/functions.go Co-authored-by: Bishal Das <70086051+bishal7679@users.noreply.github.com> --- providers/aws/cloudfront/functions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/aws/cloudfront/functions.go b/providers/aws/cloudfront/functions.go index f62e4da67..88b832b4a 100644 --- a/providers/aws/cloudfront/functions.go +++ b/providers/aws/cloudfront/functions.go @@ -107,7 +107,7 @@ func Functions(ctx context.Context, client ProviderClient) ([]Resource, error) { "provider": "AWS", "account": client.Name, "region": client.AWSClient.Region, - "service": "CloudFront Functions", + "service": "CloudFront", "resources": len(resources), }).Info("Fetched resources") return resources, nil From 66de846214afb6a3f4f402d699a305f98ba7cf4d Mon Sep 17 00:00:00 2001 From: JoLo <54506108+jolo-dev@users.noreply.github.com> Date: Sat, 28 Oct 2023 17:05:49 +0200 Subject: [PATCH 5/7] Update providers/aws/cloudfront/functions.go Co-authored-by: Bishal Das <70086051+bishal7679@users.noreply.github.com> --- providers/aws/cloudfront/functions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/aws/cloudfront/functions.go b/providers/aws/cloudfront/functions.go index 88b832b4a..1306db26b 100644 --- a/providers/aws/cloudfront/functions.go +++ b/providers/aws/cloudfront/functions.go @@ -40,7 +40,7 @@ func Functions(ctx context.Context, client ProviderClient) ([]Resource, error) { metricsInvocationsOutput, err := cloudwatchClient.GetMetricStatistics(ctx, &cloudwatch.GetMetricStatisticsInput{ StartTime: aws.Time(utils.BeginningOfMonth(time.Now())), EndTime: aws.Time(time.Now()), - MetricName: aws.String("Invocations"), + MetricName: aws.String("FunctionInvocations"), Namespace: aws.String("AWS/CloudFront"), Dimensions: []types.Dimension{ types.Dimension{ From 03887ccb21a46fbc3423010e68fdbb185f8a8728 Mon Sep 17 00:00:00 2001 From: JoLo <54506108+jolo-dev@users.noreply.github.com> Date: Sun, 29 Oct 2023 09:36:36 +0100 Subject: [PATCH 6/7] Update providers/aws/cloudfront/functions.go Co-authored-by: Azanul Haque <42029519+Azanul@users.noreply.github.com> --- providers/aws/cloudfront/functions.go | 1 + 1 file changed, 1 insertion(+) diff --git a/providers/aws/cloudfront/functions.go b/providers/aws/cloudfront/functions.go index 1306db26b..eda719aba 100644 --- a/providers/aws/cloudfront/functions.go +++ b/providers/aws/cloudfront/functions.go @@ -18,6 +18,7 @@ import ( const ( freeTierInvocations = 2000000 + costPerInvocation = 0.0000001 ) func Functions(ctx context.Context, client ProviderClient) ([]Resource, error) { From cbad52d9fb0ffabce01825a9a4f90996283cc9bf Mon Sep 17 00:00:00 2001 From: JoLo <54506108+jolo-dev@users.noreply.github.com> Date: Sun, 29 Oct 2023 09:36:43 +0100 Subject: [PATCH 7/7] Update providers/aws/cloudfront/functions.go Co-authored-by: Azanul Haque <42029519+Azanul@users.noreply.github.com> --- providers/aws/cloudfront/functions.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/providers/aws/cloudfront/functions.go b/providers/aws/cloudfront/functions.go index eda719aba..787017505 100644 --- a/providers/aws/cloudfront/functions.go +++ b/providers/aws/cloudfront/functions.go @@ -68,7 +68,7 @@ func Functions(ctx context.Context, client ProviderClient) ([]Resource, error) { invocations -= freeTierInvocations } - monthlyCost := invocations * 0.0000001 + monthlyCost := invocations * costPerInvocation outputTags, err := cloudfrontClient.ListTagsForResource(ctx, &cloudfront.ListTagsForResourceInput{ Resource: function.FunctionMetadata.FunctionARN,