
In this example, the security_groups
variable is defined with a type of list(string)
, and it is given a default value of ["default"]
. The AWS provider is then configured to use the us-west-2
region.
The aws_instance
resource is used to launch Amazon EC2 instances. The vpc_security_group_ids
argument is used to specify the security groups for the instance, and it is set to the value of the security_groups
variable.
You can change the value of security_groups
by modifying the default value in the variable definition, or you can specify a different value when you run Terraform by using the -var
flag. For example, if you want to launch an instance with security groups ["default", "web-security-group"]
, you can run the following command:
variable "security_groups" { | |
type = list(string) | |
default = ["default"] | |
} | |
provider "aws" { | |
region = "us-west-2" | |
} | |
resource "aws_instance" "example" { | |
ami = "ami-0c55b159cbfafe1f0" | |
instance_type = "t2.micro" | |
vpc_security_group_ids = var.security_groups | |
} |
$ terraform apply -var ‘security_groups=[“default”, “web-security-group”]’
#Filename - terraform-variable-type-list.tf | |
provider "aws" { | |
region = "ap-south-1" | |
access_key = "" | |
secret_key = "" | |
} | |
provider "github" { | |
token = "" | |
organization = "" | |
} | |
variable "users" { | |
type = "list" | |
default = ["devopsschool11", "devopsschool2", "devopsschool3"] | |
description = "This is for demo of list variable" | |
} | |
variable "gitrepos" { | |
type = "list" | |
default = ["devopsschool11", "devopsschool2", "devopsschool3"] | |
description = "This is for demo of list variable" | |
} | |
resource "aws_iam_user" "iamuser" { | |
name = "${var.users[0]}" | |
} | |
resource "github_repository" "repo1" { | |
name = "${var.gitrepos[0]}" | |
description = "My awesome codebase" | |
private = false | |
} | |
resource "github_repository" "repo2" { | |
name = "${var.gitrepos[1]}" | |
description = "My awesome codebase" | |
private = false | |
} | |
resource "github_repository" "repo3" { | |
name = "${var.gitrepos[2]}" | |
description = "My awesome codebase" | |
private = false | |
} |










I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND