Data-Sources
-
For creating ec2 machine, we need to provide
- subnet-id
- security group
- key value pair
-
One approach create every thing and use the attributes.
-
If we want to use existing subnet-id, security group and key-value pair, we need to know ids, for this terraform has data sources which can query the information from providers.
-
Every provider gives various data sources much like resources.
-
Lets create a simple data source to pull the information of default subnet and create a new subnet
data "aws_vpc" "default" {
default = true
}
resource "aws_subnet" "extra" {
cidr_block = "172.31.48.0/20"
vpc_id = "${data.aws_vpc.default.id}"
}
Terraform provisioning
- Execution of scripts/ansible/chef after creation of Virtual Machines is supported by terraform provisioners. Refer
Backends
- Two tf developers have same terraform script and they have applied terraform, it creates two different resources, as the state file is stored on individual developers laptop
- Now, if we want to restrict these two developers in such a way, whenever they execute terraform it should not create two different but one resource.
- Terraform supports backends, to store state remotely and terraform also supports locking feature to avoid simultaneous access to terraform state.
- Refer