From 8ea855905a73b88328c21389f95432093cf374aa Mon Sep 17 00:00:00 2001 From: Pankti Shah Date: Thu, 5 Dec 2024 15:03:17 -0800 Subject: [PATCH] Get a random subnet id from a list of subnet ids --- internal/pkg/ec2/create.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/pkg/ec2/create.go b/internal/pkg/ec2/create.go index 901bc2946126..e1c732575642 100644 --- a/internal/pkg/ec2/create.go +++ b/internal/pkg/ec2/create.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "fmt" "math/rand" + "strings" "time" "github.com/aws/aws-sdk-go/aws" @@ -61,7 +62,7 @@ func CreateInstance(session *session.Session, amiId, key, tag, instanceProfileNa IamInstanceProfile: &ec2.IamInstanceProfileSpecification{ Name: aws.String(instanceProfileName), }, - SubnetId: aws.String(subnetId), + SubnetId: aws.String(getRandomSubnetID(subnetId)), TagSpecifications: []*ec2.TagSpecification{ { ResourceType: aws.String("instance"), @@ -111,3 +112,8 @@ func isThrottleError(err error) bool { return false } + +func getRandomSubnetID(subnetIDsStr string) string { + subnetIDs := strings.Split(subnetIDsStr, ",") + return subnetIDs[rand.Intn(len(subnetIDs))] +}