-
Notifications
You must be signed in to change notification settings - Fork 130
/
variables-different-ways.txt
151 lines (116 loc) · 3.2 KB
/
variables-different-ways.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#CASE #1 - We can use variables using "locals"
# vim main.tf
# -----
locals {
ami = "ami-0d26eb3972b7f8c96"
type = "t2.micro"
tags = {
Name = "My Virtual Machine"
Env = "Dev"
}
subnet = "subnet-76a8163a"
nic = aws_network_interface.my_nic.id
}
resource "aws_instance" "myvm" {
ami = local.ami
instance_type = local.type
tags = local.tags
network_interface {
network_interface_id = aws_network_interface.my_nic.id
device_index = 0
}
}
resource "aws_network_interface" "my_nic" {
description = "My NIC"
subnet_id = local.subnet
tags = {
Name = "My NIC"
}
}
### file END here
### try to keep the number of local variables to their minimum. Using many local variables can make the code hard to read
#CASE - 2 - Using external variables.tf file
# vim variables.tf
####
variable "ami" {
type = string
description = "AMI ID for the EC2 instance"
default = "ami-0a23ccb2cdd9286bb"
validation {
condition = length(var.ami) > 4 && substr(var.ami, 0, 4) == "ami-"
error_message = "Please provide a valid value for variable AMI."
}
}
variable "type" {
type = string
description = "Instance type for the EC2 instance"
default = "t2.micro"
sensitive = true
}
variable "tags" {
type = object({
name = string
env = string
})
description = "Tags for the EC2 instance"
default = {
name = "test-machine"
env = "prod"
}
}
variable "subnet" {
type = string
description = "Subnet ID for network interface"
default = "subnet-76da2b1f"
}
### file END here
## NOW create another file - main.tf with the code
# vim main.tf
####
resource "aws_instance" "myvm" {
ami = var.ami
instance_type = var.type
tags = var.tags
network_interface {
network_interface_id = aws_network_interface.my_nic.id
device_index = 0
}
}
resource "aws_network_interface" "my_nic" {
description = "My NIC"
subnet_id = var.subnet
tags = {
Name = "My NIC"
}
}
### file END here
## CASE - 3 - Substituting variable values at run time using -var
#
terraform plan -var "ami=test" -var "type=t2.nano"
# These variable will override the values in the variables.tf file
## CASE - 4 - If you have a lot of variables to replace at runtime, then passing as -var can be tedious
# There we use .tfvars file
# vim newvalues.tfvar
###
ami = "ami-0d26eb3972b7f8c96"
type = "t2.nano"
tags = {
"name" : "My Virtual Machine"
"env" : "Dev"
}
### file END here
#
#Now run the terraform -
terraform plan -var-file values.tfvars
#CASE - 4 - Using environment variables
# input variables values can also be set using Terraform environment variables.
# To do so, simply set the environment variable in the format TF_VAR_<variable name>
export TF_VAR_ami=ami-0d26eb3972b7f8c96
## REMEMBER
# Terraform uses hierarchical logic to decide which value to consider in the event of a conflict.
#The order is as follows:
# 1. Environment variables
# 2.The terraform.tfvars file, if present
# 3. The terraform.tfvars.json file, if present
# 4. Any *.auto.tfvars or *.auto.tfvars.json files, processed in lexical order of their filenames
# 5. Any -var and -var-file options on the command line, in the order they are provided