Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/aws: tag the spot instance. #4380

Closed
wants to merge 1 commit into from
Closed

provider/aws: tag the spot instance. #4380

wants to merge 1 commit into from

Conversation

fanxu
Copy link

@fanxu fanxu commented Dec 18, 2015

Fix #3263

@phinze
Copy link
Contributor

phinze commented Jan 6, 2016

Thanks for this! Can we add an acceptance test (or enhance an existing test) to verify that tags are set on the instance and cover this behavior?

@radeksimko radeksimko added the waiting-response An issue/pull request is waiting for a response from the community label Jan 7, 2016
@radeksimko radeksimko mentioned this pull request Jan 7, 2016
7 tasks
@caarlos0
Copy link
Contributor

Nice

@dmuysson
Copy link

dmuysson commented Mar 4, 2016

We could really use this capability - hoping it gets added soon!

@harlow
Copy link

harlow commented Mar 18, 2016

+1

@johnnyshields
Copy link

johnnyshields commented May 20, 2016

Not to split hairs here but there is technically a difference between tagging the SpotInstanceRequest itself versus tagging the EC2 Instance which it generates. Maybe using a different attribute like instance_tags for the instance (while keeping tags for the SIR) would be more accurate?

(Or since tags on the EC2 instance is the far more commonly used behavior, you could make spot_instance_request_tags for the SIR)

@johnnyshields
Copy link

Another comment: what happens with persistent requests when the instance is terminated and a new instance is later auto-created, presumably the new instance will no longer have the tags?

@fctucker
Copy link

+1 for this pull request to be accepted

@johnnyshields
Copy link

@fctucker one thing to note here is that with persistent spot requests, if the bid price is hit and the instance stops and starts again, the tags will be not be added to the newly spawned instance.

While this PR can behave as a stop-gap, I think everyone who wants support for this should ask AWS to support PropagateAtLaunch for SpotInstanceRequest tags in the same way it works for AutoScalingGroup tags, as that's a much better solution.

@jedi4ever
Copy link

👍 to get this merged ; ideally it's added as an option = tag_instance true/false with default behaviour to false to be backwards compatible

@EntilZha
Copy link

Recently ran into this as well, would be great to get merged

@stack72
Copy link
Contributor

stack72 commented Aug 15, 2016

FWIW, I think that @johnnyshields is correct here - I am going to open a feature request to AWS about this - feels like propagate_at_launch would be a good thing to have

@stack72
Copy link
Contributor

stack72 commented Aug 15, 2016

just opened this aws/aws-sdk-go#804

@stack72
Copy link
Contributor

stack72 commented Aug 15, 2016

FYI, the tests for this PR work as expected:

% make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSSpotInstanceRequest_'                2 ↵
==> Checking that code complies with gofmt requirements...
/Users/stacko/Code/go/bin/stringer
go generate $(go list ./... | grep -v /terraform/vendor/)
2016/08/15 13:22:41 Generated command/internal_plugin_list.go
TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSSpotInstanceRequest_ -timeout 120m
=== RUN   TestAccAWSSpotInstanceRequest_basic
--- PASS: TestAccAWSSpotInstanceRequest_basic (147.58s)
=== RUN   TestAccAWSSpotInstanceRequest_withBlockDuration
--- PASS: TestAccAWSSpotInstanceRequest_withBlockDuration (134.29s)
=== RUN   TestAccAWSSpotInstanceRequest_vpc
--- PASS: TestAccAWSSpotInstanceRequest_vpc (174.33s)
=== RUN   TestAccAWSSpotInstanceRequest_SubnetAndSG
--- PASS: TestAccAWSSpotInstanceRequest_SubnetAndSG (167.04s)
PASS
ok      github.com/hashicorp/terraform/builtin/providers/aws    623.262s

I made one modification, I check to see if the instance has the tags as expected

func testAccCheckAWSSpotInstanceRequest_InstanceTags(
    sir *ec2.SpotInstanceRequest) resource.TestCheckFunc {
    return func(s *terraform.State) error {
        conn := testAccProvider.Meta().(*AWSClient).ec2conn
        resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{
            InstanceIds: []*string{sir.InstanceId},
        })
        if err != nil {
            if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" {
                return fmt.Errorf("Spot Instance not found")
            }
            return err
        }

        // If nothing was found, then return no state
        if len(resp.Reservations) == 0 {
            return fmt.Errorf("Spot Instance not found")
        }

        instance := resp.Reservations[0].Instances[0]

        var tagMatch bool
        for _, t := range instance.Tags {
            if *t.Value == "terraform-test" {
                tagMatch = true
            }
        }

        if !tagMatch {
            return fmt.Errorf("Error in matching Spot Instance Tags, expected 'terraform-test', got %s", instance.Tags[0].Value)
        }

        return nil
    }
}

@stack72
Copy link
Contributor

stack72 commented Aug 15, 2016

We can wait and see what AWS has to say before merging this as a stop-gap

@stack72 stack72 self-assigned this Aug 15, 2016
@radeksimko
Copy link
Member

It would be super helpful to document that tags are only attached/detached if wait_for_fulfillment = true (default is false, so by default Terraform won't assign/change any tags). It may be obvious for us who know the implementation details behind it, but it may not be for a common user.

@mitchellh mitchellh removed the waiting-response An issue/pull request is waiting for a response from the community label Dec 1, 2016
@stack72 stack72 removed their assignment Jan 17, 2017
@elatt
Copy link

elatt commented Sep 21, 2017

AWS now supports tagging natively for spot instances/fleets so this work-around should no longer be necessary if terraform updates its API params: https://aws.amazon.com/about-aws/whats-new/2017/07/tag-your-spot-fleet-ec2-instances/

@johnnyshields
Copy link

This PR should be closed and we should raise a new PR to support it natively.

@radeksimko
Copy link
Member

Closing for the reasons mentioned. There's a new PR in the new repository also: hashicorp/terraform-provider-aws#1366

@radeksimko radeksimko closed this Sep 25, 2017
@c0d5x
Copy link

c0d5x commented Nov 22, 2018

PR #1366 does NOT fix this, instead if fixes the spot fleet requests. This PR is created for spot instances, so it is still needed.

@ghost
Copy link

ghost commented Mar 31, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Mar 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

aws_spot_instance_request tags won't apply to instance