Keep instance name when migrating

This commit is contained in:
schulze 2023-05-02 13:33:33 +02:00
parent 126c0dbebb
commit 8cb672a8ea

View File

@ -162,11 +162,17 @@ func launchInstance(svc *ec2.Client, oldInstance *types.Instance, imageID string
for i, sg := range oldInstance.SecurityGroups { for i, sg := range oldInstance.SecurityGroups {
securityGroupIds[i] = aws.ToString(sg.GroupId) securityGroupIds[i] = aws.ToString(sg.GroupId)
} }
// TODO: select random zone that is not the current one.
availabilityZone, err := getRandomDifferentAvailabilityZone(svc, oldInstance, region) availabilityZone, err := getRandomDifferentAvailabilityZone(svc, oldInstance, region)
if err != nil { if err != nil {
return "", err 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{ input := &ec2.RunInstancesInput{
ImageId: aws.String(imageID), ImageId: aws.String(imageID),
@ -178,6 +184,17 @@ func launchInstance(svc *ec2.Client, oldInstance *types.Instance, imageID string
Placement: &types.Placement{ Placement: &types.Placement{
AvailabilityZone: aws.String(availabilityZone), 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) output, err := svc.RunInstances(context.TODO(), input)