What was done in this section:
mkdir ltthw_resources
echo resource "local_file" "hello_local_file" { > hello_local_file.tf
echo content = "Hello terraform local!" >> hello_local_file.tf
echo filename = "${path.module}/hello_local_file.txt" >> hello_local_file.tf
echo } >> hello_local_file.tf
terraform init
terraform fmt
terraform validate
terraform apply
terraform apply -auto-approve
terraform plan
terraform destory
- Create a data section and a file with no contents. --> Error
- Create a data section and a file with some contents. --> OK but no changes.
- Create a data section and output section and a file with some contents. --> OK and changes applied and shown.
data "local_file" "data_file" {
filename = "${path.module}/afile.txt"
}
output "data_file_content" {
value = data.local_file.data_file.content
}
This is life cycle of teraform commands:
Created a local file resource and destroy it.
resource "local_file" "hello_destroy_file" {
content = "Hello terraform destroy!"
filename = "${path.module}/hello_destroy.txt"
}
terraform init
terraform fmt
terraform validate
terraform apply
terraform show
terraform plan -destroy
terraform destroy
terraform show
Q. Print you name in terraform.
A.
myname_file.txt
My name is Sumit Das.
Dark Lord of DevOps.
output.tf
data "local_file" "myname_data" {
filename = "${path.module}/myname_file.txt"
}
output "myname_data-output" {
value = data.local_file.myname_data.content
}
Run the commands:
terraform init
terraform fmt
terraform validate
terraform apply -auto-approve
terraform show
terraform plan
Commands used:
terraform plan -destroy -target=aws_vpc.ltthw-vpc
terraform plan -destroy -target=local_file.hello_local_file
terraform graph
terraform graph > graph.dot
cat graph.dot
cat graph.dot | dot -Tpng > graph.png
Graph is crated like this:
Commands Used:
terraform show > resources.txt
rm terraform.tfstate terraform.state.backup
terraform import aws_subnet.ltthw-vpc-subnet subnet-01c0aa6aac2b22023
terraform state list
terraform show
terraform import aws_vpc.ltthw-vpc vpc-13a5a768
terraform state list
terraform show
terraform import local_file.hello_local_file hello_local.txt
Check the files in ltthw_3.3_variable
Takeaway Commands:
terraform apply -var 'myfilename=command_line_filename.txt' -auto-approve
How to access variable from another module :
output "consumer_content" {
value = module.hello.content
}
Takeaway Commands:
terraform apply -var-file="testing.tfvars" -auto-approve
References:
Data Types:
If we don’t specify a type in our code for the collection and input the value on the command line while mixing types, Terraform will convert the values to a single type if it can, based on rules specified in the language.
- Common Data Types :
a_number = 2
a_string = "SD"
a_boolean = false
- List : The simplest of these is a list which looks similar to the list construct in other languages.
a_list = [2, 1, 2]
- Sets : Unlike lists, sets do not care about ordering and dropping any duplicates within the set.
a_string_set = ["Hello", "Hello", "Goodbye"]
- Maps : If we need to associate one variable with another, then maps are our friends. They allow us to map a string to a variable of a specifiable type.
a_string_map = { "1" : "Hello", "2" : "Goodbye" }
a_number_map = { "example1" : 2.2 }
a_boolean_map = { "abc" : true }
- Objects : Complex types allow us to collect variables of different types into a single variable.
variable rectangle {
type = object({
length=number,
width=number,
description=string
})
}
- Tuples : A tuple is like a list, but it specifies the type and length of the list directly.
variable a_tuple {
type = tuple([number,string])
}
- Any : Instead of specifying a type, we can use the any keyword to instruct Terraform to take the type of the first value it sees for the variable.
variable a_tuple_with_any {
type = tuple([any,string])
}
The null resource is a resource that explicitly does not create anything. The provisioners set up resources upon creation or change. The triggers allow the user to exercise some control over when provisioners are run.
The count meta-argument looks like a simple variable, but it has a significant effect on the Terraform code. A meta-argument is a value that tells Terraform about the resources being created rather than the details of the resources themselves.
As you have probably guessed, setting count creates that number of resources. Remember that Terraform is declarative, but if we think in terms of iterative code, then this turns the code into something like a for loop. In this for loop, count.index can be used to refer to which iteration we’re currently on.
Also seen above is the format function, which allows us to format strings. If you want to learn more about it, look up the Terraform format function. In this case, the subnet for the cidr_block value for each of the three AWS VPCs created is provided.
For-each is an alternative way to count to make multiple instances of a resource. It could be viewed as an extension of the count, where each iteration has its own value beyond an integer.
The for_each can take either a set of strings or a map. If it’s a map, we can use the each.key reference to refer to the key of each item of the map in turn, or each.value to refer to each item’s value.
Extract S3 bucket Name: BUCKET_NAME="$(terraform show -no-color | grep -w bucket | awk '{print $NF}')"
When chaninging from local to rmeote backend for same resource, the following error comes up
Initializing the backend...
╷
│ Error: Backend configuration changed
│
│ A change in the backend configuration has been detected, which may require migrating existing state.
│
│ If you wish to attempt automatic migration of the state, use "terraform init -migrate-state".
│ If you wish to store the current configuration with no changes to the state, use "terraform init -reconfigure".