From 2bde854c965bf3cee09f18190c9eb9962d1c8843 Mon Sep 17 00:00:00 2001 From: EKS Distro PR Bot <75336432+eks-distro-pr-bot@users.noreply.github.com> Date: Fri, 6 Dec 2024 19:35:43 -0500 Subject: [PATCH] Get a random subnet id from a list of subnet ids (#9056) Co-authored-by: Pankti Shah --- 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))] +}