1

I’m spinning up a spot instance as you can see in below config and then trying to get the IP address from the spot. It seems to work fine with a regular ec2 instance (ie. that is not spot instance).

The error that I get is:

aws_route53_record.staging: Resource ‘aws_spot_instance_request.app-ec2’ does not have attribute ‘public_ip’ for variable ‘aws_spot_instance_request.app-ec2.public_ip’

Here is the config that I’m using:

resource "aws_spot_instance_request" "app-ec2" {
    ami = "ami-1c999999"
    spot_price    = "0.008"
    instance_type = "t2.small"
    tags {
        Name = "${var.app_name}"
    }
    key_name = "mykeypair"
    associate_public_ip_address = true
    vpc_security_group_ids = ["sg-99999999"]
    subnet_id = "subnet-99999999"
    iam_instance_profile = "myInstanceRole"
    user_data = <<-EOF
#!/bin/bash
echo ECS_CLUSTER=APP-STAGING >> /etc/ecs/ecs.config
    EOF
}

resource "aws_route53_record" "staging" {
   zone_id = "XXXXXXXX"
   name = "staging.myapp.com"
   type = "A"
   ttl = "300"
   records = ["${aws_spot_instance_request.app-ec2.public_ip}"]

The spot request is fulfilled on the AWS Console as per below: enter image description here

Any help will be greatly appreciated!

Aaron
  • 2,672
  • 10
  • 28
  • 45

2 Answers2

1

So I've been trying to figure this out since last night and kept seeing the spot instance request being fulfilled via the AWS Console. Likewise, I could see the public IP for the spot and this was misleading me.

It turns out I was missing 1 line (argument) in my script:

wait_for_fulfillment = true

By default, it is set to false, and therefore when I tried to set the public_ip address it simply did not exist at that time.

Now Terraform will wait for the Spot Request to be fulfilled. According to the documentation, it will throw an error if the timeout of 10m is reached.

Aaron
  • 2,672
  • 10
  • 28
  • 45
0

I tried the code snippet you provided with Terraform version 0.12.10 and got the same error. I checked the terraform.tfstate file and saw that the fields were not populated yet (for example private_ip, public_ip, and public_dns were set to null). I checked the "Spot Requests" section in the AWS Console and saw the following Status: price-too-low: Your Spot request price of 0.0075 is lower than the minimum required Spot request fulfillment price of 0.008. The request state was still open so this is why all the variables in the state file were set to null.

Salim
  • 408
  • 3
  • 11
  • Thanks for the quick reply on this. I just ran a quick check with my terraform script and see that the spot request is fulfilled in my AWS Console with spot price 0.008. I've updated the above in the post to include the screenshot. Still not sure on my original question, how I cannot get the public_ip? – Aaron May 05 '20 at 23:15
  • Th error message `attribute ‘public_ip’ for variable ‘aws_spot_instance_request.app-ec2.public_ip’` suggests that you have a syntax error when trying to access the `public_ip` variable from the variable `aws_spot_instance_request.app-ec2.public_ip`. Did you type `public_ip` twice for instance inside the variable? The following works fine `"${aws_spot_instance_request.app-ec2.public_ip}"` if the spot instance request has been granted. – Salim May 06 '20 at 09:14
  • That's what I thought too but I didn't type `public_ip` twice, it only appears once – Aaron May 06 '20 at 09:43
  • Salim, thanks for your help. I just figured this out and it seems that I needed to wait for fulfillment to be completed. Once I added it the public_ip works correctly. :) – Aaron May 06 '20 at 10:00