Basic Flashcards
Installation of Terraform
- Download Terraform binaries
- Set path
- Run command: terraform -version
Is Terraform a functional language?
No, it’s an declarative language
What is “main.tf”? Where should we put this file?
This is main file from which Terraform starts it’s execution. It’s location should be “root” folder.
What is the “general” syntax of any Terraform object?
… {}
How to mention the provider in terraform?
It should be mentioned inside the “main.tf” file.
provider “aws” {
region = “ap-south-1”
}
What is the “general” syntax of “resource” object?
resource { }
How arguments are named inside any object?
It is named as “key-value” pair. The name should be all small and “snake-case”.
my_attrbute = “value”
How to create instance via Terraform
resource "aws_instance" "myInstance" { ami = "asaas" instance_type = "t2.micro" vpc_security_group_ids = [ "id1", "id2"] user_data = "heredoc" tags = { name = "test" } }
What are different terraform files we should ignore from being pushing to source code?
.terraform
.tfstate
.tfstate.backup
How to use “heredoc” inside the terraform?
user_data =
How to create a security group resource in terraform?
resource "aws_security_group" "instance" { name = "mySecurityGroup" ingress = { from_port = 8080 to_port = 8080 protocol = "tcp" cidr_block = [ "0.0.0.0/0"] } }
What is interpolation syntax used in Terraform?
inside double quote and starting with $
“${}
What is the general syntax of interpolation syntax in Terraform?
”${resourceType.resourceName.resourceAttribute}”
What is the general syntax of variable object?
variable {
configs..[]
}
What are the different config parameters which we can use inside the variable object?
- description
- type
- default
Are config arguments of variable object mandatory?
No, those are all optional.
How Terraform searches for the value of “variable object”?
It searches in following order:
- search is there any variable value passed in “apply” command with -var or -var-file option
- If not found, it searches for the environment variable declared in format “TF_VAR_”
- If not found, it fallback to the default value of the variable
- If default value is not provided, it prompts the user during apply command
What are different “types” supported by the “type” of the config argument of variable object?
string list map set boolean tuple bool object any
Do we need to add double quote around string type?
No, we don’t need to put double quote around “type”
How to access a variable in Terraform?
It is done by following interpolation syntax:
“${var.}
What is output variable object? Why we need that?
Output variable is the “way” of printing output of any resource on console. It helps us to get the data.
Write the syntax of output object?
output “public_ip” {
value = “${aws_resource.instance.public_ip}”
}
When we gets the output value of output variable?
Right after “apply” command.
Is it possible to get the output value without apply command?
yes, by using the output command:
terraform output