hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.77k stars 9.12k forks source link

[Bug]: Data Source: aws_subnets filter #32497

Open aashishmishra2 opened 1 year ago

aashishmishra2 commented 1 year ago

Terraform Core Version

= 0.12

AWS Provider Version

5.3.0

Affected Resource(s)

After changing "aws_subnet_ids" with Data Source: "aws_subnets", The position of subnets in array got changed due to which the new server creation is getting redeployed due to new subnet in that position.

Previously :

data "aws_subnet_ids" "current" {
  vpc_id = var.VPC_ID
}

Output :  subnet_ids     = [
      "subnet-0a45b4c567a328c84",
       "subnet-0aa24c3a96c8d1a5c"]

Now :

data "aws_subnets" "current" {
  filter {
    name   = "vpc-id"
    values = [var.VPC_ID]
  }
}

 + subnet_ids     = [
      + "subnet-0aa24c3a96c8d1a5c",
      + "subnet-0a45b4c567a328c84]

Can you please help me to fix it or let me know if I am missing something in filter or anywhere else.

Thanks Ashish

Expected Behavior

After changing "aws_subnet_ids" with Data Source: "aws_subnets", The position of subnets in array got changed due to which the new server creation is getting redeployed due to new subnet in that position.

Previously :

data "aws_subnet_ids" "current" {
  vpc_id = var.VPC_ID
}

Output :  subnet_ids     = [
      "subnet-0a45b4c567a328c84",
       "subnet-0aa24c3a96c8d1a5c"]

Now :

data "aws_subnets" "current" {
  filter {
    name   = "vpc-id"
    values = [var.VPC_ID]
  }
}

 + subnet_ids     = [
      + "subnet-0aa24c3a96c8d1a5c",
      + "subnet-0a45b4c567a328c84]

Can you please help me to fix it or let me know if I am missing something in filter or anywhere else.

Thanks Ashish

Actual Behavior

After changing "aws_subnet_ids" with Data Source: "aws_subnets", The position of subnets in array got changed due to which the new server creation is getting redeployed due to new subnet in that position.

Previously :

data "aws_subnet_ids" "current" {
  vpc_id = var.VPC_ID
}

Output :  subnet_ids     = [
      "subnet-0a45b4c567a328c84",
       "subnet-0aa24c3a96c8d1a5c"]

Now :

data "aws_subnets" "current" {
  filter {
    name   = "vpc-id"
    values = [var.VPC_ID]
  }
}

 + subnet_ids     = [
      + "subnet-0aa24c3a96c8d1a5c",
      + "subnet-0a45b4c567a328c84]

Can you please help me to fix it or let me know if I am missing something in filter or anywhere else.

Thanks Ashish

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

After changing "aws_subnet_ids" with Data Source: "aws_subnets", The position of subnets in array got changed due to which the new server creation is getting redeployed due to new subnet in that position.

Previously :

data "aws_subnet_ids" "current" {
  vpc_id = var.VPC_ID
}

Output :  subnet_ids     = [
      "subnet-0a45b4c567a328c84",
       "subnet-0aa24c3a96c8d1a5c"]

Now :

data "aws_subnets" "current" {
  filter {
    name   = "vpc-id"
    values = [var.VPC_ID]
  }
}

 + subnet_ids     = [
      + "subnet-0aa24c3a96c8d1a5c",
      + "subnet-0a45b4c567a328c84]

Can you please help me to fix it or let me know if I am missing something in filter or anywhere else.

Thanks Ashish

Steps to Reproduce

NA

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

NA

Would you like to implement a fix?

No

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

justinretzolk commented 1 year ago

Hey @aashishmishra2 👋 Thank you for taking the time to raise this! If you can supply a sample Terraform configuration that shows the additional resources that are dependent upon this data source, it may help us to give better guidance. Without that information, my initial thought would be that you might be using the output of the data source with count to create the dependent resource. If that's the case, it's recommended to instead use the toset() function with the for_each meta argument to ensure that changes in ordering don't cause resource recreation.

aashishmishra2 commented 1 year ago

Hi @justinretzolk ,

Thank you so much for reply, Please refer below details,

data "aws_subnets" "current" { filter { name = "vpc-id" values = [var.VPC_ID] } }

Random function to select one of the subnet from the list

resource "random_integer" "index" { min = 0 max = length(data.aws_subnets.current.ids) - 1

keepers = { vpc_id = var.VPC_ID } }

This is the EC2 module code in which Subnet will be selected.

EC2 Instance Module get subnet from below code :

Subnet_id = tolist(data.aws_subnets.current.ids)[random_integer.index.result]

Please let me know if more details are required.

Thanks Ashish

RichYoungHaven commented 1 year ago

By doing a random terraform Subnet_id = tolist(data.aws_subnets.current.ids)[random_integer.index.result] on recreation there is no guarantee what subnet you will get, if you change that to terraform Subnet_id = tolist(data.aws_subnets.current.ids)[<new index number of subnet currently set>] you wont have the issue of the server being re-created

aashishmishra2 commented 1 year ago

Thank you so much for the solution, Can you please help how can I get "new index number of subnet currently set"

Thanks Ashish

RichYoungHaven commented 1 year ago

If you check the terraform plan it will say in there the index number of the subnets. Take a look at that to start

aashishmishra2 commented 1 year ago

Hi @RichYoungHaven ,

Thank you so much for the info, Request you to please go though below details

Subnet terraform plan : Created 3 months ago

module.emea.module.vpc_ft_vc01.aws_subnet.vpc_vc01[0]: Refreshing state... [id=subnet-0a1479861e1fb155e] module.emea.module.vpc_ft_vc01.aws_subnet.vpc_vc01[1]: Refreshing state... [id=subnet-02d0499b974a62d65]

Server creation terraform plan : Created 2 months ago While updating the EC2 instance:

subnet_id = "subnet-02d0499b974a62d65" -> "subnet-0a1479861e1fb155e"

Server creation terraform plan : Created 2 months ago random result = 0 (Existing one , coming from State file which was never touched since the instance launch)

After changing the data sources for subnet_id which happened nearly a month back, this issue is there.

Issue: after any update even the tag update the server is getting recreated due to subnet change.

Thanks Ashish

tonyszhang commented 1 year ago

Can confirm this unexpected behaviour.

data "aws_subnets" "retrieve_subnet_id" {
  filter {
    name   = "vpc-id"
    values = [data.aws_vpc.retrieve_vpc_id.id]
  }

  dynamic "filter" {
    for_each = local.subnet_filter
    content {
      name   = filter.value.name
      values = filter.value.values
    }
  }
}

data "aws_subnet" "selected_subnet_1" {
  state  = "available"
  id     = element(data.aws_subnets.retrieve_subnet_id.ids, 0)
  vpc_id = data.aws_vpc.retrieve_vpc_id.id
}

data "aws_subnet" "selected_subnet_2" {
  state  = "available"
  id     = element(data.aws_subnets.retrieve_subnet_id.ids, 1)
  vpc_id = data.aws_vpc.retrieve_vpc_id.id
}

Now has different subnet-id's compared to aws_subnet_ids were used.