How would one get the latest of a SpotInstanceRequ...
# aws
b
How would one get the latest of a SpotInstanceRequest? Are there any examples out there? I'm trying to do something like:
Copy code
var spotOutput = Pulumi.Aws.Ec2.SpotInstanceRequest.Get(spotRequest.GetResourceName(), spotRequest.Id, null, new CustomResourceOptions
                {
                    DependsOn = spotRequest
                });
But it keeps giving me an error: of
Copy code
aws:ec2:SpotInstanceRequest (dev-request-1):
    error: resource 'urn:pulumi:dev::aws-spot::aws:ec2/spotInstanceRequest:SpotInstanceRequest::dev-request-1' registered twice (create and read)
    error: resource 'urn:pulumi:dev::aws-spot::aws:ec2/spotInstanceRequest:SpotInstanceRequest::dev-request-1' registered twice (create and read)
g
I'm not aware of a specific spot instance example, but that error is a general error about re-using the same logical name for the same resource/lookup. Specifically, you can change the first argument in
.Get(spotRequest.GetResourceName(), ...
to a different value to avoid that error.
b
Thanks. The way the doc read, I was thinking it wanted the name of the existing resource to lookup. How do I get the value out of it now though, @gentle-diamond-70147? I've tried:
Copy code
var spotOutput = Output.Create(Pulumi.Aws.Ec2.SpotInstanceRequest.Get($"{spotRequest.GetResourceName()}-{i}", spotRequest.Id, null, new CustomResourceOptions
                {
                    DependsOn = spotRequest
                }));

                
                Console.WriteLine($"{spotOutput.Apply(x => x.SpotInstanceId)}");
And
Copy code
var spotOutput = Pulumi.Aws.Ec2.SpotInstanceRequest.Get($"{spotRequest.GetResourceName()}-{i}", spotRequest.Id, null, new CustomResourceOptions
                {
                    DependsOn = spotRequest
                });

                
                Console.WriteLine($"{spotOutput.SpotInstanceId.Apply(x => x)}");
Neither of those work. I just continue to get `Pulumi.Output`1[System.String]`
g
Can you switch it up...
spotOutput.SpotInstanceId.Apply(x => Console.WriteLine(x));
? Apologies, C# isn't my go-to language, but that's how you would do it in one of the other languages.
b
That gives a syntax error, but let me see if there's a way to change it up.
Nothing seems to be working.
g
Do you get the same `Pulumi.Output`1[System.String]` output? Or are you getting an error?
b
Syntax error:
Sounds like "lifting" is what I would want to do, but doesn't look like it's ready for C# yet? https://www.pulumi.com/docs/intro/concepts/programming-model/#lifting
g
Hmm, let me get someone more knowledgeable than me to chime in. 🙂
b
@best-receptionist-98400 this should give you a little more guidance about how to make a get request and then use it
let me know if this doesn't help
b
Thanks! I will check it out soon!
@broad-dog-22463 without diving in too much yet, the line you have linked to GetVpc is what I wanted to do but there wasn't anything for GetSpotInstanceRequest. I had to use SpotInstanceRequesr.Get. I'm not sure if there's a difference there? Or if I can do the same thing?
b
Spotinst itself may not have a datasource i.e. GetSpotInstanceRequest, but the Get should work the same
I am looking at the SDK now
You shouldn't need the InvokeAsync with that Get
if you have a small repo, I can test out the correct way first thing in the morning - it's 2314 here I'm afraid 😕
b
Ok, I didn't think so. I don't have a repo yet, but it would be easy enough to spin one up if I don't figure it out tonight. Thanks, @broad-dog-22463! Have a good evening!
@broad-dog-22463 - I threw together a repo and tried to detail out my options and what my end goal is. Hopefully it helps, but definitely let me know if you have questions. https://github.com/efleming18/pulumi-examples
t
I haven’t tested it but this would be my approach: https://github.com/efleming18/pulumi-examples/pull/1
b
@tall-librarian-49374 This looks to be working! I was able to leverage the SDK in there to tag the instances. This is great to be able to just run the one command and have everything created and tagged appropriately even given some the AWS limitations. One thing I was wondering. Do I even need to do the "Wait" approach here if I use the
DependsOn
on the
SpotInstanceRequest.Get
? I've run this a few times this morning and it's always worked the first try. I'm assuming because I have the DependsOn as well as the
WaitForFulfillment
set on the initial spot request? Or am I just getting lucky that the value has always been ready the first time?
And thank you!
t
This depends on AWS behavior. The ID should always be there, but I thought you said that the AWS SDK operation may fail for a while.
b
The AWS SDK operation should only fail if the instance isn't there yet. But with how we currently have it set up, that would never be possible now since we check to make sure the
SpotInstanceId
is there before running the SDK operation.