A VARIABLE LIST
A variable list uses a type of input variable that allows you to define a list of values using terraform configuration and it comes in handy when parameterizing. With a variable list terraform configuration uses "LIST" in the variable declaration.
HANDS-ON:
The below steps will identify how to deploy variable list. And list can be seen as passing a string ["d", "e", "f" ]. We need the instance, security group and variables.
STEP 1:
First: create a folder'variable-list' and within the folder create a file "provider.tf".
terraform {
required_version = "1.3.4"
required_providers {
aws = {
source = "hashicorp/aws"
version = "4.39.0"
}
}
}
STEP 2:
Create a file "Variables.tf" and pass instance type variable as well as the count and list them as strings.
# dev instance ami id
variable "Dev-instance-ami-id" {
type = string
default = "ami-0b0dc*****52a63"
}
# dev instance type
variable "Dev-instance-type" {
type = list(string)
default = ["t2.micro" ,"t2.nano", "t2.large", "t2.small"]
}
# dev vpc cidr block
variable "Dev-vpc-cidrblock" {
type = string
default = "10.0.0.0/16"
}
# dev vpc instance tenancy
variable "Dev-vpc-instance-tenency" {
type = string
default = "default"
}
# dev subnet 1 cidr block
variable "Dev-subnet-1-cidrblock" {
type = string
default = "10.0.1.0/24"
}
# dev subnet 1 availability zone
variable "Dev-subnet-1-availability-zone" {
type = string
default = "us-east-1a"
}
# dev subnet 2 cidr block
variable "Dev-subnet-2-cidrblock" {
type = string
default = "10.0.2.0/24"
}
# dev subnet 2 availability zone
variable "Dev-subnet-2-availability-zone" {
type = string
default = "us-east-1b"
}
variable "provider-profile" {
type = string
default = "default"
}
variable "dev-count" {
description = "dev count"
type = list(number)
default = [1, 3, 5, 10]
}
STEP 3:
Create a file "ec2.tf" and pass the instance type and the count using the applicable "index" (0,1,2,.....n)
resource "aws_instance" "Development-VM" {
ami = var.Dev-instance-ami-id
instance_type = var.Dev-instance-type[1]
count = var.dev-count[0] # create four similar EC2 instances
subnet_id = aws_subnet.Dev-subnet-1.id
vpc_security_group_ids = [aws_security_group.Development-SG.id]
tags = {
Name = "Dev-VM"
}
}
STEP 4:
We provisioned a "SG"
resource "aws_security_group" "Development-SG" {
name = "Development-SG"
description = "Development Security Group"
vpc_id = aws_vpc.Dev-VPC.id
ingress {
description = "TLS from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
### -1 protocol for egress means allow all traffic, and the below notation for ipv6 is the general way in which ipv6 is recognized.connection {
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "allow_http traffic"
}
}
STEP 5:
Create a file called "Vpc-network.tf"
# Development VPC
resource "aws_vpc" "Dev-VPC" {
cidr_block = var.Dev-vpc-cidrblock
instance_tenancy = var.Dev-vpc-instance-tenency
tags = {
Name = "Dev-VPC"
}
}
# Development subnet 1
resource "aws_subnet" "Dev-subnet-1" {
vpc_id = aws_vpc.Dev-VPC.id
cidr_block = var.Dev-subnet-1-cidrblock
availability_zone = var.Dev-subnet-1-availability-zone
tags = {
Name = "Dev-subnet-1"
}
}
# Development subnet 2
resource "aws_subnet" "Dev-subnet-2" {
vpc_id = aws_vpc.Dev-VPC.id
cidr_block = var.Dev-subnet-2-cidrblock
availability_zone = var.Dev-subnet-2-availability-zone
tags = {
Name = "Dev-subnet-2"
}
}
# Development VPC internet Gateway
resource "aws_internet_gateway" "Dev-VPC-IGW" {
vpc_id = aws_vpc.Dev-VPC.id
tags = {
Name = "Dev-VPC-IGW"
}
}
STEP 6:
"cd" into the path of the folder.
"ls" - list what you have inside the folder.
STEP 7:
Apply terraform command
-Init
-Validate
-Plan
-Apply
- Destroy
Happy learning 😊
Referecing: HashiCorp - https://developer.hashicorp.com/terraform/language/values/variables.
No comments:
Post a Comment