From 60a9ad1da80f87ae814e4d8662febfa59be02cba Mon Sep 17 00:00:00 2001 From: Michael Shen Date: Thu, 30 Nov 2023 09:21:18 -0500 Subject: [PATCH] UPSTREAM: 4670: Update awsmachine providerID and instanceID immediately after ec2:RunInstances is called This mitigates issues caused by falling back to tag-based searching for EC2 instances in case future AWS calls fail, such as attaching ENIs to security groups or tagging ENIs. Signed-off-by: Michael Shen --- pkg/cloud/services/ec2/instances.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/cloud/services/ec2/instances.go b/pkg/cloud/services/ec2/instances.go index f62da90efb..73917695a5 100644 --- a/pkg/cloud/services/ec2/instances.go +++ b/pkg/cloud/services/ec2/instances.go @@ -249,6 +249,10 @@ func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, use return nil, err } + // Set the providerID and instanceID as soon as we create an instance so that we keep it in case of errors afterward + scope.SetProviderID(out.ID, out.AvailabilityZone) + scope.SetInstanceID(out.ID) + if len(input.NetworkInterfaces) > 0 { for _, id := range input.NetworkInterfaces { s.scope.Debug("Attaching security groups to provided network interface", "groups", input.SecurityGroupIDs, "interface", id) @@ -260,7 +264,7 @@ func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, use s.scope.Debug("Adding tags on each network interface from resource", "resource-id", out.ID) - // Fetching the network interfaces attached to the specific instanace + // Fetching the network interfaces attached to the specific instance networkInterfaces, err := s.getInstanceENIs(out.ID) if err != nil { return nil, err @@ -268,7 +272,7 @@ func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte, use s.scope.Debug("Fetched the network interfaces") - // Once all the network interfaces attached to the specific instanace are found, the similar tags of instance are created for network interfaces too + // Once all the network interfaces attached to the specific instance are found, the similar tags of instance are created for network interfaces too if len(networkInterfaces) > 0 { s.scope.Debug("Attempting to create tags from resource", "resource-id", out.ID) for _, networkInterface := range networkInterfaces {