Posts

Showing posts from September, 2023

Variable

Terraform variables Terraform supports a few different variable formats. Depending on the usage, the variables are generally divided into inputs and outputs. The input variables are used to define values that configure your infrastructure. These values can be used again and again without having to remember their every occurrence in the event it needs to be updated. Output variables, in contrast, are used to get information about the infrastructure after deployment. These can be useful for passing on information such as IP addresses for connecting to the server.  Output value : -  Output values make information about our infrastructure available on the command line, and can expose information for other Terraform Configuration  resource   "aws_eip"   "lb"  {    domain     =   "vpc" } output   "public_ip"   { value   =   aws_eip . lb }               ...

Cross-Reference-Attributes

 Basic of Attributes Each resource has its associated set of attributes.  Attributes are the fields in a resource that hold the values that end up in the state Cross reference Resource Attribute Terraform allows us to reference the attribute of one resource to be used in a different resources.  In the below example aws public ip address associated with security group  resource "aws_eip" "lb" {   domain   = "vpc" } resource "aws_security_group" "allow_tls" {   name         = "allow_tls"   description = "Allow TLS inbound traffic"   ingress {     description       = "TLS from VPC"     from_port         = 443     to_port           = 443     protocol         = "tcp"     cidr_blocks       = [ " ${ aws_eip . lb . public_ip } /32" ]   }

State File and dependency lock file

Image
 Terraform State file  Terraform keeps the  remote state  of the infrastructure It stores it in a file called  terraform.tfstate There is also a backup of the  previous  state in  terraform.tfstate.backup When you execute  terraform apply , a new  terraform.tfstate  and backup is created This is how Terraform keeps track of the remote state If the remote state changes and you run  terraform apply  again, Terraform will make changes to meet the correct  remote state  again. E.g., you manually  terminate  an instance that is managed by Terraform, after you run  terraform apply , it will be  started  again. You can keep the  terraform.tfstate  in version control (e.g.,  git ). This will give you a history of your  terraform.tfstate  file (which is just a big JSON file) This allows you to collaborate with other team members (however, you can get conflicts when two or ...