-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathvariables-more.txt
128 lines (90 loc) · 2.17 KB
/
variables-more.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
====== Variables and code in same file
#1 - filename - site.tf
provider "aws" {
region = "us-east-1"
}
variable "ami" {
default = "ami-04169656fea786776"
}
variable "instance_type" {
default = "t2.nano"
}
resource "aws_key_pair" "terraform-demo" {
key_name = "terraform-demo"
public_key = "${file("terraform-demo.pub")}"
}
resource "aws_instance" "my-instance" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${aws_key_pair.terraform-demo.key_name}"
user_data = "${file("install_apache.sh")}"
tags = {
Name = "Terraform"
Batch = "5AM"
}
}
=====
==== As project grows, we can put variables and code in separate files
#1 - file for variables - vars.tf
variable "ami" {
default = "ami-04169656fea786776"
}
variable "instance_type" {
default = "t2.nano"
}
#2 - file for code - site.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_key_pair" "terraform-demo" {
key_name = "terraform-demo"
public_key = "${file("terraform-demo.pub")}"
}
resource "aws_instance" "my-instance" {
ami = "${var.ami}"
instance_type = "${var.instance_type}"
key_name = "${aws_key_pair.terraform-demo.key_name}"
user_data = "${file("install_apache.sh")}"
tags = {
Name = "Terraform"
Batch = "5AM"
}
}
#####
#### MAPS - Maps are a way to create variables that are lookup tables.
#1 - file for variables - vars.tf
variable "ami" {
type = "map"
default = {
"us-east-1" = "ami-04169656fea786776"
"us-west-1" = "ami-006fce2a9625b177f"
}
}
variable "instance_type" {
default = "t2.nano"
}
variable "aws_region" {
default = "us-east-1"
}
####
#2 - file for code - site.tf
provider "aws" {
region = "${var.aws_region}"
}
resource "aws_key_pair" "terraform-demo" {
key_name = "terraform-demo"
public_key = "${file("terraform-demo.pub")}"
}
resource "aws_instance" "my-instance" {
ami = "${lookup(var.ami,var.aws_region)}"
instance_type = "${var.instance_type}"
key_name = "${aws_key_pair.terraform-demo.key_name}"
user_data = "${file("install_apache.sh")}"
tags = {
Name = "Terraform"
Batch = "5AM"
}
}
### WE can check the value using "terraform console"
# terraform console
> "${lookup(var.ami,var.aws_region)}"