DevOps Classroom Series – 05/Mar/2021

Terraform Datasources

  • Data sources allow data to be fetched or computed in terraform configuration
  • Navigate to the aws provider documentation and search for aws_subnet to find the datasources refer here Preview
  • Since we can write the query to fetch subnet ids, we can use for each expression of terraform
  • The following expression which creates subnet associations can be changed to
resource "aws_route_table_association" "webassociations" {
  count = 2
  route_table_id = aws_route_table.publicrt.id
  subnet_id =  aws_subnet.subnets[count.index].id

  depends_on = [ 
    aws_route_table.publicrt
   ]
  
}
  • For New approach Refer Here Preview

  • Now lets create an association for private subnets Refer Here

  • Now destroy all and recreate Preview

  • This error has occured as terraform is trying to query the subnet ids before they are created Preview

  • So we need to create subnets first and then apply the whole template (This is not a good practice)

terraform apply -target="aws_subnet.subnets" .
terraform apply .
  • depending on dynamic values by writing a data source will work in incremental mode of deployment, whenever you are trying to create the complete infra data sources if not used correctly might lead to these situations.
  • So for this reason lets revert back to what we had for creating route table associations
  • Refer Here for the changes reverted and the length function used.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin