Terraform meta-arguments are special syntax constructs which help in specifying how we want resources to be managed.
There are 5 meta-arguments in Terraform:
- depends_on
- lifecycle
- count
- for_each
- provider
I've already covered depends_on and lifecycle in my previous articles Terraform Resource Dependencies | My Public Notepad and Terraform State | My Public Notepad.
count
It specifies the number of infrastructure instances to be created.
resource "local_file" "foo" {
filename = "${path.cwd}/temp/foo.txt"
content = "This is a text content of the foo file!"
count = 3
}
Let's create multiple instances of local_file:
$ terraform apply
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
+ create
Terraform will perform the following actions:
# local_file.foo[0] will be created
+ resource "local_file" "foo" {
+ content = "This is a text content of the foo file!"
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/home/bojan/dev/github/demo-terraform/local_resources_demo/temp/foo.txt"
+ id = (known after apply)
}
# local_file.foo[1] will be created
+ resource "local_file" "foo" {
+ content = "This is a text content of the foo file!"
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/home/bojan/dev/github/demo-terraform/local_resources_demo/temp/foo.txt"
+ id = (known after apply)
}
# local_file.foo[2] will be created
+ resource "local_file" "foo" {
+ content = "This is a text content of the foo file!"
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "/home/bojan/dev/github/demo-terraform/local_resources_demo/temp/foo.txt"
+ id = (known after apply)
}
Plan: 3 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
local_file.foo[0]: Creating...
local_file.foo[2]: Creating...
local_file.foo[1]: Creating...
local_file.foo[1]: Creation complete after 0s [id=db5ca40b5588d44e9ec6c1b4005e11a6fd0c910e]
local_file.foo[2]: Creation complete after 0s [id=db5ca40b5588d44e9ec6c1b4005e11a6fd0c910e]
local_file.foo[0]: Creation complete after 0s [id=db5ca40b5588d44e9ec6c1b4005e11a6fd0c910e]
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Only one file is actually created (3 times) though as filename is the same:
$ ls ./temp/
foo.txt
To prevent this, we can interpolate count.index:
filename = "${path.cwd}/temp/foo${count.index}.txt"
foo0.txt foo1.txt foo2.txt
variable "filename" {
default = [
"foo.txt",
"foo2.txt",
"foo3.txt",
]
}
for_each
# Use this in order to fix Error: Invalid for_each argument
# type = set(string)
type = list(string)
default = [
"foo.txt",
"foo2.txt",
"foo3.txt",
]
}
# for_each = var.filename
# toset() converts list into set. Use it if var.filename is a list.
for_each = toset(var.filename)
filename = "${path.cwd}/temp/${each.value}"
content = "This is a text content of the foo file!"
}
local_file.foo["foo3.txt"]: Creating...
local_file.foo["foo.txt"]: Creating...
# local_file.foo["foo.txt"]:
resource "local_file" "foo" {
content = "This is a text content of the foo file!"
directory_permission = "0777"
file_permission = "0777"
filename = "/home/bojan/dev/github/demo-terraform/meta_args_demo/for_each/temp/foo.txt"
id = "db5ca40b5588d44e9ec6c1b4005e11a6fd0c910e"
}
# local_file.foo["foo2.txt"]:
resource "local_file" "foo" {
content = "This is a text content of the foo file!"
directory_permission = "0777"
file_permission = "0777"
filename = "/home/bojan/dev/github/demo-terraform/meta_args_demo/for_each/temp/foo2.txt"
id = "db5ca40b5588d44e9ec6c1b4005e11a6fd0c910e"
}
# local_file.foo["foo3.txt"]:
resource "local_file" "foo" {
content = "This is a text content of the foo file!"
directory_permission = "0777"
file_permission = "0777"
filename = "/home/bojan/dev/github/demo-terraform/meta_args_demo/for_each/temp/foo3.txt"
id = "db5ca40b5588d44e9ec6c1b4005e11a6fd0c910e"
}
Outputs:
foo_files = (sensitive value)
{
"foo.txt" = {
"content" = "This is a text content of the foo file!"
"content_base64" = tostring(null)
"directory_permission" = "0777"
"file_permission" = "0777"
"filename" = "/home/bojan/dev/github/demo-terraform/meta_args_demo/for_each/temp/foo.txt"
"id" = "db5ca40b5588d44e9ec6c1b4005e11a6fd0c910e"
"sensitive_content" = tostring(null)
"source" = tostring(null)
}
"foo2.txt" = {
"content" = "This is a text content of the foo file!"
"content_base64" = tostring(null)
"directory_permission" = "0777"
"file_permission" = "0777"
"filename" = "/home/bojan/dev/github/demo-terraform/meta_args_demo/for_each/temp/foo2.txt"
"id" = "db5ca40b5588d44e9ec6c1b4005e11a6fd0c910e"
"sensitive_content" = tostring(null)
"source" = tostring(null)
}
"foo3.txt" = {
"content" = "This is a text content of the foo file!"
"content_base64" = tostring(null)
"directory_permission" = "0777"
"file_permission" = "0777"
"filename" = "/home/bojan/dev/github/demo-terraform/meta_args_demo/for_each/temp/foo3.txt"
"id" = "db5ca40b5588d44e9ec6c1b4005e11a6fd0c910e"
"sensitive_content" = tostring(null)
"source" = tostring(null)
}
}
No comments:
Post a Comment