learn-terraform

Course outlines for learning terraform.

View the Project on GitHub robertdebock/learn-terraform

Functions

expected time requirements
30 minutes A computer with Terraform installed, terraform knowledge.

Goal: Understand what functions are and how to apply (some of) them.

Explanation

Terraform has many built-in function. Some noteworthy:

Howto

You can use functions in any/many places, let’s take a look at an example:

Variables

variable "name" {
  type        = string
  description = "The name of the project. It's used to generate unique values for all kinds of resources."
  default     = "my"
  validation {
    condition     = length(var.name) > 0 && length(var.name) <= 32
    error_message = "Please use a name with at least 1 character, and at most 32 characters."
  }
}

Assignment

variable "example" {
  type        = string
  description = "Some variable"
}

The validation should:

Questions:

  1. Where do you see functions being used? (variables, main, output, providers, versions, etc)
  2. Can you share good use of a function from code you already have?

Solution

variable "example" {
  type        = string
  description = "Some variable"
  default     = "hello there"
  validation {
    condition     = length(var.example) >= 4 && length(var.example) <= 16 && can(regex(".*hello.*", var.example)) && lower(var.example) == var.example
    error_message = "The example variable must be 4 up to 16 characters, contain 'hello' and be lowercase."
  }
}