简体   繁体   中英

How can I get Spot InstanceID from a Spot Instance Request?

I'm using aws-go-sdk to create and manage spot instances. Every request I send in order to create a new Spot Instance Request is performed with RequestSpotInstances. I'm trying to get the Instance ID of the instance that was created for my last request, I saw that in the response object RequestSpotInstancesOutput there is a parameter for each instance that was created, but it does not include the instance id, in fact, the InstanceID is not being filled in the RequestSpotInstanceOutput, (returned from RequestSpotInstances call), I tried to use CreateTags in order to create a tag for each instance with the request id, but it didn't work. seen here: TagSpecifications with requestSpotInstances UnexpectedParameter with aws-sdk

param := &ec2.RequestSpotInstancesInput{    // Initializing the request params
    SpotPrice:     aws.String(ctx.Price),
    InstanceCount: aws.Int64(count),
    LaunchSpecification: &ec2.RequestSpotLaunchSpecification{
        ImageId:          aws.String(ctx.AWSImageID),
        InstanceType:     aws.String(ctx.InstanceType),
        KeyName:          aws.String(ctx.Key),
        UserData:         aws.String(""),
        SecurityGroupIds: aws.StringSlice(secgroup),
    },
    Type: aws.String("one-time"),
}

reqSliceCreateTagsInput := make([]string, 1)

resp, err := client.RequestSpotInstances(param)
if err != nil || resp == nil {
    log.Println(err.Error())
    log.Fatalf("pkg/aws: CreateSpotInstance resp is nil")
}
reqSliceCreateTagsInput[0] = *resp.SpotInstanceRequests[0].SpotInstanceRequestId
createTagsInput := &ec2.CreateTagsInput{
    Resources: aws.StringSlice(reqSliceCreateTagsInput),
    Tags: []*ec2.Tag{
        {
            Key:   aws.String("instance"),
            Value: aws.String(name + "-" + index),
        },
    },
}

time.Sleep(time.Second * 15)

_, err = client.CreateTags(createTagsInput)
if err != nil {
    slog.Logf(0, err.Error())
}

Is there any way to just receive the instance id by the request id only?

I think that it can take some time for a Spot Instance request to be fulfilled.

I would recommend you call describe_spot_instance_requests() until the Status of all instances is fulfilled (wait a little bit between each call).

I can't see a field that indicates that the whole request has been fulfilled, so you might need to compare the number of instances returned against the quantity that you requested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM