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

aws_spot_instance_request tags won't apply to instance #32

Closed
hashibot opened this issue Jun 13, 2017 · 25 comments · Fixed by #31495
Closed

aws_spot_instance_request tags won't apply to instance #32

hashibot opened this issue Jun 13, 2017 · 25 comments · Fixed by #31495
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/ec2 Issues and PRs that pertain to the ec2 service. tags Pertains to resource tagging.
Milestone

Comments

@hashibot
Copy link

This issue was originally opened by @caarlos0 as hashicorp/terraform#3263. It was migrated here as part of the provider split. The original body of the issue is below.


I have an aws_spot_instance_request like this:

resource "aws_spot_instance_request" "seleniumgrid" {
    ami = "${var.amiPuppet}"
    key_name = "${var.key}"
    instance_type = "c3.4xlarge"
    subnet_id = "${var.subnet}"
    vpc_security_group_ids = [ "${var.securityGroup}" ]
    user_data = "${template_file.userdata.rendered}"
    wait_for_fulfillment = true
    spot_price = "${var.price}"
    availability_zone = "${var.zone}"
    instance_initiated_shutdown_behavior = "terminate"
    root_block_device {
      volume_size = 100
      volume_type = "gp2"
    }
    tags {
      Name = "${var.name}.${var.domain}"
      Provider = "Terraform"
      CA_TEAM = "${var.team}"
      CA_ROLE = "${var.role}"
      CA_SERVICE = "${var.service}"
    }
}

The tags are being applied only to the spot request itself, not to the underlying instance. Is this an expected behavior? How can I change this?

Thanks!

@hashibot hashibot added the bug Addresses a defect in current functionality. label Jun 13, 2017
@hashibot
Copy link
Author

This comment was originally opened by @jmreicha as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


I just ran in to this issue as well.

@hashibot
Copy link
Author

This comment was originally opened by @sean-brandt as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


Definitely an issue, also in 0.6.8. Need a way to apply those tags to the new instance, or allow for an aws_instance_tag resource that will allow arbitrary tags to be set on ec2 instances.

@hashibot
Copy link
Author

This comment was originally opened by @caarlos0 as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


Agreed!

@hashibot
Copy link
Author

This comment was originally opened by @stolinzeev as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


Present in 0.6.8 as well

@hashibot
Copy link
Author

This comment was originally opened by @catsby as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


There is no AWS API that will do this via the Spot Instance request itself. We would need to trap the InstanceId (which we do) and then call out to set the tags on that. I don't see a straightforward easy way to do this, but it shouldn't be too hard. I don't know that I'll get to this anytime soon however, but maybe someone in the community will pick it up.

@hashibot
Copy link
Author

This comment was originally opened by @cbarbour as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


This is the expected behavior as per AWS documentation, unfortunately.

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-requests.html#concepts-spot-instances-request-tags

I did find a solution. I created an IAM role with a policy that grants CreateTag permissions. I added an instance_profile to the role, and a ruleset allowing it to be assigned to the instances.

Using cloud init, I have a script that looks up the instances metadata, and using the result, tags itself based on arguments passed to the script.

There are a few moving parts, unfortunately. It helps if you're already familiar with cloud-init.

@hashibot
Copy link
Author

This comment was originally opened by @cbarbour as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


Another option: Use autoscaling groups. Tags applied to the launch_configuration will be passed on to the resulting spot instance.

@hashibot
Copy link
Author

This comment was originally opened by @rehmanzile as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


This issue seems to be open for a year now. Any luck with the fix.

@hashibot
Copy link
Author

This comment was originally opened by @jalkjaer as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


Until there is explicit support, we are getting by with the following user-data snippet to clone the spot-request tags to the instance.
Its somewhat simplistic and lacks error handling, but it does the job for us. It requires that aws cli / curl is available and that the instance has the proper IAM permission of course

#!/bin/bash

REGION=us-east-1
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
SPOT_REQ_ID=$(aws --region $REGION ec2 describe-instances --instance-ids "$INSTANCE_ID"  --query 'Reservations[0].Instances[0].SpotInstanceRequestId' --output text)
if [ "$SPOT_REQ_ID" != "None" ] ; then
  TAGS=$(aws --region $REGION ec2 describe-spot-instance-requests --spot-instance-request-ids "$SPOT_REQ_ID" --query 'SpotInstanceRequests[0].Tags')
  aws --region $REGION ec2 create-tags --resources "$INSTANCE_ID" --tags "$TAGS"
fi

@hashibot
Copy link
Author

This comment was originally opened by @alextbrandwatch as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


Issue still seems to be present in 0.9.4. Causing problems - will attempt the workaround solutions posted here

@hashibot
Copy link
Author

This comment was originally opened by @kostyrev as hashicorp/terraform#3263 (comment). It was migrated here as part of the provider split. The original comment is below.


Workarounds are good but not always applicable.
Please consider adding explicit support.

@birkoff
Copy link

birkoff commented Jul 29, 2017

This is a nice workaround I just implemented with local-exec

resource "aws_spot_instance_request" "platform" {
  ami           = "${var.ami_id}"
  instance_type = "${var.instance_type}"
  key_name      = "${var.key_name}"
  subnet_id     = "${lookup(var.subnet, var.region)}"

  associate_public_ip_address = true
  vpc_security_group_ids      = ["${lookup(var.security_groups, var.region)}"]

  tags = "${var.tags}"

  spot_price    = "0.03"
  wait_for_fulfillment = true

  provisioner "local-exec" {
    command = "./update_spotinstance_tags.sh ${var.region} ${aws_spot_instance_request.platform.id} ${aws_spot_instance_request.platform.spot_instance_id}"
  }
}

update_spotinstance_tags.sh

#!/usr/bin/env bash

#$1 = ${var.region} 
#$2 = ${aws_spot_instance_request.platform.id} 
#$3 = ${aws_spot_instance_request.platform.spot_instance_id}

aws --region $1 ec2 describe-spot-instance-requests --spot-instance-request-ids $2 --query 'SpotInstanceRequests[0].Tags' > tags.json
aws ec2 create-tags --resources $3 --tags file://tags.json
rm -f tags.json

@radeksimko radeksimko added the service/ec2 Issues and PRs that pertain to the ec2 service. label Jan 25, 2018
@bflad bflad added enhancement Requests to existing resources that expand the functionality or scope. and removed bug Addresses a defect in current functionality. labels Jul 30, 2018
@razorsedge
Copy link

I would expect that this can be implemented via something similar to aws_instance's volume_tags. Call it instance_tags.

@razorsedge
Copy link

And I just realized that volume_tags are also not being passed from the Spot request to the instance's volumes.

@nbrys
Copy link
Contributor

nbrys commented Dec 11, 2018

Are you guys planning on fixing this?

@nunofernandes
Copy link

Any news on this? Thanks

@raelga
Copy link

raelga commented Sep 1, 2019

+1

@demisx
Copy link

demisx commented Mar 18, 2020

Please fix this. Currently implementing ugly workarounds in order to resolve this issue.

@CpuID
Copy link
Contributor

CpuID commented Mar 24, 2020

+1 to some implementation here

@rroque6428
Copy link

rroque6428 commented May 5, 2020

@birkoff Saved my life. I had to modify a bit to avoid file creation. Tks!

#!/usr/bin/env bash

#$1 = ${var.region} 
#$2 = ${aws_spot_instance_request.platform.id} 
#$3 = ${aws_spot_instance_request.platform.spot_instance_id}

TAGS=$(aws ec2 describe-spot-instance-requests \
--region $1 \
--spot-instance-request-ids $2 \
--query 'SpotInstanceRequests[0].Tags')

aws ec2 create-tags --region $1 --resources $3 --tags "$TAGS"

@ewbankkit
Copy link
Contributor

ewbankkit commented Aug 18, 2020

The new(ish) aws_ec2_tag resource can be used to tag the created EC2 instance, for example:

data "aws_ami" "amzn-ami-minimal-hvm-ebs" {
  most_recent = true
  owners      = ["amazon"]

  filter {
    name   = "name"
    values = ["amzn-ami-minimal-hvm-*"]
  }

  filter {
    name   = "root-device-type"
    values = ["ebs"]
  }
}

data "aws_ec2_instance_type_offering" "available" {
  filter {
    name   = "instance-type"
    values = ["t3.micro", "t2.micro"]
  }

  preferred_instance_types = ["t3.micro", "t2.micro"]
}

resource "aws_spot_instance_request" "example" {
  ami           = data.aws_ami.amzn-ami-minimal-hvm-ebs.id
  instance_type = data.aws_ec2_instance_type_offering.available.instance_type

  spot_price = "0.05"

  wait_for_fulfillment = true
}

locals {
  tags = {
    Environment = "Test"
    Name        = "Example"
  }
}

resource "aws_ec2_tag" "example" {
  resource_id = aws_spot_instance_request.example.spot_instance_id

  for_each = local.tags
  key      = each.key
  value    = each.value
}

@github-actions
Copy link

github-actions bot commented Aug 9, 2022

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

@github-actions github-actions bot added the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Aug 9, 2022
@cowwoc
Copy link

cowwoc commented Aug 23, 2022

Still relevant.

@github-actions github-actions bot removed the stale Old or inactive issues managed by automation, if no further action taken these will get closed. label Aug 23, 2022
@ewbankkit ewbankkit added the tags Pertains to resource tagging. label Jun 5, 2023
@github-actions github-actions bot added this to the v5.2.0 milestone Jun 6, 2023
@github-actions
Copy link

github-actions bot commented Jun 9, 2023

This functionality has been released in v5.2.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/ec2 Issues and PRs that pertain to the ec2 service. tags Pertains to resource tagging.
Projects
None yet
Development

Successfully merging a pull request may close this issue.