AWS Spot Instance Request Tags Won't Apply to Instance

Terraform AWS Provider Version

hashicorp/aws v4.0.0

I have an aws_spot_instance_request like this:

resource “aws_spot_instance_request” “test” {
ami = “{var.amitest}" key_name = "{var.key}”
count = 5
spot_price = “{var.price}" instance_type = "c5.18xlarge" subnet_id = "{var.subnet}”
vpc_security_group_ids = [ “${var.securityGroup}” ]
}

tags {
  Name = "server100${count.index}"
}

}

The tags are being applied only to the spot request itself, not to the underlying instance. How can I fix this?

Thanks!

@twoheadedwolf

I imagine you could use the aws_ec2_tag resource here: Terraform Registry

The tag would use the id provided from the output of the aws_spot_instance_request documented here: Terraform Registry

Something like:

resource "aws_spot_instance_request" "cheap_worker" {
  ami           = "ami-1234"
  spot_price    = "0.03"
  instance_type = "c4.xlarge"
}

resource "aws_ec2_tag" "example" {
  resource_id = aws_spot_instance_request.cheap_worker.spot_instance_id
  key         = "Name"
  value       = "Hello World"
}

2 Likes