From 3a554568caf9ac35b56a23837991b968954d0c11 Mon Sep 17 00:00:00 2001 From: schulze Date: Wed, 22 Mar 2023 10:47:29 +0100 Subject: [PATCH] aws sdk utils --- mtdaws/sdk.go | 59 ++++++++++++++++++++++++++------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/mtdaws/sdk.go b/mtdaws/sdk.go index 3b055b3..4b066fa 100644 --- a/mtdaws/sdk.go +++ b/mtdaws/sdk.go @@ -9,13 +9,19 @@ import ( "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" - "github.com/google/uuid" "github.com/thefeli73/polemos/state" ) +// AwsInstance is basic info about a single aws instance (instance id, redion, pubIP and privIP) +type AwsInstance struct { + InstanceID string + Region string + PublicIP string + PrivateIP string +} + // NewConfig creates a AWS config for a specific region func NewConfig(region string, credentials string) aws.Config { - // Create a new AWS config cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithSharedConfigFiles([]string{credentials}), config.WithRegion(region)) if err != nil { fmt.Println("Error creating config:", err) @@ -26,9 +32,9 @@ func NewConfig(region string, credentials string) aws.Config { } // IndexInstances scans all configured regions for instances and add them to services -func IndexInstances(config state.Config) { +func IndexInstances(config state.Config) []AwsInstance { + awsInstances := []AwsInstance{} for _, region := range config.AWS.Regions { - //fmt.Println("Listing instances in region:", region) awsConfig := NewConfig(region, config.AWS.CredentialsPath) instances, err := Instances(awsConfig) if err != nil { @@ -36,38 +42,33 @@ func IndexInstances(config state.Config) { continue } + //fmt.Println("Listing instances in region:", region) for _, instance := range instances { - InstanceInfo(awsConfig, *instance.InstanceId) - u := uuid.New() - _ = u + var publicAddr string + if instance.PublicIpAddress != nil { + publicAddr = aws.ToString(instance.PublicIpAddress) + } + awsInstances = append(awsInstances, AwsInstance{ + InstanceID: aws.ToString(instance.InstanceId), + Region: region, + PublicIP: publicAddr, + PrivateIP: aws.ToString(instance.PrivateIpAddress)}) } } + return awsInstances } -// InstanceInfo collects info about a specific instance in a region -func InstanceInfo(config aws.Config, instanceID string) { - // Create a new EC2 service client - svc := ec2.NewFromConfig(config) - - input := &ec2.DescribeInstancesInput{ - InstanceIds: []string{instanceID}, - } - result, err := svc.DescribeInstances(context.TODO(), input) - if err != nil { - fmt.Println("Error describing instance:", err) - return - } - // Print instance information - instance := result.Reservations[0].Instances[0] - fmt.Println("Instance ID:", aws.ToString(instance.InstanceId)) - fmt.Println("Instance Type:", string(instance.InstanceType)) - fmt.Println("AMI ID:", aws.ToString(instance.ImageId)) - fmt.Println("State:", string(instance.State.Name)) - fmt.Println("Availability Zone:", aws.ToString(instance.Placement.AvailabilityZone)) +// PrintInstanceInfo prints info about a specific instance in a region +func PrintInstanceInfo(instance *types.Instance) { + fmt.Println("\tInstance ID:", aws.ToString(instance.InstanceId)) + fmt.Println("\t\tInstance Type:", string(instance.InstanceType)) + fmt.Println("\t\tAMI ID:", aws.ToString(instance.ImageId)) + fmt.Println("\t\tState:", string(instance.State.Name)) + fmt.Println("\t\tAvailability Zone:", aws.ToString(instance.Placement.AvailabilityZone)) if instance.PublicIpAddress != nil { - fmt.Println("Public IP Address:", aws.ToString(instance.PublicIpAddress)) + fmt.Println("\t\tPublic IP Address:", aws.ToString(instance.PublicIpAddress)) } - fmt.Println("Private IP Address:", aws.ToString(instance.PrivateIpAddress)) + fmt.Println("\t\tPrivate IP Address:", aws.ToString(instance.PrivateIpAddress)) } // Instances returns all instances for a config i.e. a region