Giter Site home page Giter Site logo

variables's Introduction

Variables

Variables is a universal concept in all scripting and programming. It follows DRY principle, don't repeat yourself. Instead of hardcoding the values we can create variables and use them wherever required. If you change the value at one place then it can reflect everywhere it is used.

Terraform have following type of variables.

  • Number
  • String
  • List
  • Map
  • Bool

Nested Map:

variable "servers" {
  type = map(map(string))
  default = {
    "web" = {
      "server1" = "192.168.1.10"
      "server2" = "192.168.1.11"
    }
    "database" = {
      "server1" = "10.0.0.5"
      "server2" = "10.0.0.6"
    }
  }
}

Syntax is:

variable "name-of-variable"{
    type = string/number/list/map/bool
    default = "default-value"
    description = "a short description about this"
}

How to use variable

ami_id = var.ami_id

Count and Count Index

When there is a requirement of creating a same kind of resource multiple times you can use count and count index paramaters instead of creating the same resource multiple times.

count index starts from 0.

Conditions

When there is a need of taking a decission based on some value we need to use conditions. Usual code of condition is

if(expression){
    this code run when expression is true
}
else{
    this code run when expression is false
}

in terraform the condition syntax is reduced to single line.

condition ? "this will run, if condition is true" : "this will run, if condition is false"

Functions

Functions will do a unit of work, when you call a function it will take 0 or few inputs and give us output, we can call functions whenever we need them. usual syntax is

function-name(inputs){
    run some thing
    return value
}

Terraform has built-in functions. It has many numeric, string, file, IP, etc based functions.

Loops

Loops are required when you want to create a resource multiple times based on a limit. We have 2 types of loops in terraform. Usually we iterate over a list or map and create the resources.

  1. Count based loops based on count parameter
  2. For each loop

variables's People

Contributors

joindevopscloud avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.