Skip to content

Commit

Permalink
Merge pull request #99 from Cloud-Code-AI/43-aws-add-autoscaling-groups
Browse files Browse the repository at this point in the history
feat: Added AWS autoscaling
  • Loading branch information
sauravpanda authored Feb 15, 2024
2 parents 6d92e64 + 5fc4e89 commit 3a4b23e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.45.0
)

require github.com/aws/aws-sdk-go-v2/service/autoscaling v1.38.0 // indirect

require (
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.42.5
github.com/aws/aws-sdk-go-v2/service/cloudwatchevents v1.21.6
Expand All @@ -23,12 +25,12 @@ require (
)

require (
github.com/aws/aws-sdk-go-v2 v1.24.1
github.com/aws/aws-sdk-go-v2 v1.25.0
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.4 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.16.4 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.10 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.10 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.2.4 // indirect
github.com/aws/aws-sdk-go-v2/service/apigateway v1.21.6
Expand All @@ -45,5 +47,5 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.17.3 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.20.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.25.4 // indirect
github.com/aws/smithy-go v1.19.0 // indirect
github.com/aws/smithy-go v1.20.0 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/aws/aws-sdk-go-v2 v1.24.0 h1:890+mqQ+hTpNuw0gGP6/4akolQkSToDJgHfQE7Aw
github.com/aws/aws-sdk-go-v2 v1.24.0/go.mod h1:LNh45Br1YAkEKaAqvmE1m8FUx6a5b/V0oAKV7of29b4=
github.com/aws/aws-sdk-go-v2 v1.24.1 h1:xAojnj+ktS95YZlDf0zxWBkbFtymPeDP+rvUQIH3uAU=
github.com/aws/aws-sdk-go-v2 v1.24.1/go.mod h1:LNh45Br1YAkEKaAqvmE1m8FUx6a5b/V0oAKV7of29b4=
github.com/aws/aws-sdk-go-v2 v1.25.0 h1:sv7+1JVJxOu/dD/sz/csHX7jFqmP001TIY7aytBWDSQ=
github.com/aws/aws-sdk-go-v2 v1.25.0/go.mod h1:G104G1Aho5WqF+SR3mDIobTABQzpYV0WxMsKxlMggOA=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.1 h1:ZY3108YtBNq96jNZTICHxN1gSBSbnvIdYwwqnvCV4Mc=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.1/go.mod h1:t8PYl/6LzdAqsU4/9tz28V/kU+asFePvpOMkdul0gEQ=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.5.4 h1:OCs21ST2LrepDfD3lwlQiOqIGp6JiEUqG84GzTDoyJs=
Expand All @@ -16,16 +18,22 @@ github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.9 h1:v+HbZaCGmOwnTTVS86
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.9/go.mod h1:Xjqy+Nyj7VDLBtCMkQYOw1QYfAEZCVLrfI0ezve8wd4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.10 h1:vF+Zgd9s+H4vOXd5BMaPWykta2a6Ih0AKLq/X6NYKn4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.2.10/go.mod h1:6BkRjejp/GR4411UGqkX8+wFMbFbqsUIimfK4XjOKR4=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.0 h1:NPs/EqVO+ajwOoq56EfcGKa3L3ruWuazkIw1BqxwOPw=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.0/go.mod h1:D+duLy2ylgatV+yTlQ8JTuLfDD0BnFvnQRc+o6tbZ4M=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.9 h1:N94sVhRACtXyVcjXxrwK1SKFIJrA9pOJ5yu2eSHnmls=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.9/go.mod h1:hqamLz7g1/4EJP+GH5NBhcUMLjW+gKLQabgyz6/7WAU=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.10 h1:nYPe006ktcqUji8S2mqXf9c/7NdiKriOwMvWQHgYztw=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.5.10/go.mod h1:6UV4SZkVvmODfXKql4LCbaZUpF7HO2BX38FgBf9ZOLw=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.0 h1:ks7KGMVUMoDzcxNWUlEdI+/lokMFD136EL6DWmUOV80=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.0/go.mod h1:hL6BWM/d/qz113fVitZjbXR0E+RCTU1+x+1Idyn5NgE=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.1 h1:uR9lXYjdPX0xY+NhvaJ4dD8rpSRz5VY81ccIIoNG+lw=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.1/go.mod h1:6fQQgfuGmw8Al/3M2IgIllycxV7ZW7WCdVSqfBeUiCY=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.2.4 h1:40Q4X5ebZruRtknEZH/bg91sT5pR853F7/1X9QRbI54=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.2.4/go.mod h1:u77N7eEECzUv7F0xl2gcfK/vzc8wcjWobpy+DcrLJ5E=
github.com/aws/aws-sdk-go-v2/service/apigateway v1.21.6 h1:ePPaOVn92r5n8Neecdpy93hDmR0PBH6H6b7VQCE5vKE=
github.com/aws/aws-sdk-go-v2/service/apigateway v1.21.6/go.mod h1:P/zwE9uiC6eK/kL3CS60lxTTVC2zAvaS4iW31io41V4=
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.38.0 h1:BnElrrgowaG50hoUCbBc5lq5XX7Fr7F4nvZovCDjevk=
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.38.0/go.mod h1:6ioQn0JPZSvTdXmnUAQa9h7x8m+KU63rkgiAD1ZLnqc=
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.42.5 h1:5+m0XrCIwjjeP4f3AdC1wyQBc2ClIJi2mP4e3Wkdgvw=
github.com/aws/aws-sdk-go-v2/service/cloudformation v1.42.5/go.mod h1:oPk8ZMctRUtGC13pOE83Zp0baZgJsmzuKm4IRR+zQOI=
github.com/aws/aws-sdk-go-v2/service/cloudfront v1.31.0 h1:D8FSJvBDs+WLHjZiN1brxI4Vn9OmjhqlIG3mobYFsnA=
Expand Down Expand Up @@ -78,6 +86,8 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.25.4 h1:yEvZ4neOQ/KpUqyR+X0ycUTW/kVR
github.com/aws/aws-sdk-go-v2/service/sts v1.25.4/go.mod h1:feTnm2Tk/pJxdX+eooEsxvlvTWBvDm6CasRZ+JOs2IY=
github.com/aws/smithy-go v1.19.0 h1:KWFKQV80DpP3vJrrA9sVAHQ5gc2z8i4EzrLhLlWXcBM=
github.com/aws/smithy-go v1.19.0/go.mod h1:NukqUGpCZIILqqiV0NIjeFh24kd/FAa4beRb6nbIUPE=
github.com/aws/smithy-go v1.20.0 h1:6+kZsCXZwKxZS9RfISnPc4EXlHoyAkm2hPuM8X2BrrQ=
github.com/aws/smithy-go v1.20.0/go.mod h1:uo5RKksAl4PzhqaAbjd4rLgFoq5koTsQKYuGe7dklGc=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
Expand Down
94 changes: 94 additions & 0 deletions services/awshandler/autoscaling.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package awshandler

import (
"context"
"fmt"
"log"

"github.com/Cloud-Code-AI/cloudstate/services/utils"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/autoscaling/types"
)

type autoscalingInfo struct {
AutoScalingGroups []types.AutoScalingGroup `json:"groups"`
AutoScalingInstances []types.AutoScalingInstanceDetails `json:"instances"`
LaunchConfigurations []types.LaunchConfiguration `json:"metric_streams"`
}

// Gets all the AutoScaling Data for a given regions and
// stores the results in output/{region}/autoscaling/metrics.json file
func ListAutoscalingFn(sdkConfig aws.Config, parentpath string) {
const maxItems = 50

// Create autoscaling service client
client := autoscaling.NewFromConfig(sdkConfig)
data := autoscalingInfo{
AutoScalingGroups: getAutoScalingGroups(client),
AutoScalingInstances: getAutoScalingInstances(client),
LaunchConfigurations: getLaunchConfigurations(client),
}

const (
path = "/autoscaling/data.json"
)

stats := addAutoscalingStats(data)
output := BasicTemplate{
Data: data,
Stats: stats,
}

filepath := parentpath + sdkConfig.Region + path

err := utils.WriteJSONToFile(filepath, output)
if err != nil {
fmt.Println("Error writing autoscaling data")
}

}

func addAutoscalingStats(inp autoscalingInfo) interface{} {
s := make(map[string]float64)
s["autoscaling_groups"] = float64(len(inp.AutoScalingGroups))
s["autoscaling_instances"] = float64(len(inp.AutoScalingInstances))
s["launch_configurations"] = float64(len(inp.LaunchConfigurations))
return s
}

func getAutoScalingGroups(client *autoscaling.Client) []types.AutoScalingGroup {
// Retrieve the AutoScaling dashboard
result, err := client.DescribeAutoScalingGroups(context.TODO(), &autoscaling.DescribeAutoScalingGroupsInput{})
if err != nil {
log.Fatalf("Unable to retrieve autoscaling groups, %v", err)
}
var groups []types.AutoScalingGroup
// TODO: Add pagination updates
groups = result.AutoScalingGroups
return groups
}

func getAutoScalingInstances(client *autoscaling.Client) []types.AutoScalingInstanceDetails {
// Retrieve the AutoScaling Instances
result, err := client.DescribeAutoScalingInstances(context.TODO(), &autoscaling.DescribeAutoScalingInstancesInput{})
if err != nil {
log.Fatalf("Unable to retrieve autoscaling instances, %v", err)
}
var instances []types.AutoScalingInstanceDetails
// TODO: Add pagination updates
instances = result.AutoScalingInstances
return instances
}

func getLaunchConfigurations(client *autoscaling.Client) []types.LaunchConfiguration {
// Retrieve the AutoScaling LaunchConfigurations
result, err := client.DescribeLaunchConfigurations(context.TODO(), &autoscaling.DescribeLaunchConfigurationsInput{})
if err != nil {
log.Fatalf("Unable to retrieve autoscaling configs, %v", err)
}
var configs []types.LaunchConfiguration
// TODO: Add pagination updates
configs = result.LaunchConfigurations
return configs
}
1 change: 1 addition & 0 deletions services/awshandler/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func StoreAWSData(region string, outFolder string) {
ECRMetaData(sdkConfig, parentpath)
codebuildMetadata(sdkConfig, parentpath)
ListVpcFn(sdkConfig, parentpath)
ListAutoscalingFn(sdkConfig, parentpath)

}

Expand Down

0 comments on commit 3a4b23e

Please sign in to comment.