O'Reilly logo

Getting Started with Terraform - Second Edition by Kirill Shirinkin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using list variables

Continuing analogy with programming, list in Terraform is similar to arrays in most programming languages. There is a very nice place where we can use lists in our templates: security group.

Currently, an application module defines a single security group and assigns it to the instance. But an EC2 instance can have multiple security groups attached. We could have a default security group that allows an SSH access and then on an application level we have another one for app-specific permissions.

Let's add yet another variable to module/application/variables.tf, with an empty list as a default value:

variable "extra_sgs" { default = [] } 

Now, let's define a default security group in template.tf with SSH access allowed: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required