From 8cb672a8ea45139fa72fdcc4255f90fdf855b0d1 Mon Sep 17 00:00:00 2001 From: schulze Date: Tue, 2 May 2023 13:33:33 +0200 Subject: [PATCH] Keep instance name when migrating --- mtdaws/utils.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/mtdaws/utils.go b/mtdaws/utils.go index 1726fca..a080125 100644 --- a/mtdaws/utils.go +++ b/mtdaws/utils.go @@ -162,11 +162,17 @@ func launchInstance(svc *ec2.Client, oldInstance *types.Instance, imageID string for i, sg := range oldInstance.SecurityGroups { securityGroupIds[i] = aws.ToString(sg.GroupId) } - // TODO: select random zone that is not the current one. availabilityZone, err := getRandomDifferentAvailabilityZone(svc, oldInstance, region) if err != nil { return "", err } + var nameTag string + for _, tag := range oldInstance.Tags { + if aws.ToString(tag.Key) == "Name" { + nameTag = aws.ToString(tag.Value) + break + } + } input := &ec2.RunInstancesInput{ ImageId: aws.String(imageID), @@ -178,6 +184,17 @@ func launchInstance(svc *ec2.Client, oldInstance *types.Instance, imageID string Placement: &types.Placement{ AvailabilityZone: aws.String(availabilityZone), }, + TagSpecifications: []types.TagSpecification{ + { + ResourceType: types.ResourceTypeInstance, + Tags: []types.Tag{ + { + Key: aws.String("Name"), + Value: aws.String(nameTag), + }, + }, + }, + }, } output, err := svc.RunInstances(context.TODO(), input)