- Create multiple cloud / online apps, link and configure them
- Infrastructure as Code (IaC)
- Sandbox: https://developer.hashicorp.com/terraform/sandbox
- Terraform loads all
.tffiles in the same folder together
/* multi-line comment */
# single-line commentmy_block {
key1 = "value" # string
key2 = 3 # integer
key3 = true # boolean
key4 = [1, 2, 3] # list
key5 = { k = "v" } # map
}- Everything is a block
- Main Blocks are provider, resource , variable , output , locals
- Provider :
- Download Libarary
- Copy from Terraform Doc
- Eg: provider "git_hub"{}
- Can be in main.tf / providers.tf
- Resource
- Config Library
- Copy from Terraform Doc
- should be in main.tf
- variable
- Input values from user
- variables.tf
ormain.tf` - Eg: variable "a"{default = 1}
- Access by "var.a"
- output
- print putput
- outputs.tf
ormain.tf` - Eg: output "my_output" {value = var.a}
- locals
- Interal Values
- locals.tf
ormain.tf` - eg locals{k="v"}
- Access by local.k
- Others
- module {} : Functions
- terraform {} : Terraform Backend Settings
- Provider :
- Input from user
- Access using
var.name - Interpolation:
${var.name}
variable "a" {
default = 1
}- Pass via CLI :
variable "a" {
} /* blank*/terraform plan -var="a=1"- Using tfvars file (
prod.tfvars):
a = 1terraform plan -var-file="prod.tfvars"- Using environment variable:
export TF_VAR_a=1- For internal usage
locals {
name = "Deepak"
message = "HI ${local.name}"
}- Display values after apply
output "my_output" {
value = resource_name.attribute
}resource "local_file" "pet" {
count = fileexists("./pets.txt") ? 0 : 1
filename = "./pets.txt"
content = "HI"
}-
Built-in functions:
- Numeric
- String
- List / Collection
- DateTime
- File
- Conversion
resource "aws_instance" "a1" {
key = "val"
}
resource "aws_instance" "a2" {
key = aws_instance.a1.key
}# $export TF_VAR_test_var="123"
#main.tf
output { value =var.test_var}
variable test_var{}
# terraform init
# terraform validate
# terraform plan
# terraform apply
# terraform destroy
# Create file main.tf and add below
resource "local_file" "pet" {
filename = "./pets.txt"
content = "HI"
}Commands:
terraform init
terraform validate
terraform plan
terraform apply
terraform destroy -target=local_file.pet# create main.tf
/*1.Import and Setup : Copy from Terraform Website */
provider "github" {
token = var.terraform_git_token
}
/*2.Env variable */
variable "terraform_git_token" {}
#$export TF_VAR_terraform_git_token="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"
/*3.Config : Copy from Terraform Website */
resource "github_repository" "my_git_resrc" {
name = var.git_repo_name
description = local.git_description
visibility = "public"
}
/*4.Input from CLI */
variable "git_repo_name"{
}
/*5. local*/
locals{
git_description ="terraform_git_description"
}
# terraform init
# terraform validate
# terraform plan
# terraform apply
# terraform destroy