🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Terraform Tutorials: Set data type and example


set(…): a collection of unique values that do not have any secondary identifiers or ordering.

Set collections are unordered and cannot contain duplicate values, so the ordering of the argument elements is lost and any duplicate values are coalesced:

Since Terraform’s concept of a set requires all of the elements to be of the same type, mixed-typed elements will be converted to the most general type:

Example Code


# Slice a set by first casting it to a list and then accessing it as a list.

variable "set" {
  type = set(string)
  default = [
    "foo",
    "bar",
  ]
}

output "set" {
  value = var.set
}

output "set_first_element" {
  value = var.set[0]
}

How to convert list to set type?



Function name – toset

Similar kinds of complex types (list/tuple/set and map/object) can usually be used interchangeably within the Terraform language, and most of Terraform’s documentation glosses over the differences between the kinds of complex type.

Pass a list value to toset to convert it to a set, which will remove any duplicate elements and discard the ordering of the elements.

Explicit type conversions are rarely necessary in Terraform because it will convert types automatically where required. Use the explicit type conversion functions only to normalize types returned in module outputs.



Example

> toset(["a", "b", "c"])
[
  "a",
  "b",
  "c",
]

> toset(["a", "b", 3])
[
  "3",
  "a",
  "b",
]

> toset(["c", "b", "b"])
[
  "b",
  "c",
]

Sets are almost similar to both tuples and lists:


  • When a list or tuple is converted to a set, duplicate values are discarded and the ordering of elements is lost.
  • When a set is converted to a list or tuple, the elements will be in an arbitrary order. If the set’s elements were strings, they will be in lexicographical order; sets of other element types do not guarantee any particular order of elements.

Reference

  • https://developer.hashicorp.com/terraform/language/expressions/type-constraints
  • https://developer.hashicorp.com/terraform/language/functions/toset
Subscribe
Notify of
guest


1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
FooBar
FooBar
1 year ago

Elements of a set are identified only by their value and don’t have any separate index or key to select with, so it’s only possible to perform operations across all elements of the set.

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

1
0
Would love your thoughts, please comment.x
()
x