Skip to content

Commit

Permalink
Merge pull request #17 from Cloud-Code-AI/14-add-support-for-ec2-inst…
Browse files Browse the repository at this point in the history
…ances

Added Get EC2 Instance Support
  • Loading branch information
sauravpanda authored Nov 27, 2023
2 parents c22feaf + e973bf6 commit afae149
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
Binary file modified cmd/cloudstate
Binary file not shown.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
)

require (
github.com/aws/aws-sdk-go-v2/service/ec2 v1.137.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.8.4 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/aws/aws-sdk-go-v2/internal/v4a v1.2.4 h1:40Q4X5ebZruRtknEZH/bg91sT5pR
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/dynamodb v1.25.3 h1:f5MV/o9V143ZKOxDh/+LLcufe4F8B3gdfg4c5Nwasyg=
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.25.3/go.mod h1:p8SrrAzcuXBoLEgNI7NEw5eHFyvkvEPABS3jSE8xOZg=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.137.1 h1:J/N4ydefXQZIwKBDPtvrhxrIuP/vaaYKnAsy3bKVIvU=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.137.1/go.mod h1:hrBzQzlQQRmiaeYRQPr0SdSx6fdqP+5YcGhb97LCt8M=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.1 h1:rpkF4n0CyFcrJUG/rNNohoTmhtWlFTRI4BsZOh9PvLs=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.10.1/go.mod h1:l9ymW25HOqymeU2m1gbUQ3rUIsTwKs8gYHXkqDQUhiI=
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.2.4 h1:6DRKQc+9cChgzL5gplRGusI5dBGeiEod4m/pmGbcX48=
Expand Down
54 changes: 54 additions & 0 deletions services/awshandler/ec2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
)

type ec2List struct {
Instances []types.Instance `json:"Instances"`
}

// Gets all the lambda functions for a given regions and
// stores the results in output/{region}/lambda/functions.json file
func ListEc2Fn(sdkConfig aws.Config) {
const maxItems = 50

// Create Lambda service client
client := ec2.NewFromConfig(sdkConfig)

// Retrieve the instances
result, err := client.DescribeInstances(context.TODO(), &ec2.DescribeInstancesInput{})
if err != nil {
log.Fatalf("Unable to retrieve instances, %v", err)
}
var instances []types.Instance
// Process and print the instances details
for _, reservation := range result.Reservations {
for _, instance := range reservation.Instances {
instances = append(instances, instance)
}
}

const (
path = "/ec2/instances.json"
)

output := ec2List{
Instances: instances,
}

filepath := parentpath + sdkConfig.Region + path

err = utils.WriteJSONToFile(filepath, output)
if err != nil {
fmt.Println("Error writing lambda function lists")
}

}
2 changes: 2 additions & 0 deletions services/awshandler/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ func StoreAWSData() {
ListLambdaFns(sdkConfig)
// Get all dynamodb tables
DynamoDBListFn(sdkConfig)
// Get EC2 Instance info
ListEc2Fn(sdkConfig)

}

0 comments on commit afae149

Please sign in to comment.