Reputation: 413
I've faced an issue while creating tags for network interfaces, in AWS
I've created an aws_ec2_tag
resource, and tagged each created network interface with nested names of availability zones however, the tags, does not behave as expected (shown in image).
AWS NETWORK INTERFACES
Current appearance:
UNMANAGED | EU-WEST-1A eu-west-1b
UNMANAGED | EU-WEST-1B eu-west-1c
UNMANAGED | EU-WEST-1C eu-west-1a
Expected appearance:
UNMANAGED | EU-WEST-1A eu-west-1a
UNMANAGED | EU-WEST-1B eu-west-1b
UNMANAGED | EU-WEST-1C eu-west-1c
Here is the main.tf
resource "aws_ec2_tag" "def_eni_sqs_private" {
count = length(var.availability_zones)
resource_id = element(flatten([for interface in aws_vpc_endpoint.sqs: interface.network_interface_ids]), count.index)
key = "Name"
value = join(" | ", [ UNMANAGED, upper(element(var.availability_zones, count.index)) ])
}
Please help me to resolve this logic
As requested here is the variable availability_zones
variable "availability_zones" {
type = list(string)
default = data.aws_availability_zones.network_zones.names
}
Upvotes: 1
Views: 329
Reputation: 2123
As I can see in this line count = length(var.availability_zones) you consider the number of interfaces is the same number of Azs but what about if you have more than interface in the same AZ so I think it should be count = length(aws_vpc_endpoint.sqs.network_interface_ids)
another one in the next line is that the return type of aws_vpc_endpoint.sqs.network_interface_ids is a set not alist, so you may casting it to a list. The last one is reading the value of AZ from interface itself, so your code can be something like this:
data "aws_network_interface" "transfer_eni" {
for_each = aws_vpc_endpoint.sqs.network_interface_ids
id = each.value
}
resource "aws_ec2_tag" "def_eni_sqs_private" {
count = length(aws_vpc_endpoint.sqs.network_interface_ids)
resource_id =
data.aws_network_interface.transfer_eni[keys(data.aws_network_interface.transfer_eni)[count.index]].id
key = "Name"
value = join(" | ", [ UNMANAGED, upper
(data.aws_network_interface.transfer_eni[keys(data.aws_network_interface.transfer_eni
)[count.index]].availability_zone) ])}
Upvotes: 1