Definition:
Automate the provisioning of resources over the cloud.
To learn this topic , we need to below tools and SW are ready in our system
Terraform:https://www.terraform.io/downloads.html
Visual studio code: https://code.visualstudio.com/download
AWS CLI: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2-windows.html
Programmatic access from Terraform to AWS console.
Lets create a IAM user to provide access to Terraform.
Click "Next permissions"
Once User created ,Make a note of secret key and access key IDs to configure the access from AWS CLI to the console.
Open command prompt and type AWS to check your CLI access.
type aws configure command
Next download and configure Terraform tool
Source docs.oracle.com
To see the terraform success installation, check as below
Now lets use Visual studio code editor in my case, you case whatever editor your convenient.
Create a directory and with simple file name, in my case sample.tf. Form name must have extension .tf as a naming convention.
Folder structure will be like below
sample.tfprovider "aws"{
region="us-west-2"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "main"
}
}
resource "aws_subnet" "subnet1" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "Subnet1"
}
}
Once you enter the vpc and subnet code , you need to initialise the terraform by using the below
Before running to create AWS resources , lets see what we have us-west-2 region.
We have default VPC and subnets as below
VPC:
subnet:
Now execute "terraform apply" command in terminal and give "yes" as you want to approve to create the resources what you have asked.
See in above screen , we can see two resources are created ,lets open our console and check.
Check the resources in "us-west-2" region
VPC:
subnet
With this we can easily provision the AWS resources with terraform code.
Once it successfully executes the creation of resources, it will create the state form where it has the code to create the resources in AWS.
In our case it will be like below.
Now lets learn deep out resource filed and how we can separate the variables and initialize them .
See vars.tf , how we are separating
execute again by using terraform apply
This is how we can initialize the variables and execute them.
Now lets create multiple subnets using loops in terraform.
Since we are in us-west-2 region and lets check how many AZs are available at present.
Lets create CIDR blocks dynamically and create subnets against CIDR.
Now the vars.tf looks like below.
variable "region"{
default="us-west-2"
}
variable "vpc_cidr"{
default="10.0.0.0/16"
}
variable "subnet_cidr"{
type=list(string)
default=["10.0.5.0/24","10.0.2.0/24","10.0.3.0/24","10.0.4.0/24"]
}
variable "azs"{
type=list(string)
default=["us-west-2a","us-west-2b","us-west-2c","us-west-2d"]
}
sample.tf like below
provider "aws"{
region=var.region
}
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
instance_tenancy = "default"
tags = {
Name = "main"
}
}
resource "aws_subnet" "subnets" {
count=length(var.azs)
vpc_id = aws_vpc.main.id
cidr_block = element(var.subnet_cidr,count.index)
tags = {
Name = "Subnet1"
}
}
apply the terraform and see the resources are created as below.
When you see the above pic, we have subnet names are equal. Lets change the name of the subnet using the count.index
Just change the Name = "Subnet-${count.index+1}". and apply terraform.
As of Now we are hardcoded region and AZs but we can get those also dynamically using data sources.
Just change the code in vars.tf as below
#variable "azs"{
# type=list(string)
# default=["us-west-2a","us-west-2b","us-west-2c","us-west-2d"]
#}
# Declare the data source
data "aws_availability_zones" "azs" {
state = "available"
}
update the sample.tf as below
resource "aws_subnet" "subnets" {
count=length(data.aws_availability_zones.azs.names)
vpc_id = aws_vpc.main.id
cidr_block = element(var.subnet_cidr,count.index)
tags = {
Name = "Subnet-${count.index+1}"
}
}
apply the terraform, so here we can see same output as above though we are getting dynamically the data of AZs.
Since we the subnets are created in single AZ like below
Lets create subnets in different AZs as you want.
sample.tf:
count=length(data.aws_availability_zones.azs.names)
availability_zone=element(data.aws_availability_zones.azs.names,count.index)
Lets apply terraform
If you observe above pic, it created only 3 subnets again and 3 subnets deleted , because it make used the existing one and created the freshly with new AZs as below
Lets see other functions in Terraform.
Map:
Lets see without Map , how we can create ec2 instance using terraform.
vars.tf:
variable "region"{
default="ap-south-1"
}
variable "ec2_ami"{
default="ami-76d6f519"
}
provider.tf:
provider "aws"{
region=var.region
}
ec2-instance.tf:
resource "aws_instance" "web" {
ami = var.ec2_ami
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
}
Apply terraform and check the EC2 is created.
In above code , we specified the region and AMI , But when we change the region AMI id will change.
So , to get the AMI id based on the region we are using the Map.
Get the AMIs using Map and get the region using Lookup as below
vars.tf:
variable "region"{
default="ap-south-1"
}
variable "ec2_ami"{
type=map
default={
ap-south-1="ami-76d6f519"
us-west-2="ami-e251209a"
}
}
ec2-instance.tf:
resource "aws_instance" "web" {
ami = lookup(var.ec2_ami, var.region)
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
}
See the below there is no instance created in us-west-2 region
but the Ec2 instance is created in ap-south-1.
Thank you for reading 👍👍👍👍👍