1

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

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
}
4
  • It looks like var.availability_zones is ordered incorrectly. Can you please add the piece of code that defines var.availability_zones? Commented Dec 25, 2020 at 18:07
  • Hello @DennisTraub the var.availability_zones is a simple data statement, but in any case I've added the variable Commented Dec 25, 2020 at 18:23
  • Are you trying to tag Interface Endpoint type (aws_vpc_endpoint.sqs) for sqs? Commented Dec 26, 2020 at 13:06
  • Hi, @AsriBadlah! Yes, is has been tagged, but not how I expected Commented Dec 26, 2020 at 13:41

1 Answer 1

1

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) ])}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, @Asri Badlah! Just one question I missed the part, where you mapping an var.availability_zone, could you please mark the variable if it possible, and thank you again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.