From d9aa192cd1a35c9c566765404e1c5cd26ad1c35f Mon Sep 17 00:00:00 2001 From: schulze Date: Tue, 28 Mar 2023 16:09:14 +0200 Subject: [PATCH] move a hardcoded instance (not working) --- mtdaws/mtd.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 mtdaws/mtd.go diff --git a/mtdaws/mtd.go b/mtdaws/mtd.go new file mode 100644 index 0000000..cbd1d4c --- /dev/null +++ b/mtdaws/mtd.go @@ -0,0 +1,46 @@ +package mtdaws + +import ( + "fmt" + + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/thefeli73/polemos/state" +) + +// AWSMoveInstance moves a specified instance to a new availability region +func AWSMoveInstance(config state.Config) (state.Config) { + instance := config.MTD.Services[0] // for testing use first instance + region, instanceID := DecodeCloudID(instance.CloudID) + awsConfig := NewConfig(region, config.AWS.CredentialsPath) + svc := ec2.NewFromConfig(awsConfig) + + realInstance, err := getInstanceDetailsFromString(svc, instanceID) + if err != nil { + fmt.Println("Error getting instance details:", err) + return config + } + + imageName, err := createImage(svc, instanceID) + if err != nil { + fmt.Println("Error creating image:", err) + return config + } + fmt.Println("Created image: ", imageName) + + newInstanceID, err := launchInstance(svc, realInstance, imageName) + if err != nil { + fmt.Println("Error launching instance:", err) + return config + } + fmt.Println("Launched new instance:", newInstanceID) + + err = terminateInstance(svc, instanceID) + if err != nil { + fmt.Println("Error terminating instance:", err) + return config + } + fmt.Println("Terminated original instance:", instanceID) + + + return config +}