From e30fba38fc5589a855424c6f97b8c18fb4bf901b Mon Sep 17 00:00:00 2001 From: Sanne Raymaekers Date: Tue, 6 Aug 2024 10:54:04 +0200 Subject: [PATCH] cloud/awscloud: handle nil describe output when creating LTs/SGs --- internal/cloud/awscloud/secure-instance.go | 30 ++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/internal/cloud/awscloud/secure-instance.go b/internal/cloud/awscloud/secure-instance.go index 87bfbf723..034c1b677 100644 --- a/internal/cloud/awscloud/secure-instance.go +++ b/internal/cloud/awscloud/secure-instance.go @@ -294,15 +294,17 @@ func (a *AWS) createOrReplaceSG(hostInstanceID, hostIP, vpcID string) (string, e if err != nil && !isInvalidGroupNotFoundErr(err) { return "", err } - for _, sg := range descrSGOutput.SecurityGroups { - _, err := a.ec2.DeleteSecurityGroup( - context.Background(), - &ec2.DeleteSecurityGroupInput{ - GroupId: sg.GroupId, - }, - ) - if err != nil { - return "", err + if descrSGOutput != nil { + for _, sg := range descrSGOutput.SecurityGroups { + _, err := a.ec2.DeleteSecurityGroup( + context.Background(), + &ec2.DeleteSecurityGroupInput{ + GroupId: sg.GroupId, + }, + ) + if err != nil { + return "", err + } } } @@ -384,7 +386,12 @@ func (a *AWS) createOrReplaceLT(hostInstanceID, imageID, sgID, iamProfile, keyNa }, }, ) - if len(descrLTOutput.LaunchTemplates) == 1 { + + if err != nil && !isLaunchTemplateNotFoundError(err) { + return "", err + } + + if descrLTOutput != nil && len(descrLTOutput.LaunchTemplates) == 1 { _, err := a.ec2.DeleteLaunchTemplate( context.Background(), &ec2.DeleteLaunchTemplateInput{ @@ -395,9 +402,6 @@ func (a *AWS) createOrReplaceLT(hostInstanceID, imageID, sgID, iamProfile, keyNa return "", err } } - if err != nil && !isLaunchTemplateNotFoundError(err) { - return "", err - } input := &ec2.CreateLaunchTemplateInput{ LaunchTemplateData: &ec2types.RequestLaunchTemplateData{