Today I was fiddling a bit with AWS for a personal project (a PoC actually), so I begin writing my terraform code as usual. I never used spot instances but it seemed a good idea in this case, since the project isn't really a mission-critical one, and the savings were huge (~60%)...so why not?
I headed to the excellent terraform documentation for the relevant resource, which basically says it inherits the same arguments as its brother aws_instance, plus some options specific to the spot instances (eg. the bid price). It also exports the same attributes, in my case I was interested in the ubiquitous public_ip one, which I usually always include in the outputs (you want to know how to reach your instances right after creation, don't you?).
So I just fired terraform apply:
Flatiron:zoom simone$ terraform apply -auto-approve
aws_vpc.example_vpc: Creating...
aws_key_pair.example_kp: Creating...
aws_key_pair.example_kp: Creation complete after 1s [id=simone]
aws_vpc.example_vpc: Creation complete after 4s [id=vpc-0c273f252dd84cfb2]
aws_internet_gateway.example_ig: Creating...
aws_subnet.example_subnet: Creating...
aws_security_group.example_sg: Creating...
aws_subnet.example_subnet: Creation complete after 2s [id=subnet-0446fe6f6ffc33dd8]
aws_internet_gateway.example_ig: Creation complete after 2s [id=igw-0fa90dbb2cb9d9aa0]
aws_route.example_rt: Creating...
aws_route.example_rt: Creation complete after 1s [id=r-rtb-065a3e5d422b3471d1080289494]
aws_security_group.example_sg: Creation complete after 4s [id=sg-05349c2e570029b9f]
aws_spot_instance_request.example_instance: Creating...
aws_spot_instance_request.example_instance: Creation complete after 2s [id=sir-jamr5ipq]
Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
Everything seems fine...but hey, where's my output?! Maybe I forgot it? Double check, seems not:
Flatiron:zoom simone$ cat outputs.tf
output "instance_ip_addr" {
value = aws_spot_instance_request.example_instance.public_ip
}
Weird...I thought there could be a bug with the AWS provider, but it's probably the most used one, so it seemed quite unlikely. World's best computer expert just suggests an unresolved github issue so I'm quite on my own. And the bug theory again sounds far-fetched.
Ok, double checked the documentation and after a few minutes this caught my eye:
wait_for_fulfillment
- (Optional; Default: false) If set, Terraform will wait for the Spot Request to be fulfilled, and will throw an error if the timeout of 10m is reached.
What!? That really was one of those AHA! moments; obviously if this is not set, terraform will not wait for the instance to be up...so it can't know any attribute 🤦♂️
Ok, let's try terraform apply again after adding that directive:
Flatiron:zoom simone$ terraform apply -auto-approve
aws_key_pair.example_kp: Creating...
aws_vpc.example_vpc: Creating...
aws_key_pair.example_kp: Creation complete after 0s [id=simone]
aws_vpc.example_vpc: Creation complete after 9s [id=vpc-05ad79e3f88824d4c]
aws_internet_gateway.example_ig: Creating...
aws_subnet.example_subnet: Creating...
aws_security_group.example_sg: Creating...
aws_subnet.example_subnet: Creation complete after 2s [id=subnet-03d76d2a22dc3e123]
aws_internet_gateway.example_ig: Creation complete after 2s [id=igw-02d6fc46feef75373]
aws_route.example_rt: Creating...
aws_route.example_rt: Creation complete after 1s [id=r-rtb-0f2b46b10523b7e051080289494]
aws_security_group.example_sg: Creation complete after 4s [id=sg-0eebba442f1560a15]
aws_spot_instance_request.example_instance: Creating...
aws_spot_instance_request.example_instance: Still creating... [10s elapsed]
aws_spot_instance_request.example_instance: Creation complete after 12s [id=sir-n4vi6dkp]
Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
Outputs:
instance_ip_addr = 34.244.XX.XXX
Profit!
TL;DR: if you're missing outputs with aws_spot_instance_request, double check the wait_for_fulfillment directive!