1) Install terraform.
Đầu tiên chúng ta cần download terraform.
https://www.terraform.io/downloads
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-143.png)
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-144.png)
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-145.png)
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-146.png)
Bước tiếp theo là bạn đi vào thư mục giải nén và gõ ./terraform
là sẽ ok rồi nhé!
Hoặc nếu bạn cần thận có thể add thêm Environment Windows để gõ ở chỗ nào cũng được
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-149.png)
Bạn phải khởi động lại mấy để có thể ăn config ở trên nhé
2) Practise the basic example.
2.1) Terraform init, plan, apply
Giờ làm 1 bài lab đơn giản với terraform là tạo 1 file
resource local_file sample_res {
content = "Nimtechnology Love Terraform"
filename = "sample.txt"
}
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-157.png)
Giờ bạn làm như ảnh. KO cần giải thích nhiều
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-150.png)
Bước đầu tiên khi bạn muốn chạy 1 file .tf
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-151.png)
show các step chạy ntn
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-152.png)
cái này là chạy thật oi đó
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-153.png)
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-154.png)
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-156.png)
GIờ tạo file như ý
2.2) Terraform – local_file arguments
Giờ làm sao biết 1 resource là local_file
thì nó có những arguments nào?
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-160.png)
![](https://nimtechnology.com/wp-content/uploads/2022/03/Screen-Shot-2022-03-28-at-15.44.43.png)
Giờ chúng ta thử argument là sensitive_content
resource local_file sample_res {
filename = "sample.txt"
sensitive_content = "Sensitive - Nimtechnology Love Terraform"
}
sensitive_content: thì nó sẽ không print value ra màn hình console:
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-158.png)
2.3) Terraform – Multiple Resources
Ngoài ra bạn có thể khai báo multiple Resource
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-159.png)
từ 1 file main.tf ta chạy 2 resource và nó tạo ra 2 file.
2.4) Random Provider
Bước đâu tiên là google
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-162.png)
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-163.png)
Random Provider
The “random” provider allows the use of randomness within Terraform configurations.
This is a logical provider, which means that it works entirely within Terraform’s logic, and doesn’t
interact with any other services.
2.4.1) random_integer
resource random_integer rint {
min = 1
max = 50000
}
Tiếp đến là bạn CD vào folder chưa file main.tf
terraform init
terraform plan
terraform apply
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-164.png)
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-165.png)
Nếu vậy bạn cần change min or max thì apply ở lần tiếp theo sẽ có sử thay đổi.
Giờ mình sử dụng thêm output
resource random_integer rint {
min = 1
max = 100
}
output name {
value = random_integer.rint.id
}
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-166.png)
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-167.png)
2.4.2) random_string
Bạn sẽ đọc thêm ở link này:
https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string
resource random_string rstring {
length = 15
}
output name {
value = random_string.rstring.result
}
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-168.png)
2.5) Variables
2.5.1) Introduction to Variables
Đầu tiên chúng ta có 1 file variables variables.tf
variable filename1 {
type = string
default = "sample1.txt"
}
variable content1 {
type = string
default = "Nimtechnology is loving Terraform"
}
Tiếp đến là file main.tf
resource local_file sample_res {
filename = var.filename1
content = var.content1
}
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-169.png)
terraform init
terraform plan
terraform apply
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-170.png)
Ok vậy là chúng ta có thể tạo ra 1 file có tên là variables.tf
Xong rồi chúng ta add các variable mà chúng ta muốn
xong bên file main.tf thì chúng ta gọi qua.
Trên này là simle và understand easily
chúng ta tham khảo link này thêm
https://www.terraform.io/language/values/variables
2.5.2) Types of variables
string — “cat”
number — 234, 6.5
bool – true/false
list — sequence of value
list(string) =>[“red”, “green”, ‘blue”]
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-444.png)
Tuple – Group non homogeneous data type
tuple([string, number, bool]) => [“dog”, 23, true
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-452.png)
map — like key value : Dictionary
{name = “Ankit”, age = 32}
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-445.png)
set — only unique values
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-450.png)
Bên phải sẽ lỗi vì set thì ko cho phép Duplicate.
object — complex data tye
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-451.png)
2.5.2.1) Variable block
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-447.png)
default: data của variable đó>
type là liểu dữ liệu
description: là mô tả của bạn về variable đó
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-448.png)
bạn value type của nó là string.
Nếu bạn định nghĩ type là number sẽ bị lỗi
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-449.png)
2.5.3) Using Variables
Bạn có thể thao khảo chi tiết ở đây:
https://www.terraform.io/language/values/variables
2.5.3.1) Interactive Mode
Như mình đã chỉ ở phần trên
Introduction to Variables
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-453.png)
sau đó để sử dụng các argument trong file thì bạn gọi var.xxx
2.5.3.2) Command Line Flag
Bạn sẽ khai báo các variables ngày trong command line để sử dụng:
terraform apply -var="image_id=ami-abc123" terraform apply -var='image_id_list=["ami-abc123","ami-def456"]' -var="instance_type=t2.micro" terraform apply -var='image_id_map={"us-east-1":"ami-abc123","us-east-2":"ami-def456"}'
2.5.3.3) Environment Variable.
export TF_VAR_image_id=ami-abc123 terraform plan ...
Nếu bạn muốn sử dụng trong file main.tf là
resource local_file sample_res { filename = var.filename1 }
thì Environment phải là: export TF_VAR_filename1=ami-abc123
2.5.3.4) Variable Definition Files
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-454.png)
Vậy nó sẽ có thử tự load và ghi đè
2.5.3.5) Which Variable will load first
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-455.png)
- export TF_VAR_filename=sample.txt
- terraform.tfvars file
- variable.auto.tfvars file
- terraform apply -var “filename=sample.txt”
2.6) Multiple Providers
Bạn có thể sử dụng nhiều provider trong 1 file main.tf
resource local_file name {
content = "This is HCL"
filename = "sample.txt"
}
resource random_string name {
length = 10
}
Các bạn tự run nhé!
mình lười quá
2.7) Dependency
2.7.1) Implicit Dependency (calling is Resource Attributes)
resource local_file name1 {
filename = "explicit.txt"
content = "This is random String from RP : ${random_string.name2.id}"
}
resource random_string name2 {
length = 10
}
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-190.png)
sau khi chạy
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-191.png)
Cuối cùng là ghi vào file
2.7.2) Explicit Dependency
Ở phần này để rõ ràng Dependency thì chúng ta sài depends_on
resource local_file name1 {
filename = "explicit.txt"
content = "This is random String from RP : ${random_string.name2.id}"
depends_on = [random_string.name2]
}
resource random_string name2 {
length = 10
}
Cái này bạn có thể hiểu đơn giản là resource name1 sẽ cần giá trị từ resource name2
Vậy resource name2(first run) -> resource name1(second run)
2.8) Output Block
data local_file foo {
filename = "sample1.txt"
}
output name1 {
value = data.local_file.foo.content
}
Giờ chúng ta đọc content của file sample1.txt
rồi output ra screen
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-192.png)
Output ngoài việc để chúng ta nhìn thì chúng ta con để đưa các variable trang ansible hoặc shell
![](https://nimtechnology.com/wp-content/uploads/2022/04/image-460.png)
2.9) Lifecycle Rules
![](https://nimtechnology.com/wp-content/uploads/2022/03/image-193.png)
resource random_integer name {
min = 20
max = 350
lifecycle{
#create_before_destroy = true
#prevent_destroy = true
ignore_changes = [min, max]
}
}
Bạn có thể chạy thử từng case nhé
2.10) Provider Version
Mặc định các mục ở trên khi màn bạn chạy init thì terraform sẽ lấy các latest packages.
Giờ bạn múa chỉ định version cho package thì như thế nào
terraform {
required_providers {
random = {
source = "hashicorp/random"
version = "2.3.1"
}
}
}
provider "random" {
# Configuration options
}
2.11) Data Source – Read File
data local_file foo {
filename = "sample1.txt"
}
output name1 {
value = data.local_file.foo.content
}
Cái này thì Y chang múc output