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

Fix: AWS EC2 volume tag errors #191

Merged
merged 6 commits into from
Mar 6, 2024
Merged

Conversation

TomerHeber
Copy link
Collaborator

resolves #190

Does not add tags to volume_tags if root_block_device block is used. (Per documentation they conflict).
I'm now also tagging root_block_device. I hope it makes sense.

return nil, err
rootBlockDevice := args.Block.Body().FirstMatchingBlock("root_block_device", nil)

if rootBlockDevice == nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the block does not exist - tag volume_tags as usual...

instance_type = "t3.micro"
availability_zone = "us-west-2"

root_block_device {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please also add the volume_tags block here and make sure it's not altered?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't... it will create a confilct.
volume_tags should have been added, but wasn't added because root_block_device exist.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yaronya - based on the documentation, I've made larger changes. (it's less straighforward than I initially assumed).
Please recheck.

See the tag guide:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#tag-guide

@TomerHeber TomerHeber requested a review from yaronya March 1, 2024 13:52
volumeTagBlock, err := TagBlock(volumeTagBlockArgs)
if err != nil {
return nil, err
rootBlockDevice := args.Block.Body().FirstMatchingBlock("root_block_device", nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect we have a similar problem with ebs_block_device too

}
swappedTagsStrings = append(swappedTagsStrings, volumeTagBlock)
} else {
// tag 'root_block_device' block (if it exists).
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0..1

}

// tag 'ebs_block_device' blocks (if any exist).
for _, block := range args.Block.Body().Blocks() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0..*

} else {
// tag 'root_block_device' block (if it exists).
if rootBlockDevice != nil {
origArgsBlock := args.Block
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for swapping back in line 66.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yaronya - please review the input vs expected files.
Need to make sure it's correct, and no missing use-cases.

(tofu plan is successful).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @TomerHeber
we think that the logic should be as following

  • if there's volume_tags (even if there's no root_block_device or ebs_block_device)
    • add tags to it
  • else
    • if there's any ebs_block_device add tags to it
    • add tags to the root_block_device even if it's not exists

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from this

resource "aws_instance" "test" {
  ami           = "ami-0f403e3180720dd7e"
  instance_type = "t3a.nano"
  volume_tags = {
    "c" = "d"
  }
  root_block_device {
    volume_type = "gp3"
  }
  ebs_block_device {
    device_name = "/dev/xvdb"
    volume_type = "gp3"
  }
}

to this

resource "aws_instance" "test" {
  ami           = "ami-0f403e3180720dd7e"
  instance_type = "t3a.nano"
  volume_tags = merge({
    "c" = "d"
  }, local.terratag_added_main)
  root_block_device {
    volume_type = "gp3"
  }
  ebs_block_device {
    device_name = "/dev/xvdb"
    volume_type = "gp3"
  }
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from this

resource "aws_instance" "test" {
  ami           = "ami-0f403e3180720dd7e"
  instance_type = "t3a.nano"
  ebs_block_device {
    device_name = "/dev/xvdb"
    volume_type = "gp3"
  }
}

to this

resource "aws_instance" "test" {
  ami           = "ami-0f403e3180720dd7e"
  instance_type = "t3a.nano"
  root_block_device {
    tags = local.terratag_added_main
  }
  ebs_block_device {
    device_name = "/dev/xvdb"
    volume_type = "gp3"
    tags = local.terratag_added_main
  }
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that makes sense.
I'll update the code. Thanks!

@RLRabinowitz
Copy link
Contributor

Tiny note - Looking at the note of volume_tags, there might be some inconsistent behaviour if the user is adding tags of his own to attached aws_ebs_volume.

Might be something worth considering. I don't think we can really tell if there's an attached aws_ebs_volume, so maybe adding volume_tags here is good enough

@TomerHeber
Copy link
Collaborator Author

Tiny note - Looking at the note of volume_tags, there might be some inconsistent behaviour if the user is adding tags of his own to attached aws_ebs_volume.

Might be something worth considering. I don't think we can really tell if there's an attached aws_ebs_volume, so maybe adding volume_tags here is good enough

Yeah - I saw it as well. I guess the user will have to fix it manually. (?)

@RLRabinowitz
Copy link
Contributor

Hey @TomerHeber , we've discussed the implications of this fix. Please see @chpl 's comments here, here and here

This way:

  • We will not add volume_tags when they are not intended (When they would conflict with the tags of a root_block_device or an ebs_block_device, or when there's an attached volume to the instance)
  • We will add tags to the volumes whether or not volume_tags are defined (ebs_block_device and root_block_device will get the tags from this fix, other aws_ebs_volumes will get the tags from terratag itself)

@chpl
Copy link

chpl commented Mar 6, 2024

@RLRabinowitz I commented here

@TomerHeber TomerHeber requested a review from chpl March 6, 2024 13:55
@TomerHeber TomerHeber merged commit c476801 into master Mar 6, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AWS EC2 volume tag errors
5 participants