Activity 2 AWS Infra Contd
- We have created the resource which look as shown below
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
}
}
# Configure the AWS Provider
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "primary_vpc" {
cidr_block = "192.168.0.0/16"
tags = {
Name = "primary"
}
}
resource "aws_subnet" "web_1" {
vpc_id = aws_vpc.primary_vpc.id
availability_zone = "us-west-2a"
cidr_block = "192.168.0.0/24"
tags = {
Name = "Web1"
}
}
resource "aws_subnet" "web_2" {
vpc_id = aws_vpc.primary_vpc.id
availability_zone = "us-west-2b"
cidr_block = "192.168.1.0/24"
tags = {
Name = "Web2"
}
}
resource "aws_subnet" "db_1" {
vpc_id = aws_vpc.primary_vpc.id
availability_zone = "us-west-2a"
cidr_block = "192.168.2.0/24"
tags = {
Name = "db1"
}
}
resource "aws_subnet" "db_2" {
vpc_id = aws_vpc.primary_vpc.id
availability_zone = "us-west-2b"
cidr_block = "192.168.3.0/24"
tags = {
Name = "db2"
}
}
- Lets try to parametrize the configuration, which gives the ability to the user pass information while running the template
- To parametrize we would use variables (input variables, terraform variables)
- Refer Here for the docs
- To create a variable we need to define a variable block. Lets create a variable for vpc cidr
variable "vpc_cidr" {
default = "192.168.0.0/16"
description = "This is the VPC cidr"
type = string
}
-
To access/use this variable
var.<VARIABLE_NAME>is the syntax -
Refer Here for the changes
-
To pass the variable value from cli
terraform apply -var="vpc_cidr=192.168.0.0/16" -
Lets create a variable for subnet cidr ranges
-
Refer Here for the changeset
-
Now Lets try to create subnet resources by using the count-meta argument Refer Here
-
Refer Here for the change set with changes to use the count.
-
Impact of changes in the terraform configuration on already created infra:
- Updates in place
- Must be replaced => Existing resource will be deleted and new one will be created.
- Updates in place
