USING REMOTE STATE TO TRANSFER DATA FROM ONE MODULE TO ANOTHER IN TERRAFORM
Hello guys back with another article, in this article you will get understanding of transferring data from one module to another module using remote state backend.
What is remote state backend ?
Backends define where Terraform’s state snapshots are stored. A given Terraform configuration can either specify a backend, integrate with Terraform Cloud, or do neither and default to storing state locally.
The terraform backend service is best to manage the state files generated while creating the resources for cloud. This state files in the backend will be more secure compare with stored locally.
The state files contains all the data of the resources e.g. resources names, resources ids, resources indices etc. If you are deploying the resources by creating different modules and trying to take data reference resource ids, names etc to the other module for creating the resources then you will stuck. So, this article will helps you to transfer the data from one module to another using remote state.
Lets consider an example, If you have a module A that created vpc only and you have module B that creates subnet only. So while creating vpc you will able to create easily but when you will create subnets from module B you will need vpc-id so in that case you have to take the reference of module A state file to get the vpc-id to create subnets.
so let’s deep dive into this to understand better with the help of practical examples.
In the first module you will able to create vpc. We will use s3 backend to store state files. The key name of every backend will be different but the bucket will be same. You can use different buckets for different backends.
We will be using the aws provider as we will be deploying the resources over aws only.
While creating the vpc resource we will be creating the security groups also as we will be deploying some ec2 instances in the resource-3[module-3] .
The important part we have to take care of is the outputs. If you mention to give some outputs e.g. vpc-id, security-group-ids then only terraform will add the outputs in the state file. If you don’t then terraform will not add the outputs in the state file and hence we will not able to retrieve the data to create the resources using other modules.
You terraform.tfvars should like this while creating vpc and security group from above resources-1[module-1].
The respective variable you will find at the below of this code.
All the code is followed same for the other resources.
So, while creating the resource-2[module-2] you will have to add the backend as mentioned below. The important part of referring the data from other remote state is mentioned in the below code of data block.
Data sources allow Terraform to use information defined outside of Terraform, defined by another separate Terraform configuration, or modified by functions.
The state files of vpc is stored in now the data block.
Here, we are using the outputs after data.terraform_remote_state.vpc_subnets_ids because we are placing outputs in resource-1[module-1] while creating the vpc and security groups. So the output will be directly going in the state file in the folder dev/vpc/main/terraform.tfstate.
Now while creating subnets from resource-2[module-2] we will be using vpc id from the above data block.
Referring the data(vpc-id) from resource-1[module-1] to resource-2[module-2] for creating subnets.
Here if you see the line 3, we are taking the vpc_id from data.terraform_remote_state.vpc_subnets_ids.outputs.vpc_id.
The code structure is same for resource-3[module-3]. Now for creating ec2 instances we will have to use security group id as well as subnet ids.
If you remember that the state file of vpc is stored in dev/vpc/main/terraform.tfstate and the state file of subnet is stored in dev/subnet/main/terraform.tfstate.
So here we have to add two data blocks to store the state files of security group ids output and subnets ids output differently. So we can use that when its required. You will understand the backend file structure of resource-3[module-3].
Here if see the above code then vpc_subnet-ids data block is maintaining the state file of resource-2[module-2] and vpc_security_group_ids data block is maintaining the state file of resource-1[module-1] respectively.
Now for launching the ec2 instances we will be using the above two data block to refer the security groups as well as subnet ids.
If you seeing line number 4, the we are taking the subnet_id from the vpc_subnet_ids data block. Here mentioning the  index because the state file gives the vpc_ids, subnet_ids, secuity_group_isd in the list format.
subnet_id = data.terraform_remote_state.vpc_subnets_ids.outputs.subnet_idsvpc_security_group_ids = [ data.terraform_remote_state.vpc_security_group_ids.outputs.aws_security_group_name ]
If you liked this article please like and comment. I hope you have learnt something new today in IAAC world. Keep grinding ! keep learning !