-
Notifications
You must be signed in to change notification settings - Fork 44
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
Conversation
internal/tagging/aws.go
Outdated
return nil, err | ||
rootBlockDevice := args.Block.Body().FirstMatchingBlock("root_block_device", nil) | ||
|
||
if rootBlockDevice == nil { |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
internal/tagging/aws.go
Outdated
volumeTagBlock, err := TagBlock(volumeTagBlockArgs) | ||
if err != nil { | ||
return nil, err | ||
rootBlockDevice := args.Block.Body().FirstMatchingBlock("root_block_device", nil) |
There was a problem hiding this comment.
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
internal/tagging/aws.go
Outdated
} | ||
swappedTagsStrings = append(swappedTagsStrings, volumeTagBlock) | ||
} else { | ||
// tag 'root_block_device' block (if it exists). |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0..*
internal/tagging/aws.go
Outdated
} else { | ||
// tag 'root_block_device' block (if it exists). | ||
if rootBlockDevice != nil { | ||
origArgsBlock := args.Block |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
orebs_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
- if there's any
There was a problem hiding this comment.
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"
}
}
There was a problem hiding this comment.
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
}
}
There was a problem hiding this comment.
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!
Tiny note - Looking at the note of Might be something worth considering. I don't think we can really tell if there's an attached aws_ebs_volume, so maybe adding |
Yeah - I saw it as well. I guess the user will have to fix it manually. (?) |
Hey @TomerHeber , we've discussed the implications of this fix. Please see @chpl 's comments here, here and here This way:
|
@RLRabinowitz I commented here |
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.