Open aashishmishra2 opened 1 year ago
Voting for Prioritization
Volunteering to Work on This Issue
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.
Hi @justinretzolk ,
Thank you so much for reply, Please refer below details,
data "aws_subnets" "current" { filter { name = "vpc-id" values = [var.VPC_ID] } }
resource "random_integer" "index" { min = 0 max = length(data.aws_subnets.current.ids) - 1
keepers = { vpc_id = var.VPC_ID } }
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
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
Thank you so much for the solution, Can you please help how can I get "new index number of subnet currently set"
Thanks Ashish
If you check the terraform plan it will say in there the index number of the subnets. Take a look at that to start
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
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.
Terraform Core Version
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 :
Now :
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 :
Now :
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 :
Now :
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 :
Now :
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